Literals

A literal (also known as a constant) represents a fixed data value. Spark SQL supports the following literals:

String Literal

A string literal is used to specify a character string value.

Syntax

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

Parameters

  • char

    One character from the character set. Use \ to escape special characters (e.g., ' or \). To represent unicode characters, use 16-bit or 32-bit unicode escape of the form \uxxxx or \Uxxxxxxxx, where xxxx and xxxxxxxx are 16-bit and 32-bit code points in hexadecimal respectively (e.g., \u3042 for and \U0001F44D for 👍).

  • r

    Case insensitive, indicates RAW. If a string literal starts with r prefix, neither special characters nor unicode characters are escaped by \.

Examples

  1. SELECT 'Hello, World!' AS col;
  2. +-------------+
  3. | col|
  4. +-------------+
  5. |Hello, World!|
  6. +-------------+
  7. SELECT "SPARK SQL" AS col;
  8. +---------+
  9. | col|
  10. +---------+
  11. |Spark SQL|
  12. +---------+
  13. SELECT 'it\'s $10.' AS col;
  14. +---------+
  15. | col|
  16. +---------+
  17. |It's $10.|
  18. +---------+
  19. SELECT r"'\n' represents newline character." AS col;
  20. +----------------------------------+
  21. | col|
  22. +----------------------------------+
  23. |'\n' represents newline character.|
  24. +----------------------------------+

Binary Literal

A binary literal is used to specify a byte sequence value.

Syntax

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

Parameters

  • num

    Any hexadecimal number from 0 to F.

Examples

  1. SELECT X'123456' AS col;
  2. +----------+
  3. | col|
  4. +----------+
  5. |[12 34 56]|
  6. +----------+

Null Literal

A null literal is used to specify a null value.

Syntax

  1. NULL

Examples

  1. SELECT NULL AS col;
  2. +----+
  3. | col|
  4. +----+
  5. |NULL|
  6. +----+

Boolean Literal

A boolean literal is used to specify a boolean value.

Syntax

  1. TRUE | FALSE

Examples

  1. SELECT TRUE AS col;
  2. +----+
  3. | col|
  4. +----+
  5. |true|
  6. +----+

Numeric Literal

A numeric literal is used to specify a fixed or floating-point number. There are two kinds of numeric literals: integral literal and fractional literal.

Integral Literal Syntax

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

Integral Literal Parameters

  • digit

    Any numeral from 0 to 9.

  • L

    Case insensitive, indicates BIGINT, which is an 8-byte signed integer number.

  • S

    Case insensitive, indicates SMALLINT, which is a 2-byte signed integer number.

  • Y

    Case insensitive, indicates TINYINT, which is a 1-byte signed integer number.

  • default (no postfix)

    Indicates a 4-byte signed integer number.

Integral Literal Examples

  1. SELECT -2147483648 AS col;
  2. +-----------+
  3. | col|
  4. +-----------+
  5. |-2147483648|
  6. +-----------+
  7. SELECT 9223372036854775807l AS col;
  8. +-------------------+
  9. | col|
  10. +-------------------+
  11. |9223372036854775807|
  12. +-------------------+
  13. SELECT -32Y AS col;
  14. +---+
  15. |col|
  16. +---+
  17. |-32|
  18. +---+
  19. SELECT 482S AS col;
  20. +---+
  21. |col|
  22. +---+
  23. |482|
  24. +---+

Fractional Literals Syntax

decimal literals:

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

double literals:

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

float literals:

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

While decimal_digits is defined as

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

and exponent is defined as

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

Fractional Literals Parameters

  • digit

    Any numeral from 0 to 9.

  • D

    Case insensitive, indicates DOUBLE, which is an 8-byte double-precision floating point number.

  • F

    Case insensitive, indicates FLOAT, which is a 4-byte single-precision floating point number.

  • BD

    Case insensitive, indicates DECIMAL, with the total number of digits as precision and the number of digits to right of decimal point as scale.

Fractional Literals Examples

  1. SELECT 12.578 AS col;
  2. +------+
  3. | col|
  4. +------+
  5. |12.578|
  6. +------+
  7. SELECT -0.1234567 AS col;
  8. +----------+
  9. | col|
  10. +----------+
  11. |-0.1234567|
  12. +----------+
  13. SELECT -.1234567 AS col;
  14. +----------+
  15. | col|
  16. +----------+
  17. |-0.1234567|
  18. +----------+
  19. SELECT 123. AS col;
  20. +---+
  21. |col|
  22. +---+
  23. |123|
  24. +---+
  25. SELECT 123.BD AS col;
  26. +---+
  27. |col|
  28. +---+
  29. |123|
  30. +---+
  31. SELECT 5E2 AS col;
  32. +-----+
  33. | col|
  34. +-----+
  35. |500.0|
  36. +-----+
  37. SELECT 5D AS col;
  38. +---+
  39. |col|
  40. +---+
  41. |5.0|
  42. +---+
  43. SELECT -5BD AS col;
  44. +---+
  45. |col|
  46. +---+
  47. | -5|
  48. +---+
  49. SELECT 12.578e-2d AS col;
  50. +-------+
  51. | col|
  52. +-------+
  53. |0.12578|
  54. +-------+
  55. SELECT -.1234567E+2BD AS col;
  56. +---------+
  57. | col|
  58. +---------+
  59. |-12.34567|
  60. +---------+
  61. SELECT +3.e+3 AS col;
  62. +------+
  63. | col|
  64. +------+
  65. |3000.0|
  66. +------+
  67. SELECT -3.E-3D AS col;
  68. +------+
  69. | col|
  70. +------+
  71. |-0.003|
  72. +------+

Datetime Literal

A datetime literal is used to specify a date or timestamp value.

Date Syntax

  1. DATE { 'yyyy' |
  2. 'yyyy-[m]m' |
  3. 'yyyy-[m]m-[d]d' |
  4. 'yyyy-[m]m-[d]d[T]' }

Note: defaults to 01 if month or day is not specified.

Date Examples

  1. SELECT DATE '1997' AS col;
  2. +----------+
  3. | col|
  4. +----------+
  5. |1997-01-01|
  6. +----------+
  7. SELECT DATE '1997-01' AS col;
  8. +----------+
  9. | col|
  10. +----------+
  11. |1997-01-01|
  12. +----------+
  13. SELECT DATE '2011-11-11' AS col;
  14. +----------+
  15. | col|
  16. +----------+
  17. |2011-11-11|
  18. +----------+

Timestamp Syntax

  1. TIMESTAMP { 'yyyy' |
  2. 'yyyy-[m]m' |
  3. 'yyyy-[m]m-[d]d' |
  4. 'yyyy-[m]m-[d]d ' |
  5. 'yyyy-[m]m-[d]d[T][h]h[:]' |
  6. 'yyyy-[m]m-[d]d[T][h]h:[m]m[:]' |
  7. 'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s[.]' |
  8. 'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s.[ms][ms][ms][us][us][us][zone_id]'}

Note: defaults to 00 if hour, minute or second is not specified. zone_id should have one of the forms:

  • Z - Zulu time zone UTC+0
  • +|-[h]h:[m]m
  • An id with one of the prefixes UTC+, UTC-, GMT+, GMT-, UT+ or UT-, and a suffix in the formats:
    • +|-h[h]
    • +|-hh[:]mm
    • +|-hh:mm:ss
    • +|-hhmmss
  • Region-based zone IDs in the form area/city, such as Europe/Paris

Note: defaults to the session local timezone (set via spark.sql.session.timeZone) if zone_id is not specified.

Timestamp Examples

  1. SELECT TIMESTAMP '1997-01-31 09:26:56.123' AS col;
  2. +-----------------------+
  3. | col|
  4. +-----------------------+
  5. |1997-01-31 09:26:56.123|
  6. +-----------------------+
  7. SELECT TIMESTAMP '1997-01-31 09:26:56.66666666UTC+08:00' AS col;
  8. +--------------------------+
  9. | col |
  10. +--------------------------+
  11. |1997-01-30 17:26:56.666666|
  12. +--------------------------+
  13. SELECT TIMESTAMP '1997-01' AS col;
  14. +-------------------+
  15. | col|
  16. +-------------------+
  17. |1997-01-01 00:00:00|
  18. +-------------------+

Interval Literal

An interval literal is used to specify a fixed period of time. The interval literal supports two syntaxes: ANSI syntax and multi-units syntax.

ANSI Syntax

The ANSI SQL standard defines interval literals in the form:

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

where <interval qualifier> can be a single field or in the field-to-field form:

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

The field name is case-insensitive, and can be one of YEAR, MONTH, DAY, HOUR, MINUTE and SECOND.

An interval literal can have either year-month or day-time interval type. The interval sub-type defines format of <interval string>:

  1. <interval string> ::= <quote> [ <sign> ] { <year-month literal> | <day-time literal> } <quote>
  2. <year-month literal> ::= <years value> [ <minus sign> <months value> ] | <months value>
  3. <day-time literal> ::= <day-time interval> | <time interval>
  4. <day-time interval> ::= <days value> [ <space> <hours value> [ <colon> <minutes value> [ <colon> <seconds value> ] ] ]
  5. <time interval> ::= <hours value> [ <colon> <minutes value> [ <colon> <seconds value> ] ]
  6. | <minutes value> [ <colon> <seconds value> ]
  7. | <seconds value>

Supported year-month interval literals and theirs formats:

<interval qualifier>Interval string patternAn instance of the literal
YEAR[+|-]’[+|-]y’INTERVAL -‘2021’ YEAR
YEAR TO MONTH[+|-]’[+|-]y-m’INTERVAL ‘-2021-07’ YEAR TO MONTH
MONTH[+|-]’[+|-]m’interval ‘10’ month

Formats of supported day-time interval literals:

<interval qualifier>Interval string patternAn instance of the literal
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 Examples

  1. SELECT INTERVAL '2-3' YEAR TO MONTH AS col;
  2. +----------------------------+
  3. |col |
  4. +----------------------------+
  5. |INTERVAL '2-3' YEAR TO MONTH|
  6. +----------------------------+
  7. SELECT INTERVAL -'20 15:40:32.99899999' DAY TO SECOND AS col;
  8. +--------------------------------------------+
  9. |col |
  10. +--------------------------------------------+
  11. |INTERVAL '-20 15:40:32.998999' DAY TO SECOND|
  12. +--------------------------------------------+

Multi-units Syntax

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

Multi-units Parameters

  • interval_value

    Syntax:

    1. [ + | - ] number_value | '[ + | - ] number_value'
  • interval_unit

    Syntax:

    1. YEAR[S] | MONTH[S] | WEEK[S] | DAY[S] | HOUR[S] | MINUTE[S] | SECOND[S] |
    2. MILLISECOND[S] | MICROSECOND[S]
    3. Mix of the YEAR[S] or MONTH[S] interval units with other units is not allowed.

Multi-units Examples

  1. SELECT INTERVAL 3 YEAR AS col;
  2. +-------+
  3. | col|
  4. +-------+
  5. |3 years|
  6. +-------+
  7. SELECT INTERVAL -2 HOUR '3' MINUTE AS col;
  8. +--------------------+
  9. | col|
  10. +--------------------+
  11. |-1 hours -57 minutes|
  12. +--------------------+
  13. SELECT INTERVAL '1 YEAR 2 DAYS 3 HOURS';
  14. +----------------------+
  15. | col|
  16. +----------------------+
  17. |1 years 2 days 3 hours|
  18. +----------------------+
  19. SELECT INTERVAL 1 YEARS 2 MONTH 3 WEEK 4 DAYS 5 HOUR 6 MINUTES 7 SECOND 8
  20. MILLISECOND 9 MICROSECONDS AS col;
  21. +-----------------------------------------------------------+
  22. | col|
  23. +-----------------------------------------------------------+
  24. |1 years 2 months 25 days 5 hours 6 minutes 7.008009 seconds|
  25. +-----------------------------------------------------------+