字面量

字面量(也称为常量)表示一个固定的数据值。Spark SQL 支持以下字面量

字符串字面量

字符串字面量用于指定一个字符字符串值。

语法

[ r ] { 'char [ ... ]' | "char [ ... ]" }

参数

示例

SELECT 'Hello, World!' AS col;
+-------------+
|          col|
+-------------+
|Hello, World!|
+-------------+

SELECT "SPARK SQL" AS col;
+---------+
|      col|
+---------+
|Spark SQL|
+---------+

SELECT 'it\'s $10.' AS col;
+---------+
|      col|
+---------+
|It's $10.|
+---------+

SELECT r"'\n' represents newline character." AS col;
+----------------------------------+
|                               col|
+----------------------------------+
|'\n' represents newline character.|
+----------------------------------+

二进制字面量

二进制字面量用于指定一个字节序列值。

语法

X { 'num [ ... ]' | "num [ ... ]" }

参数

示例

SELECT X'123456' AS col;
+----------+
|       col|
+----------+
|[12 34 56]|
+----------+

空值字面量

空值字面量用于指定一个空值。

语法

NULL

示例

SELECT NULL AS col;
+----+
| col|
+----+
|NULL|
+----+

布尔字面量

布尔字面量用于指定一个布尔值。

语法

TRUE | FALSE

示例

SELECT TRUE AS col;
+----+
| col|
+----+
|true|
+----+

数值字面量

数值字面量用于指定一个固定或浮点数。数值字面量有两种:整数字面量和分数字面量。

整数字面量语法

[ + | - ] digit [ ... ] [ L | S | Y ]

整数字面量参数

整数字面量示例

SELECT -2147483648 AS col;
+-----------+
|        col|
+-----------+
|-2147483648|
+-----------+

SELECT 9223372036854775807l AS col;
+-------------------+
|                col|
+-------------------+
|9223372036854775807|
+-------------------+

SELECT -32Y AS col;
+---+
|col|
+---+
|-32|
+---+

SELECT 482S AS col;
+---+
|col|
+---+
|482|
+---+

分数字面量语法

十进制字面量

decimal_digits { [ BD ] | [ exponent BD ] } | digit [ ... ] [ exponent ] BD

双精度字面量

decimal_digits  { D | exponent [ D ] }  | digit [ ... ] { exponent [ D ] | [ exponent ] D }

浮点字面量

decimal_digits  { F | exponent [ F ] }  | digit [ ... ] { exponent [ F ] | [ exponent ] F }

而 decimal_digits 定义为

[ + | - ] { digit [ ... ] . [ digit [ ... ] ] | . digit [ ... ] }

而 exponent 定义为

E [ + | - ] digit [ ... ]

分数字面量参数

分数字面量示例

SELECT 12.578 AS col;
+------+
|   col|
+------+
|12.578|
+------+

SELECT -0.1234567 AS col;
+----------+
|       col|
+----------+
|-0.1234567|
+----------+

SELECT -.1234567 AS col;
+----------+
|       col|
+----------+
|-0.1234567|
+----------+

SELECT 123. AS col;
+---+
|col|
+---+
|123|
+---+

SELECT 123.BD AS col;
+---+
|col|
+---+
|123|
+---+

SELECT 5E2 AS col;
+-----+
|  col|
+-----+
|500.0|
+-----+

SELECT 5D AS col;
+---+
|col|
+---+
|5.0|
+---+

SELECT -5BD AS col;
+---+
|col|
+---+
| -5|
+---+

SELECT 12.578e-2d AS col;
+-------+
|    col|
+-------+
|0.12578|
+-------+

SELECT -.1234567E+2BD AS col;
+---------+
|      col|
+---------+
|-12.34567|
+---------+

SELECT +3.e+3 AS col;
+------+
|   col|
+------+
|3000.0|
+------+

SELECT -3.E-3D AS col;
+------+
|   col|
+------+
|-0.003|
+------+

日期时间字面量

日期时间字面量用于指定一个日期或时间戳值。

日期语法

DATE { 'yyyy' |
       'yyyy-[m]m' |
       'yyyy-[m]m-[d]d' |
       'yyyy-[m]m-[d]d[T]' }

注意:如果未指定月份或日期,则默认为 01

日期示例

SELECT DATE '1997' AS col;
+----------+
|       col|
+----------+
|1997-01-01|
+----------+

SELECT DATE '1997-01' AS col;
+----------+
|       col|
+----------+
|1997-01-01|
+----------+

SELECT DATE '2011-11-11' AS col;
+----------+
|       col|
+----------+
|2011-11-11|
+----------+

时间戳语法

TIMESTAMP { 'yyyy' |
            'yyyy-[m]m' |
            'yyyy-[m]m-[d]d' |
            'yyyy-[m]m-[d]d ' |
            'yyyy-[m]m-[d]d[T][h]h[:]' |
            'yyyy-[m]m-[d]d[T][h]h:[m]m[:]' |
            'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s[.]' |
            'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s.[ms][ms][ms][us][us][us][zone_id]'}

注意:如果未指定小时、分钟或秒,则默认为 00zone_id 应采用以下格式之一

