Conversion Functions

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

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

    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.

    format(format, args…) -> varchar

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

    The parse_presto_data_size function supports the following units:

    parse_presto_data_size(string) -> decimal(38)

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

    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:

    • 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.

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

    (8): VARCHAR->DECIMAL conversion will fail when its 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

    typeof(expr) -> varchar