Conversion Functions

openLooKeng will implicitly convert numeric and character values to the correct type if such a conversion is possible. For any other types, by default, openLooKeng will not convert it implicitly. For example, a query that expects a varchar will not automatically convert a bigint value to an equivalent varchar.

When necessary, values can be explicitly cast to a particular type.

Or, you can enable the implicit conversion functionality, then openLooKeng will try to auto apply conversion between source type and target type.

Conversion Functions

cast(value AS type) -> type

Explicitly cast a value as a type. This can be used to cast a varchar to a numeric value type and vice versa.

try_cast(value AS type) -> type

Like cast, but returns null if the cast fails.

Formatting

format(format, args...) -> varchar

Returns a formatted string using the specified format string and arguments:

SELECT format('%s%%', 123); -- '123%'
SELECT format('%.5f', pi()); -- '3.14159'
SELECT format('%03d', 8); -- '008'
SELECT format('%,.2f', 1234567.89); -- '1,234,567.89'
SELECT format('%-7s,%7s', 'hello', 'world'); --  'hello  ,  world'
SELECT format('%2$s %3$s %1$s', 'a', 'b', 'c'); -- 'b c a'
SELECT format('%1$tA, %1$tB %1$te, %1$tY', date '2006-07-04'); -- 'Tuesday, July 4, 2006'

Data Size

The parse_presto_data_size function supports the following units:

UnitDescriptionValue
BBytes1
kBKilobytes1024
MBMegabytes1024^2
GBGigabytes1024^3
TBTerabytes1024^4
PBPetabytes1024^5
EBExabytes1024^6
ZBZettabytes1024^7
YBYottabytes1024^8

parse_presto_data_size(string) -> decimal(38)

Parses string of format value unit into a number, where value is the fractional number of unit values:

SELECT parse_presto_data_size('1B'); -- 1
SELECT parse_presto_data_size('1kB'); -- 1024
SELECT parse_presto_data_size('1MB'); -- 1048576
SELECT parse_presto_data_size('2.3MB'); -- 2411724

Implicit Conversion

You can enable the implicit conversion vis setting session property:

SET SESSION implicit_conversion=true

By default, the property value is false, the implicit conversion is turned off.

If set it to true, whenever openLooKeng found the type is not match, but probably can be compatible with each other, openLooKeng will automatically rewrite the statement by applying the CAST function.

In this way, user do not need to explicitly add CAST function to convert it.

For example, a query that expects a varchar will automatically convert a bigint value to an equivalent varchar.

Obviously, not all data types are compatible with each other, below table lists all the feasible conversion for all basic datatype.

BOOLEANTINYINTSMALLINTINTEGERBIGINTREALDOUBLEDECIMALVARCHARCHARVARBINARYJSONDATETIMETIME WITH TIME ZONETIMESTAMPTIMESTAMP WITH TIME ZONE
BOOLEANY(1)YYYYYYY(2)NNYNNNNN
TINYINTY(3)YYYYYYYNNYNNNNN
SMALLINTYY(4)YYYYYYNNYNNNNN
INTEGERYYYYYYYYNNYNNNNN
BIGINTYYYYYYYYNNYNNNNN
REALYYYYYYY(5)YNNYNNNNN
DOUBLEYYYYYYYYNNYNNNNN
DECIMALYYYYYYY(6)YNNYNNNNN
VARCHARY(7)YYYYYYY(8)Y(9)YYY(10)Y(11)Y(12)Y(13)Y
CHARNNNNNNNNYNNNNNNN
VARBINARYNNNNNNNNNNNNNNNN
JSONNNNNNNNNYNNNNNNN
DATENNNNNNNNYNNYNNY(14)Y
TIMENNNNNNNNYNNNNY(15)Y(16)Y
TIME WITHNNNNNNNNYNNNNYYY
TIME ZONE
TIMESTAMPNNNNNNNNYNNNYYYY
TIMESTAMPNNNNNNNNYNNNYYYY
WITH TIME
ZONE

Note:

  • Y or Y(#): standard for support implicit convert. But there might be some limitation need your attention. Please refer to below item.
  • N: standard for not support implicit convert

(1): BOOLEAN->NUMBER the converted result can be only 0 or 1

(2): BOOLEAN->VARCHAR the converted result can be only TRUE or FALSE

(3): NUMBER -> BOOLEAN 0 will be converted to false, others will be converted to true

(4): BIG PRECISION -> SAMLL conversion will fail when data is out of range of SMALL

(5): REAL/FLOAT ->DECIMAL conversion will fail when data is out of range of DECIMAL. Scale will be cut off when out of range.

(6): DECIMAL->DECIMAL conversion will fail when data is out of range of DECIMAL. Scale will be cut off when out of range.

(7): VARCHAR->BOOLEAN only '0','1','TRUE','FALSE' can be converted. Others will be failed

(8): VARCHAR->DECIMAL conversion will fail when it’s not an numeric or the converted value is out of range of DECIMAL. Scale will be cut off when out of range.

(9): VARCHAR->CHAR if length of VARCHAR is larger than CHAR, it will be cut off.

(10): VARCHAR->DATE The VARCHAR can only be formatted like: 'YYYY-MM-DD', e.g. 2000-01-01

(11): VARCHAR->TIME The VARCHAR can only be formatted like: 'HH:MM:SS.XXX'

(12): VARCHAR->TIME ZONE The VARCHAR can only be formatted like: 'HH:MM:SS.XXX XXX', e.g. 01:02:03.456 America/Los_Angeles

(13): VARCHAR->TIMESTAMP The VARCHAR can only be formatted like: YYYY-MM-DD HH:MM:SS.XXX

(14): DATE->TIMESTAMP will auto padding the time with 0. e.g. '2010-01-01' -> 2010-01-01 00:00:00.000

(15): TIME->TIME WITH TIME ZONE will auto padding the default time zone

(16): TIME->TIMESTAMP will auto add the default date: 1970-01-01

Miscellaneous

typeof(expr) -> varchar

Returns the name of the type of the provided expression:

SELECT typeof(123); -- integer
SELECT typeof('cat'); -- varchar(3)
SELECT typeof(cos(2) + 1.5); -- double

有奖捉虫

“有虫”文档片段

0/500

存在的问题

文档存在风险与错误

● 拼写,格式,无效链接等错误;

● 技术原理、功能、规格等描述和软件不一致,存在错误;

● 原理图、架构图等存在错误;

● 版本号不匹配:文档版本或内容描述和实际软件不一致;

● 对重要数据或系统存在风险的操作,缺少安全提示;

● 排版不美观,影响阅读;

内容描述不清晰

● 描述存在歧义;

● 图形、表格、文字等晦涩难懂;

● 逻辑不清晰,该分类、分项、分步骤的没有给出;

内容获取有困难

● 很难通过搜索引擎,openLooKeng官网,相关博客找到所需内容;

示例代码有错误

● 命令、命令参数等错误;

● 命令无法执行或无法完成对应功能;

内容有缺失

● 关键步骤错误或缺失,无法指导用户完成任务,比如安装、配置、部署等;

● 逻辑不清晰,该分类、分项、分步骤的没有给出

● 图形、表格、文字等晦涩难懂

● 缺少必要的前提条件、注意事项等;

● 描述存在歧义

0/500

您对文档的总体满意度

非常不满意
非常满意

请问是什么原因让您参与到这个问题中

您的邮箱

创Issue赢奖品
根据您的反馈,会自动生成issue模板。您只需点击按钮,创建issue即可。
有奖捉虫