注意:如果未指定 zone_id,则默认为会话本地时区(通过 spark.sql.session.timeZone 设置)。

时间戳示例

SELECT TIMESTAMP '1997-01-31 09:26:56.123' AS col;
+-----------------------+
|                    col|
+-----------------------+
|1997-01-31 09:26:56.123|
+-----------------------+

SELECT TIMESTAMP '1997-01-31 09:26:56.66666666UTC+08:00' AS col;
+--------------------------+
|                      col |
+--------------------------+
|1997-01-30 17:26:56.666666|
+--------------------------+

SELECT TIMESTAMP '1997-01' AS col;
+-------------------+
|                col|
+-------------------+
|1997-01-01 00:00:00|
+-------------------+

间隔字面量

间隔字面量用于指定一个固定的时间段。间隔字面量支持两种语法:ANSI 语法和多单位语法。

ANSI 语法

ANSI SQL 标准将间隔字面量定义为以下形式

INTERVAL [ <sign> ] <interval string> <interval qualifier>

其中 <interval qualifier> 可以是单个字段或字段到字段形式

<interval qualifier> ::= <start field> TO <end field> | <single field>

字段名称不区分大小写,可以是 YEARMONTHDAYHOURMINUTESECOND 之一。

间隔字面量可以是年-月或日-时间隔类型。间隔子类型定义了 <interval string> 的格式

<interval string> ::= <quote> [ <sign> ] { <year-month literal> | <day-time literal> } <quote>
<year-month literal> ::= <years value> [ <minus sign> <months value> ] | <months value>
<day-time literal> ::= <day-time interval> | <time interval>
<day-time interval> ::= <days value> [ <space> <hours value> [ <colon> <minutes value> [ <colon> <seconds value> ] ] ]
<time interval> ::= <hours value> [ <colon> <minutes value> [ <colon> <seconds value> ] ]
  | <minutes value> [ <colon> <seconds value> ]
  | <seconds value>

支持的年-月间隔字面量及其格式

<interval qualifier> 间隔字符串模式 字面量的实例
YEAR [+|-]'[+|-]y' INTERVAL -'2021' YEAR
YEAR TO MONTH [+|-]'[+|-]y-m' INTERVAL '-2021-07' YEAR TO MONTH
MONTH [+|-]'[+|-]m' interval '10' month

支持的日-时间隔字面量的格式

<interval qualifier> 间隔字符串模式 字面量的实例
DAY [+|-]'[+|-]d' INTERVAL -'100' DAY
DAY TO HOUR [+|-]'[+|-]d h' INTERVAL '-100 10' DAY TO HOUR
DAY TO MINUTE [+|-]'[+|-]d h:m' INTERVAL '100 10:30' DAY TO MINUTE
DAY TO SECOND [+|-]'[+|-]d h:m:s.n' INTERVAL '100 10:30:40.999999' DAY TO SECOND
HOUR [+|-]'[+|-]h' INTERVAL '123' HOUR
HOUR TO MINUTE [+|-]'[+|-]h:m' INTERVAL -'-123:10' HOUR TO MINUTE
HOUR TO SECOND [+|-]'[+|-]h:m:s.n' INTERVAL '123:10:59' HOUR TO SECOND
MINUTE [+|-]'[+|-]m' interval '1000' minute
MINUTE TO SECOND [+|-]'[+|-]m:s.n' INTERVAL '1000:01.001' MINUTE TO SECOND
SECOND [+|-]'[+|-]s.n' INTERVAL '1000.000001' SECOND

ANSI 示例

SELECT INTERVAL '2-3' YEAR TO MONTH AS col;
+----------------------------+
|col                         |
+----------------------------+
|INTERVAL '2-3' YEAR TO MONTH|
+----------------------------+

SELECT INTERVAL -'20 15:40:32.99899999' DAY TO SECOND AS col;
+--------------------------------------------+
|col                                         |
+--------------------------------------------+
|INTERVAL '-20 15:40:32.998999' DAY TO SECOND|
+--------------------------------------------+

多单位语法

INTERVAL interval_value interval_unit [ interval_value interval_unit ... ] |
INTERVAL 'interval_value interval_unit [ interval_value interval_unit ... ]' |

多单位参数

多单位示例

SELECT INTERVAL 3 YEAR AS col;
+-------+
|    col|
+-------+
|3 years|
+-------+

SELECT INTERVAL -2 HOUR '3' MINUTE AS col;
+--------------------+
|                 col|
+--------------------+
|-1 hours -57 minutes|
+--------------------+

SELECT INTERVAL '1 YEAR 2 DAYS 3 HOURS';
+----------------------+
|                   col|
+----------------------+
|1 years 2 days 3 hours|
+----------------------+

SELECT INTERVAL 1 YEARS 2 MONTH 3 WEEK 4 DAYS 5 HOUR 6 MINUTES 7 SECOND 8
    MILLISECOND 9 MICROSECONDS AS col;
+-----------------------------------------------------------+
|                                                        col|
+-----------------------------------------------------------+
|1 years 2 months 25 days 5 hours 6 minutes 7.008009 seconds|
+-----------------------------------------------------------+