Conversion Functions
在表达式求值期间,Pollux 不会将表达式参数隐式转换为正确的类型。如果需要进行此类转换,可以使用提供的两个转换函数显式地将值转换为特定类型。
Conversion Functions
cast(value AS type) -> type
将值显式转换为类型。这可用于将 varchar 转换为数值类型,反之亦然。
try_cast(value AS type) -> type
类似于 :func:`cast`,但如果转换失败则返回 null。``try_cast(x AS type)`` 与 ``try(cast(x AS type))``
不同,``try_cast`` 仅抑制转换过程中发生的错误,而不会抑制其参数求值过程中发 生的错误。例如,
``try_cast(x / 0 as double)`` 会抛出除以 0 的错误,而 ``try(cast(x / 0 as double))`` 则返回 NULL。
Supported Conversions
支持的转换如下所示,其中行指定起始类型,列指定目标类型。如果 ARRAY、MAP 和 ROW 类型的元素类型转换受支持,则这些类型的转换也受支持。此外,
支持的 JSON 转换列表列于 :doc:json 中。
| tinyint | smallint | integer | bigint | boolean | real | double | varchar | varbinary | timestamp | timestamp with time zone | date | interval day to second | decimal | ipaddress | ipprefix | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| tinyint | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| smallint | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| integer | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| bigint | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| boolean | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| real | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| double | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| varchar | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | ||
| varbinary | Y | |||||||||||||||
| timestamp | Y | Y | Y | Y | ||||||||||||
| timestamp with time zone | Y | Y | Y | |||||||||||||
| date | Y | Y | Y | |||||||||||||
| interval day to second | Y | |||||||||||||||
| decimal | Y | Y | Y | Y | Y | Y | Y | Y | Y | |||||||
| ipaddress | Y | Y | Y | |||||||||||||
| ipprefix | Y | Y | Y |
Cast to Integral Types
整数类型包括 bigint、integer、smallint 和 tinyint。
From integral types
当输入值在结果类型的范围内时,允许将一个整数类型转换为另一个整数类型。从无效输入值进行转换会抛出异常。
有效示例:
SELECT cast(1234567 as bigint); -- 1234567
SELECT cast(12 as tinyint); -- 12
无效示例
SELECT cast(1234 as tinyint); -- Out of range
SELECT cast(1234567 as smallint); -- Out of range
From floating-point types
从浮点输入转换为整数类型会将输入值四舍五入为最接近的整数值。当四舍五入的结果在结果类型的范围内时,允许这样做。从无效输入值进行转换会抛出错误。
有效示例
SELECT cast(12345.12 as bigint); -- 12345
SELECT cast(12345.67 as bigint); -- 12346
SELECT cast(127.1 as tinyint); -- 127
SELECT cast(nan() as integer); -- 0
SELECT cast(nan() as smallint); -- 0
SELECT cast(nan() as tinyint); -- 0
无效示例
SELECT cast(127.8 as tinyint); -- Out of range
SELECT cast(1234567.89 as smallint); -- Out of range
SELECT cast(infinity() as bigint); -- Out of range
在 Pollux 中,将 NaN 强制转换为 bigint 会返回 0,但在 Presto 中则会抛出错误。我们特意保留了 Pollux 的行为,因为这与其他受支持的情况更加一致。
SELECT cast(nan() as bigint); -- 0
From VARCHAR
如果字符串表示结果类型范围内的整数,则允许将其转换为整数类型。默认情况下,不允许从表示浮点数的字符串进行转换。 从无效输入值进行转换会抛出异常。
有效示例
SELECT cast('12345' as bigint); -- 12345
SELECT cast('+1' as tinyint); -- 1
SELECT cast('-1' as tinyint); -- -1
无效示例
SELECT cast('12345.67' as tinyint); -- Invalid argument
SELECT cast('12345.67' as bigint); -- Invalid argument
SELECT cast('1.2' as tinyint); -- Invalid argument
SELECT cast('-1.8' as tinyint); -- Invalid argument
SELECT cast('1.' as tinyint); -- Invalid argument
SELECT cast('-1.' as tinyint); -- Invalid argument
SELECT cast('0.' as tinyint); -- Invalid argument
SELECT cast('.' as tinyint); -- Invalid argument
SELECT cast('-.' as tinyint); -- Invalid argument
From decimal
小数部分已四舍五入。
有效示例
SELECT cast(2.56 decimal(6, 2) as integer); -- 3
SELECT cast(3.46 decimal(6, 2) as integer); -- 3
无效示例
SELECT cast(214748364890 decimal(12, 2) as integer); -- Out of range
Cast to Boolean
From integral and floating-point types
允许将整数或浮点数转换为布尔值。非零数字会被转换为“true”,而零会被转换为“false”。
有效示例
SELECT cast(1 as boolean); -- true
SELECT cast(0 as boolean); -- false
SELECT cast(12 as boolean); -- true
SELECT cast(-1 as boolean); -- true
SELECT cast(1.0 as boolean); -- true
SELECT cast(1.1 as boolean); -- true
SELECT cast(-1.1 as boolean); -- true
SELECT cast(nan() as boolean); -- true
SELECT cast(infinity() as boolean); -- true
SELECT cast(0.0000000000001 as boolean); -- true
SELECT cast(0.5 as boolean); -- true
SELECT cast(-0.5 as boolean); -- true
From VARCHAR
字符串 t, f, 1, 0, true, false 及其大写等价物可以转换为布尔值。
从其他字符串转换为布尔值会抛出异常。
有效示例
SELECT cast('1' as boolean); -- true
SELECT cast('0' as boolean); -- false
SELECT cast('t' as boolean); -- true (case insensitive)
SELECT cast('true' as boolean); -- true (case insensitive)
SELECT cast('f' as boolean); -- false (case insensitive)
SELECT cast('false' as boolean); -- false (case insensitive)
SELECT cast('F' as boolean); -- false (case insensitive)
SELECT cast('T' as boolean); -- true (case insensitive)
无效示例
SELECT cast('1.7E308' as boolean); -- Invalid argument
SELECT cast('nan' as boolean); -- Invalid argument
SELECT cast('infinity' as boolean); -- Invalid argument
SELECT cast('12' as boolean); -- Invalid argument
SELECT cast('-1' as boolean); -- Invalid argument
SELECT cast('tr' as boolean); -- Invalid argument
SELECT cast('tru' as boolean); -- Invalid argument
SELECT cast('No' as boolean); -- Invalid argument
Cast to Floating-Point Types
From integral or floating-point types
允许从整数或浮点数进行强制类型转换。
有效示例
SELECT cast(1 as real); -- 1.0
SELECT cast(123.45 as real); -- 123.45
在将超出 real 限制的值转换为 real 时,Pollux 的行为与 Presto 的行为存在两种差异。我们将修复这些问题,使其与 Presto 的行为保持一致。
SELECT cast(1.7E308 as real); -- Presto returns Infinity but Pollux throws
SELECT cast(-1.7E308 as real); -- Presto returns -Infinity but Pollux throws
From VARCHAR
如果字符串表示整数或浮点数,则允许将其转换为实数。如果输入值无效,则转换将引发错误。
有效示例
SELECT cast('1.' as real); -- 1.0
SELECT cast('1' as real); -- 1.0
SELECT cast('1.7E308' as real); -- Infinity
SELECT cast('Infinity' as real); -- Infinity (case sensitive)
SELECT cast('-Infinity' as real); -- -Infinity (case sensitive)
SELECT cast('NaN' as real); -- NaN (case sensitive)
无效示例
SELECT cast('1.2a' as real); -- Invalid argument
SELECT cast('1.2.3' as real); -- Invalid argument
SELECT cast('infinity' as real); -- Invalid argument
SELECT cast('-infinity' as real); -- -Invalid argument
SELECT cast('inf' as real); -- Invalid argument
SELECT cast('InfiNiTy' as real); -- Invalid argument
SELECT cast('INFINITY' as real); -- Invalid argument
SELECT cast('nAn' as real); -- Invalid argument
SELECT cast('nan' as real); -- Invalid argument
Presto 支持以下情况,但 Pollux 也支持。
SELECT cast('1.2f' as real); -- 1.2
SELECT cast('1.2f' as double); -- 1.2
SELECT cast('1.2d' as real); -- 1.2
SELECT cast('1.2d' as double); -- 1.2
From decimal
允许从十进制转换为双精度型、浮点型或任何整数类型。在十进制到整数类型的转换过程中,如果结果溢出或下溢,则会引发异常。
有效示例
SELECT cast(decimal '10.001' as double); -- 10.001
无效示例
SELECT cast(decimal '300.001' as tinyint); -- Out of range
Cast to VARCHAR
允许从标量类型转换为字符串。
有效示例
SELECT cast(123 as varchar); -- '123'
SELECT cast(123.45 as varchar); -- '123.45'
SELECT cast(123.0 as varchar); -- '123.0'
SELECT cast(nan() as varchar); -- 'NaN'
SELECT cast(infinity() as varchar); -- 'Infinity'
SELECT cast(true as varchar); -- 'true'
SELECT cast(timestamp '1970-01-01 00:00:00' as varchar); -- '1970-01-01 00:00:00.000'
SELECT cast(timestamp '2024-06-01 11:37:15.123 America/New_York' as varchar); -- '2024-06-01 11:37:15.123 America/New_York'
SELECT cast(cast(22.51 as DECIMAL(5, 3)) as varchar); -- '22.510'
SELECT cast(cast(-22.51 as DECIMAL(4, 2)) as varchar); -- '-22.51'
SELECT cast(cast(0.123 as DECIMAL(3, 3)) as varchar); -- '0.123'
SELECT cast(cast(1 as DECIMAL(6, 2)) as varchar); -- '1.00'
SELECT cast(cast(0 as DECIMAL(6, 2)) as varchar); -- '0.00'
From Floating-Point Types
默认情况下,将实数或双精度数转换为字符串时,如果输入值的幅度大于或等于 10 -3 且小于 10 -7,则返回标准表示法,否则返回科学计数法。
正零返回“0.0”,负零返回“-0.0”。正无穷大返回“无穷大”,负无穷大返回“-无穷大”。正负 NaN 均返回“NaN”。
如果 legacy_cast 配置属性为 true,则所有输入值的结果均为标准表示法。
Valid examples if legacy_cast = false,
SELECT cast(double '123456789.01234567' as varchar); -- '1.2345678901234567E8'
SELECT cast(double '10000000.0' as varchar); -- '1.0E7'
SELECT cast(double '12345.0' as varchar); -- '12345.0'
SELECT cast(double '-0.001' as varchar); -- '-0.001'
SELECT cast(double '-0.00012' as varchar); -- '-1.2E-4'
SELECT cast(double '0.0' as varchar); -- '0.0'
SELECT cast(double '-0.0' as varchar); -- '-0.0'
SELECT cast(infinity() as varchar); -- 'Infinity'
SELECT cast(-infinity() as varchar); -- '-Infinity'
SELECT cast(nan() as varchar); -- 'NaN'
SELECT cast(-nan() as varchar); -- 'NaN'
SELECT cast(real '123456780.0' as varchar); -- '1.2345678E8'
SELECT cast(real '10000000.0' as varchar); -- '1.0E7'
SELECT cast(real '12345.0' as varchar); -- '12345.0'
SELECT cast(real '-0.001' as varchar); -- '-0.001'
SELECT cast(real '-0.00012' as varchar); -- '-1.2E-4'
SELECT cast(real '0.0' as varchar); -- '0.0'
SELECT cast(real '-0.0' as varchar); -- '-0.0'
Valid examples if legacy_cast = true,
SELECT cast(double '123456789.01234567' as varchar); -- '123456789.01234567'
SELECT cast(double '10000000.0' as varchar); -- '10000000.0'
SELECT cast(double '-0.001' as varchar); -- '-0.001'
SELECT cast(double '-0.00012' as varchar); -- '-0.00012'
SELECT cast(real '123456780.0' as varchar); -- '123456784.0'
SELECT cast(real '10000000.0' as varchar); -- '10000000.0'
SELECT cast(real '12345.0' as varchar); -- '12345.0'
SELECT cast(real '-0.00012' as varchar); -- '-0.00011999999696854502'
From DATE
将 DATE 转换为 VARCHAR 将返回 ISO-8601 格式的字符串:YYYY-MM-DD。
SELECT cast(date('2024-03-14') as varchar); -- '2024-03-14'
From TIMESTAMP
默认情况下,将时间戳转换为字符串会返回 ISO 8601 格式 ,日期和时间之间以空格作为分隔符,年份部分用零填充至 4 个字符。
如果 legacy_cast 配置属性为 true,则结果字符串将使用字符“T”作为日期和时间之间的分隔符,年份部分不填充。
Valid examples if legacy_cast = false,
SELECT cast(timestamp '1970-01-01 00:00:00' as varchar); -- '1970-01-01 00:00:00.000'
SELECT cast(timestamp '2000-01-01 12:21:56.129' as varchar); -- '2000-01-01 12:21:56.129'
SELECT cast(timestamp '384-01-01 08:00:00.000' as varchar); -- '0384-01-01 08:00:00.000'
SELECT cast(timestamp '10000-02-01 16:00:00.000' as varchar); -- '10000-02-01 16:00:00.000'
SELECT cast(timestamp '-10-02-01 10:00:00.000' as varchar); -- '-0010-02-01 10:00:00.000'
Valid examples if legacy_cast = true,
SELECT cast(timestamp '1970-01-01 00:00:00' as varchar); -- '1970-01-01T00:00:00.000'
SELECT cast(timestamp '2000-01-01 12:21:56.129' as varchar); -- '2000-01-01T12:21:56.129'
SELECT cast(timestamp '384-01-01 08:00:00.000' as varchar); -- '384-01-01T08:00:00.000'
SELECT cast(timestamp '-10-02-01 10:00:00.000' as varchar); -- '-10-02-01T10:00:00.000'
From INTERVAL DAY TO SECOND
将 INTERVAL DAY TO SECOND 转换为 VARCHAR 类型将返回一个格式为 '[sign]D HH:MM:SS.ZZZ' 的字符串,其中,如果间隔为负数,则 'sign' 为可选的 '-' 符号,D 表示间隔中的整天数,HH 表示介于 00 到 24 之间的小时数,MM 表示介于 00 到 59 之间的分钟数,SS 表示介于 00 到 59 之间的秒数,zzz 表示介于 000 到 999 之间的毫秒数。
SELECT cast(interval '1' day as varchar); -- '1 00:00:00.000'
SELECT cast(interval '123456' second as varchar); -- '1 10:17:36.000'
SELECT cast(now() - date('2024-03-01') as varchar); -- '35 09:15:54.092'
SELECT cast(date('2024-03-01') - now() as varchar); -- '-35 09:16:20.598'
From IPADDRESS
对于 IPV4 格式的 IPV6 地址,将 IPADDRESS 转换为 VARCHAR 会返回格式为 x.x.x.x 的字符串。 对于所有其他 IPV6 地址,它将被格式化为 RFC 4291#section-2.2 中定义的压缩替代格式 IPV6。
IPV4:
SELECT cast(ipaddress '1.2.3.4' as varchar); -- '1.2.3.4'
IPV6:
SELECT cast(ipaddress '2001:0db8:0000:0000:0000:ff00:0042:8329' as varchar); -- '2001:db8::ff00:42:8329'
SELECT cast(ipaddress '0:0:0:0:0:0:13.1.68.3' as varchar); -- '::13.1.68.3'
IPV4 mapped IPV6:
SELECT cast(ipaddress '::ffff:ffff:ffff' as varchar); -- '255.255.255.255'
From IPPREFIX
对于 IPv4 格式的 IPv6 地址,将 IPPREFIX 转换为 VARCHAR 会返回一个格式为 x.x.x.x/<prefix-length> 的字符串。
对于所有其他 IPv6 地址,它将被格式化为 RFC 4291#section-2.2
中定义的压缩替代格式 IPv6,后跟 <prefix-length>。RFC 4291#section-2.3
IPv4:
SELECT cast(ipprefix '1.2.0.0/16' as varchar); -- '1.2.0.0/16'
IPv6:
SELECT cast(ipprefix '2001:db8::ff00:42:8329/128' as varchar); -- '2001:db8::ff00:42:8329/128'
SELECT cast(ipprefix '0:0:0:0:0:0:13.1.68.3/32' as varchar); -- '::/32'
IPv4 mapped IPv6:
SELECT cast(ipaddress '::ffff:ffff:0000/16' as varchar); -- '255.255.0.0/16'
Cast to VARBINARY
From IPADDRESS
以网络字节序的 16 字节 varbinary 字符串形式返回 IPV6 地址。
该类型在内部是纯 IPv6 地址。对 IPv4 的支持使用 IPv4 映射的 IPv6 地址范围 `RFC 4291#section-2.5.5.2 进行处理。 创建 IPADDRESS 时,IPv4 地址将映射到该范围。
IPV6:
SELECT cast(ipaddress '2001:0db8:0000:0000:0000:ff00:0042:8329' as varbinary); -- 0x20010db8000000000000ff0000428329
IPV4:
SELECT cast('1.2.3.4' as ipaddress); -- 0x00000000000000000000ffff01020304
IPV4 mapped IPV6:
SELECT cast('::ffff:ffff:ffff' as ipaddress); -- 0x00000000000000000000ffffffffffff
Cast to TIMESTAMP
From VARCHAR
如果字符串表示的时间戳格式为“YYYY-MM-DD”,后接可选的“hh:mm:ss.MS”,则允许将其转换为时间戳。 秒和毫秒是可选的。从无效输入值进行转换会抛出异常。
有效示例:
SELECT cast('1970-01-01' as timestamp); -- 1970-01-01 00:00:00
SELECT cast('1970-01-01 00:00:00.123' as timestamp); -- 1970-01-01 00:00:00.123
SELECT cast('1970-01-01 02:01' as timestamp); -- 1970-01-01 02:01:00
SELECT cast('1970-01-01 00:00:00-02:00' as timestamp); -- 1970-01-01 02:00:00
Invalid example:
SELECT cast('2012-Oct-23' as timestamp); -- Invalid argument
字符串末尾还可以包含时区信息(可选)。时区信息可以是偏移量,例如 +01:00 或 -02:00 格式,也可以是时区名称,例如 UTC、Z、America/Los_Angeles 等,
定义见此处。
例如,以下字符串包含有效的时区信息:
SELECT cast('1970-01-01 00:00:00 +09:00' as timestamp);
SELECT cast('1970-01-01 00:00:00 UTC' as timestamp);
SELECT cast('1970-01-01 00:00:00 America/Sao_Paulo' as timestamp);
如果字符串中指定了时区信息,则返回的时间戳将调整为相应的时区。否则,时间戳将被假定为客户端会话时区,并根据
adjust_timestamp_to_session_timezone 的值进行相应调整,如下所述。小时和时区定义之间的空格是可选的。
SELECT cast('1970-01-01 00:00 Z' as timestamp);
SELECT cast('1970-01-01 00:00Z' as timestamp);
Are both valid.
From DATE
允许从日期转换为时间戳。
有效示例
SELECT cast(date '1970-01-01' as timestamp); -- 1970-01-01 00:00:00
SELECT cast(date '2012-03-09' as timestamp); -- 2012-03-09 00:00:00
From TIMESTAMP WITH TIME ZONE
结果取决于是否设置了配置属性“adjust_timestamp_to_session_timezone”。
如果设置为 true,则忽略输入时区,并按原样返回时间戳。例如,
1970-01-01 00:00:00.000 America/Los_Angeles将变为1970-01-01 08:00:00.000。
否则,时间戳将根据时区的偏移量进行偏移。例如,
1970-01-01 00:00:00.000 America/Los_Angeles将变为1970-01-01 00:00:00.000。
有效示例
-- `adjust_timestamp_to_session_timezone` is true
SELECT to_unixtime(cast(timestamp '1970-01-01 00:00:00 America/Los_Angeles' as timestamp)); -- 28800.0 (1970-01-01 08:00:00.000)
SELECT to_unixtime(cast(timestamp '2012-03-09 10:00:00 Asia/Chongqing' as timestamp)); -- 1.3312584E9 (2012-03-09 02:00:00.000)
SELECT to_unixtime(cast(from_unixtime(0, '+06:00') as timestamp)); -- 0.0 (1970-01-01 00:00:00.000)
SELECT to_unixtime(cast(from_unixtime(0, '-02:00') as timestamp)); -- 0.0 (1970-01-01 00:00:00.000)
-- `adjust_timestamp_to_session_timezone` is false
SELECT to_unixtime(cast(timestamp '1970-01-01 00:00:00 America/Los_Angeles' as timestamp)); -- 0.0 (1970-01-01 00:00:00.000)
SELECT to_unixtime(cast(timestamp '2012-03-09 10:00:00 Asia/Chongqing' as timestamp)); -- 1.3312872E9 (2012-03-09 10:00:00.000)
SELECT to_unixtime(cast(from_unixtime(0, '+06:00') as timestamp)); -- 21600.0 (1970-01-01 06:00:00.000)
SELECT to_unixtime(cast(from_unixtime(0, '-02:00') as timestamp)); -- -7200.0 (1969-12-31 22:00:00.000)
Cast to TIMESTAMP WITH TIME ZONE
From TIMESTAMP
结果取决于是否设置了配置属性“adjust_timestamp_to_session_timezone”。
如果设置为 true,则输出将根据用户提供的“session_timezone”(如果有)调整为与输入时间戳(UTC)相同的时间。例如,当用户输入America/Los_Angeles时,
1970-01-01 00:00:00.000将变为1969-12-31 16:00:00.000 America/Los_Angeles。
否则,用户提供的“session_timezone”(如果有)将直接附加到输入时间戳。例如,1970-01-01 00:00:00.000将变为1970-01-01 00:00:00.000 America/Los_Angeles。
有效示例
-- `adjust_timestamp_to_session_timezone` is true
SELECT cast(timestamp '1970-01-01 00:00:00' as timestamp with time zone); -- 1969-12-31 16:00:00.000 America/Los_Angeles
SELECT cast(timestamp '2012-03-09 10:00:00' as timestamp with time zone); -- 2012-03-09 02:00:00.000 America/Los_Angeles
SELECT cast(from_unixtime(0) as timestamp with time zone); -- 1969-12-31 16:00:00.000 America/Los_Angeles
-- `adjust_timestamp_to_session_timezone` is false
SELECT cast(timestamp '1970-01-01 00:00:00' as timestamp with time zone); -- 1970-01-01 00:00:00.000 America/Los_Angeles
SELECT cast(timestamp '2012-03-09 10:00:00' as timestamp with time zone); -- 2012-03-09 10:00:00.000 America/Los_Angeles
SELECT cast(from_unixtime(0) as timestamp with time zone); -- 1970-01-01 00:00:00.000 America/Los_Angeles
From DATE
The results depend on session_timestamp.
Valid examples
-- session_timezone = America/Los_Angeles
SELECT cast(date '2024-06-01' as timestamp with time zone); -- 2024-06-01 00:00:00.000 America/Los_Angeles
-- session_timezone = Asia/Shanghai
SELECT cast(date '2024-06-01' as timestamp with time zone); -- 2024-06-01 00:00:00.000 Asia/Shanghai
Cast to Date
From VARCHAR
仅支持 ISO 8601 字符串:[+-]YYYY-MM-DD。从无效输入值进行转换将引发错误。
Valid examples
SELECT cast('1970-01-01' as date); -- 1970-01-01
Invalid examples
SELECT cast('2012' as date); -- Invalid argument
SELECT cast('2012-10' as date); -- Invalid argument
SELECT cast('2012-10-23T123' as date); -- Invalid argument
SELECT cast('2012-10-23 (BC)' as date); -- Invalid argument
SELECT cast('2012-Oct-23' as date); -- Invalid argument
SELECT cast('2012/10/23' as date); -- Invalid argument
SELECT cast('2012.10.23' as date); -- Invalid argument
SELECT cast('2012-10-23 ' as date); -- Invalid argument
From TIMESTAMP
允许从时间戳转换为日期。如果存在,则输入中的“hh:mm:ss”部分将被忽略。
有效示例
SELECT cast(timestamp '1970-01-01 00:00:00' as date); -- 1970-01-01
SELECT cast(timestamp '1970-01-01 23:59:59' as date); -- 1970-01-01
From TIMESTAMP WITH TIME ZONE
允许从带时 区的 TIMESTAMP 转换为 DATE。如果存在,则输入中的“hh:mm:ss”部分将被忽略。
会话时区不会影响结果。
有效示例
SELECT CAST(timestamp '2024-06-01 01:38:00 America/New_York' as DATE); -- 2024-06-01
Cast to Decimal
From boolean type
允许将布尔值转换为指定精度和小数位数的十进制数。 真值转换为 1,假值转换为 0。
有效示例
SELECT cast(true as decimal(4, 2)); -- decimal '1.00'
SELECT cast(false as decimal(8, 2)); -- decimal '0'
From integral types
如果输入值可以用给定的精度和小数位数表示,则允许将整数转换为具有给定精度和 小数位数的小数。如果输入值无效,则转换将引发错误。
有效示例
SELECT cast(1 as decimal(4, 2)); -- decimal '1.00'
SELECT cast(10 as decimal(4, 2)); -- decimal '10.00'
SELECT cast(123 as decimal(5, 2)); -- decimal '123.00'
Invalid examples
SELECT cast(123 as decimal(6, 4)); -- Out of range
SELECT cast(123 as decimal(4, 2)); -- Out of range
From floating-point types
如果输入值可以用指定的精度和小数位数表示,则允许将浮点数转换为具有指定精度和小数位数的小数。当给定的小数位数小于小数位数时, 浮点值将被四舍五入。根据双精度型和实数型提供的有效小数位数精度,转换精度最高为 15 位,最高为 6 位。从 NaN 或无穷 大值进行转换将抛出异常。
有效示例
SELECT cast(0.12 as decimal(4, 4)); -- decimal '0.1200'
SELECT cast(0.12 as decimal(4, 1)); -- decimal '0.1'
SELECT cast(0.19 as decimal(4, 1)); -- decimal '0.2'
SELECT cast(0.123456789123123 as decimal(38, 18)); -- decimal '0.123456789123123000'
SELECT cast(real '0.123456' as decimal(38, 18)); -- decimal '0.123456000000000000'
Invalid example
SELECT cast(123.12 as decimal(6, 4)); -- Out of range
SELECT cast(99999.99 as decimal(6, 2)); -- Out of range
From decimal
如果输入值可以用结果十进制类型表示,则允许将一个小数转换为另一个小数。当从较大的小数位转换为较小的小数位时,小数部分会被四舍五入。
有效示例
SELECT cast(decimal '0.69' as decimal(4, 3)); -- decimal '0.690'
SELECT cast(decimal '0.69' as decimal(4, 1)); -- decimal '0.7'
Invalid example
SELECT cast(decimal '-1000.000' as decimal(6, 4)); -- Out of range
SELECT cast(decimal '123456789' as decimal(9, 1)); -- Out of range
From varchar
如果输入值可以用指定的精度和小数位数表示,则允许将 varchar 转换为指定精度和小数位数的小数。当从较大的小数位数转换为较小的小数位数时, 小数部分会被四舍五入。从无效的输入值进行转换会引发错误。
有效示例
SELECT cast('9999999999.99' as decimal(12, 2)); -- decimal '9999999999.99'
SELECT cast('1.556' as decimal(12, 2)); -- decimal '1.56'
SELECT cast('1.554' as decimal(12, 2)); -- decimal '1.55'
SELECT cast('-1.554' as decimal(12, 2)); -- decimal '-1.55'
SELECT cast('+09' as decimal(12, 2)); -- decimal '9.00'
SELECT cast('9.' as decimal(12, 2)); -- decimal '9.00'
SELECT cast('.9' as decimal(12, 2)); -- decimal '0.90'
SELECT cast('3E+2' as decimal(12, 2)); -- decimal '300.00'
SELECT cast('3E+00002' as decimal(12, 2)); -- decimal '300.00'
SELECT cast('3e+2' as decimal(12, 2)); -- decimal '300.00'
SELECT cast('31.423e+2' as decimal(12, 2)); -- decimal '3142.30'
SELECT cast('1.2e-2' as decimal(12, 2)); -- decimal '0.01'
SELECT cast('1.2e-5' as decimal(12, 2)); -- decimal '0.00'
SELECT cast('0000.123' as decimal(12, 2)); -- decimal '0.12'
SELECT cast('.123000000' as decimal(12, 2)); -- decimal '0.12'
Invalid example
SELECT cast('1.23e67' as decimal(38, 0)); -- Value too large
SELECT cast('0.0446a' as decimal(9, 1)); -- Value is not a number
SELECT cast('' as decimal(9, 1)); -- Value is not a number
SELECT cast('23e-5d' as decimal(9, 1)); -- Value is not a number
SELECT cast('1.23 ' as decimal(38, 0)); -- Value is not a number
SELECT cast(' -3E+2' as decimal(12, 2)); -- Value is not a number
SELECT cast('-3E+2.1' as decimal(12, 2)); -- Value is not a number
SELECT cast('3E+' as decimal(12, 2)); -- Value is not a number
Cast to IPADDRESS
From VARCHAR
要将 varchar 转换为 IPAddress,输入字符串必须采用 IPV4 或 IPV6 格式。
对于 IPV4,其格式必须为: x.x.x.x,其中每个 x 为 0-255 之间的整数值。
对于 IPV6,其格式必须符合 RFC 4291#section-2.2 中定义的任意一种格式。
完整格式:
2001:0DB8:0000:0000:0008:0800:200C:417A
2001:DB8:0:0:8:800:200C:417A
Compressed form:
2001:DB8::8:800:200C:417A
Alternate form:
0:0:0:0:0:0:13.1.68.3
::13.1.68.3
在内部,该类型是纯 IPv6 地址。对 IPv4 的支持使用 IPv4 映射的 IPv6 地址范围 RFC 4291#section-2.5.5.2](https://datatracker.ietf.org/doc/html/rfc4291.html#section-2.5.5.2)。 创建 IPADDRESS 时,IPv4 地址将映射到该范围。
格式化 IPADDRESS 时,映射范围内的任何地址都将被格式化为 IPv4 地址。 其他地址将使用 RFC 5952 中定义的规范格式格式化为 IPv6 地址。
有效示例:
SELECT cast('2001:0db8:0000:0000:0000:ff00:0042:8329' as ipaddress); -- ipaddress '2001:db8::ff00:42:8329'
SELECT cast('1.2.3.4' as ipaddress); -- ipaddress '1.2.3.4'
SELECT cast('::ffff:ffff:ffff' as ipaddress); -- ipaddress '255.255.255.255'
Invalid examples:
``
SELECT cast('2001:db8::1::1' as ipaddress); -- Invalid IP address '2001:db8::1::1'
SELECT cast('789.1.1.1' as ipaddress); -- Invalid IP address '789.1.1.1'
From VARBINARY
要将 varbinary 转换为 IPAddress,它必须是网络字节顺序的 IPV4(4 字节)或 IPV6(16 字节)。 IPV4:
[01, 02, 03, 04] -> 1.2.3.4
IPV6:
[0x20, 0x01, 0x0d, 0xb8 0x00, 0x00, 0x00, 0x00 0x00 0x00, 0xff, 0x00, 0x00, 0x42, 0x83, 0x29] -> 2001:db8::ff00:42:8329
在内部,该类型是纯 IPv6 地址。对 IPv4 的支持使用 IPv4 映射的 IPv6 地址范围 RFC 4291 section-2.5.5.2 进行处理。 创建 IPADDRESS 时,IPv4 地址将映射到该范围。
格式化 IPADDRESS 时,映射范围内的任何地址都将被格式化为 IPv4 地址。 其他地址将使用 RFC 5952 中定义的规范格式格式化为 IPv6 地址。
IPV6 映射的 IPV4 地址:
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04] -> 1.2.3.4
Valid examples:
SELECT cast(from_hex('20010db8000000000000ff0000428329') as ipaddress); -- ipaddress '2001:db8::ff00:42:8329'
SELECT cast(from_hex('01020304') as ipaddress); -- ipaddress '1.2.3.4'
SELECT cast(from_hex('00000000000000000000ffff01020304') as ipaddress); -- ipaddress '1.2.3.4'
Invalid examples:
SELECT cast(from_hex('f000001100') as ipaddress); -- Invalid IP address binary length: 5
From IPPREFIX
返回子网范围内的规范(最低)IPADDRESS。
示例:
SELECT cast(ipprefix '1.2.3.4/24' as ipaddress) -- ipaddress '1.2.3.0'
SELECT cast(ipprefix '2001:db8::ff00:42:8329/64' as ipaddress) -- ipaddress '2001:db8::'
Cast to IPPREFIX
From VARCHAR
IPPREFIX 字符串必须采用 RFC 4291#section-2.3 中定义的 <ip_address>/<ip_prefix> 格式。
IPPREFIX 的 IPADDRESS 部分遵循与 IPADDRESS from VARCHAR <#ipaddress-from-varchar>_ 相同的转换规则。
如果 IP 是 IPv4 地址,则前缀部分必须小于等于 32 位;如果是 IPv6 地址,则前缀部分必须小于等于 128 位。 与 IPADDRESS 一样,任何采用 IPv4 映射 IPv6 地址形式的 IPv6 地址都将被解释为 IPv4 地址。只有规范(最小)的 IP 地址才会存储在 IPPREFIX 中。
示例:
有效示例:
SELECT cast('2001:0db8:0000:0000:0000:ff00:0042:8329/32' as ipprefix); -- ipprefix '2001:0db8::/32'
SELECT cast('1.2.3.4/24' as ipprefix); -- ipprefix '1.2.3.0/24'
SELECT cast('::ffff:ffff:ffff/16' as ipprefix); -- ipprefix '255.255.0.0/16'
Invalid examples:
SELECT cast('2001:db8::1::1/1' as ipprefix); -- Cannot cast value to IPPREFIX: 2001:db8::1::1/1
SELECT cast('2001:0db8:0000:0000:0000:ff00:0042:8329/129' as ipprefix); -- Cannot cast value to IPPREFIX: 2001:0db8:0000:0000:0000:ff00:0042:8329/129
SELECT cast('2001:0db8:0000:0000:0000:ff00:0042:8329/-1' as ipprefix); -- Cannot cast value to IPPREFIX: 2001:0db8:0000:0000:0000:ff00:0042:8329/-1
SELECT cast('255.2.3.4/33' as ipprefix); -- Cannot cast value to IPPREFIX: 255.2.3.4/33
SELECT cast('::ffff:ffff:ffff/33' as ipprefix); -- Cannot cast value to IPPREFIX: ::ffff:ffff:ffff/33
From IPADDRESS
返回一个 IPPREFIX 值,其前缀长度为整个 IP 地址的长度。 IPv4 的前缀长度为 32,IPv6 的前缀长度为 128。
示例:
SELECT cast(ipaddress '1.2.3.4' as ipprefix) -- ipprefix '1.2.3.4/32'
SELECT cast(ipaddress '2001:db8::ff00:42:8329' as ipprefix) -- ipprefix '2001:db8::ff00:42:8329/128'
Data Size Functions
parse_presto_data_size(string) -> decimal(38)
将格式为“value unit”的“string”解析为数字,其中“value”是单位值的分数:
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
Miscellaneous
typeof(x) -> varchar
返回 x 类型的名称
SELECT typeof(123); -- integer
SELECT typeof(1.5); -- double
SELECT typeof(array[1,2,3]); -- array(integer)