Back to home page

OSCL-LXR

 
 

    


0001 ---
0002 layout: global
0003 title: Literals
0004 displayTitle: Literals
0005 license: |
0006   Licensed to the Apache Software Foundation (ASF) under one or more
0007   contributor license agreements.  See the NOTICE file distributed with
0008   this work for additional information regarding copyright ownership.
0009   The ASF licenses this file to You under the Apache License, Version 2.0
0010   (the "License"); you may not use this file except in compliance with
0011   the License.  You may obtain a copy of the License at
0012  
0013      http://www.apache.org/licenses/LICENSE-2.0
0014  
0015   Unless required by applicable law or agreed to in writing, software
0016   distributed under the License is distributed on an "AS IS" BASIS,
0017   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018   See the License for the specific language governing permissions and
0019   limitations under the License.
0020 ---
0021 
0022 A literal (also known as a constant) represents a fixed data value. Spark SQL supports the following literals:
0023 
0024  * [String Literal](#string-literal)
0025  * [Binary Literal](#binary-literal)
0026  * [Null Literal](#null-literal)
0027  * [Boolean Literal](#boolean-literal)
0028  * [Numeric Literal](#numeric-literal)
0029  * [Datetime Literal](#datetime-literal)
0030  * [Interval Literal](#interval-literal)
0031 
0032 ### String Literal
0033 
0034 A string literal is used to specify a character string value.
0035 
0036 #### Syntax
0037 
0038 ```sql
0039 'char [ ... ]' | "char [ ... ]"
0040 ```
0041 
0042 #### Parameters
0043 
0044 * **char**
0045 
0046     One character from the character set. Use `\` to escape special characters (e.g., `'` or `\`).
0047 
0048 #### Examples
0049 
0050 ```sql
0051 SELECT 'Hello, World!' AS col;
0052 +-------------+
0053 |          col|
0054 +-------------+
0055 |Hello, World!|
0056 +-------------+
0057 
0058 SELECT "SPARK SQL" AS col;
0059 +---------+
0060 |      col|
0061 +---------+
0062 |Spark SQL|
0063 +---------+
0064 
0065 SELECT 'it\'s $10.' AS col;
0066 +---------+
0067 |      col|
0068 +---------+
0069 |It's $10.|
0070 +---------+
0071 ```
0072 
0073 ### Binary Literal
0074 
0075 A binary literal is used to specify a byte sequence value.
0076 
0077 #### Syntax
0078 
0079 ```sql
0080 X { 'num [ ... ]' | "num [ ... ]" }
0081 ```
0082 
0083 #### Parameters
0084 
0085 * **num**
0086 
0087     Any hexadecimal number from 0 to F.
0088 
0089 #### Examples
0090 
0091 ```sql
0092 SELECT X'123456' AS col;
0093 +----------+
0094 |       col|
0095 +----------+
0096 |[12 34 56]|
0097 +----------+
0098 ```
0099 
0100 ### Null Literal
0101 
0102 A null literal is used to specify a null value.
0103 
0104 #### Syntax
0105 
0106 ```sql
0107 NULL
0108 ```
0109 
0110 #### Examples
0111 
0112 ```sql
0113 SELECT NULL AS col;
0114 +----+
0115 | col|
0116 +----+
0117 |NULL|
0118 +----+
0119 ```
0120 
0121 ### Boolean Literal
0122 
0123 A boolean literal is used to specify a boolean value.
0124 
0125 #### Syntax
0126 
0127 ```sql
0128 TRUE | FALSE
0129 ```
0130 
0131 #### Examples
0132 
0133 ```sql
0134 SELECT TRUE AS col;
0135 +----+
0136 | col|
0137 +----+
0138 |true|
0139 +----+
0140 ```
0141 
0142 ### Numeric Literal
0143 
0144 A numeric literal is used to specify a fixed or floating-point number.
0145 
0146 #### Integral Literal
0147 
0148 ##### Syntax
0149 
0150 ```sql
0151 [ + | - ] digit [ ... ] [ L | S | Y ]
0152 ```
0153 
0154 ##### Parameters
0155 
0156 * **digit**
0157 
0158     Any numeral from 0 to 9.
0159 
0160 * **L**
0161 
0162     Case insensitive, indicates `BIGINT`, which is an 8-byte signed integer number.
0163 
0164 * **S**
0165 
0166     Case insensitive, indicates `SMALLINT`, which is a 2-byte signed integer number.
0167 
0168 * **Y**
0169 
0170     Case insensitive, indicates `TINYINT`, which is a 1-byte signed integer number.
0171 
0172 * **default (no postfix)**
0173 
0174     Indicates a 4-byte signed integer number.
0175 
0176 ##### Examples
0177 
0178 ```sql
0179 SELECT -2147483648 AS col;
0180 +-----------+
0181 |        col|
0182 +-----------+
0183 |-2147483648|
0184 +-----------+
0185 
0186 SELECT 9223372036854775807l AS col;
0187 +-------------------+
0188 |                col|
0189 +-------------------+
0190 |9223372036854775807|
0191 +-------------------+
0192 
0193 SELECT -32Y AS col;
0194 +---+
0195 |col|
0196 +---+
0197 |-32|
0198 +---+
0199 
0200 SELECT 482S AS col;
0201 +---+
0202 |col|
0203 +---+
0204 |482|
0205 +---+
0206 ```
0207 
0208 #### Fractional Literals
0209 
0210 ##### Syntax
0211 
0212 decimal literals:
0213 ```sql
0214 decimal_digits { [ BD ] | [ exponent BD ] } | digit [ ... ] [ exponent ] BD
0215 ```
0216 
0217 double literals:
0218 ```sql
0219 decimal_digits  { D | exponent [ D ] }  | digit [ ... ] { exponent [ D ] | [ exponent ] D }
0220 ```
0221 
0222 While decimal_digits is defined as
0223 ```sql
0224 [ + | - ] { digit [ ... ] . [ digit [ ... ] ] | . digit [ ... ] }
0225 ```
0226 
0227 and exponent is defined as
0228 ```sql
0229 E [ + | - ] digit [ ... ]
0230 ```
0231 
0232 ##### Parameters
0233 
0234 * **digit**
0235 
0236     Any numeral from 0 to 9.
0237 
0238 * **D**
0239 
0240     Case insensitive, indicates `DOUBLE`, which is an 8-byte double-precision floating point number.
0241 
0242 * **BD**
0243 
0244     Case insensitive, indicates `DECIMAL`, with the total number of digits as precision and the number of digits to right of decimal point as scale.
0245 
0246 ##### Examples
0247 
0248 ```sql
0249 SELECT 12.578 AS col;
0250 +------+
0251 |   col|
0252 +------+
0253 |12.578|
0254 +------+
0255 
0256 SELECT -0.1234567 AS col;
0257 +----------+
0258 |       col|
0259 +----------+
0260 |-0.1234567|
0261 +----------+
0262 
0263 SELECT -.1234567 AS col;
0264 +----------+
0265 |       col|
0266 +----------+
0267 |-0.1234567|
0268 +----------+
0269 
0270 SELECT 123. AS col;
0271 +---+
0272 |col|
0273 +---+
0274 |123|
0275 +---+
0276 
0277 SELECT 123.BD AS col;
0278 +---+
0279 |col|
0280 +---+
0281 |123|
0282 +---+
0283 
0284 SELECT 5E2 AS col;
0285 +-----+
0286 |  col|
0287 +-----+
0288 |500.0|
0289 +-----+
0290 
0291 SELECT 5D AS col;
0292 +---+
0293 |col|
0294 +---+
0295 |5.0|
0296 +---+
0297 
0298 SELECT -5BD AS col;
0299 +---+
0300 |col|
0301 +---+
0302 | -5|
0303 +---+
0304 
0305 SELECT 12.578e-2d AS col;
0306 +-------+
0307 |    col|
0308 +-------+
0309 |0.12578|
0310 +-------+
0311 
0312 SELECT -.1234567E+2BD AS col;
0313 +---------+
0314 |      col|
0315 +---------+
0316 |-12.34567|
0317 +---------+
0318 
0319 SELECT +3.e+3 AS col;
0320 +------+
0321 |   col|
0322 +------+
0323 |3000.0|
0324 +------+
0325 
0326 SELECT -3.E-3D AS col;
0327 +------+
0328 |   col|
0329 +------+
0330 |-0.003|
0331 +------+
0332 ```
0333 
0334 ### Datetime Literal
0335 
0336 A Datetime literal is used to specify a datetime value.
0337 
0338 #### Date Literal
0339 
0340 ##### Syntax
0341 
0342 ```sql
0343 DATE { 'yyyy' |
0344        'yyyy-[m]m' |
0345        'yyyy-[m]m-[d]d' |
0346        'yyyy-[m]m-[d]d[T]' }
0347 ```
0348 **Note:** defaults to `01` if month or day is not specified.
0349 
0350 ##### Examples
0351 
0352 ```sql
0353 SELECT DATE '1997' AS col;
0354 +----------+
0355 |       col|
0356 +----------+
0357 |1997-01-01|
0358 +----------+
0359 
0360 SELECT DATE '1997-01' AS col;
0361 +----------+
0362 |       col|
0363 +----------+
0364 |1997-01-01|
0365 +----------+
0366 
0367 SELECT DATE '2011-11-11' AS col;
0368 +----------+
0369 |       col|
0370 +----------+
0371 |2011-11-11|
0372 +----------+
0373 ```
0374 
0375 #### Timestamp Literal
0376 
0377 ##### Syntax
0378 
0379 ```sql
0380 TIMESTAMP { 'yyyy' |
0381             'yyyy-[m]m' |
0382             'yyyy-[m]m-[d]d' |
0383             'yyyy-[m]m-[d]d ' |
0384             'yyyy-[m]m-[d]d[T][h]h[:]' |
0385             'yyyy-[m]m-[d]d[T][h]h:[m]m[:]' |
0386             'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s[.]' |
0387             'yyyy-[m]m-[d]d[T][h]h:[m]m:[s]s.[ms][ms][ms][us][us][us][zone_id]'}
0388 ```
0389 **Note:** defaults to `00` if hour, minute or second is not specified.
0390 `zone_id` should have one of the forms:
0391 * Z - Zulu time zone UTC+0
0392 * `+|-[h]h:[m]m`
0393 * An id with one of the prefixes UTC+, UTC-, GMT+, GMT-, UT+ or UT-, and a suffix in the formats:
0394   * `+|-h[h]`
0395   * `+|-hh[:]mm`
0396   * `+|-hh:mm:ss`
0397   * `+|-hhmmss`
0398 * Region-based zone IDs in the form `area/city`, such as `Europe/Paris`
0399 
0400 **Note:** defaults to the session local timezone (set via `spark.sql.session.timeZone`) if `zone_id` is not specified.
0401 
0402 ##### Examples
0403 
0404 ```sql
0405 SELECT TIMESTAMP '1997-01-31 09:26:56.123' AS col;
0406 +-----------------------+
0407 |                    col|
0408 +-----------------------+
0409 |1997-01-31 09:26:56.123|
0410 +-----------------------+
0411 
0412 SELECT TIMESTAMP '1997-01-31 09:26:56.66666666UTC+08:00' AS col;
0413 +--------------------------+
0414 |                      col |
0415 +--------------------------+
0416 |1997-01-30 17:26:56.666666|
0417 +--------------------------+
0418 
0419 SELECT TIMESTAMP '1997-01' AS col;
0420 +-------------------+
0421 |                col|
0422 +-------------------+
0423 |1997-01-01 00:00:00|
0424 +-------------------+
0425 ```
0426 
0427 ### Interval Literal
0428 
0429 An interval literal is used to specify a fixed period of time.
0430 
0431 ```sql
0432 INTERVAL interval_value interval_unit [ interval_value interval_unit ... ] |
0433 INTERVAL 'interval_value interval_unit [ interval_value interval_unit ... ]' |
0434 INTERVAL interval_string_value interval_unit TO interval_unit
0435 ```
0436 
0437 #### Parameters
0438 
0439 * **interval_value**
0440 
0441     **Syntax:**
0442 
0443       [ + | - ] number_value | '[ + | - ] number_value'
0444 
0445 * **interval_string_value**
0446 
0447      year-month/day-time interval string.
0448 
0449 * **interval_unit**
0450 
0451     **Syntax:**
0452 
0453       YEAR[S] | MONTH[S] | WEEK[S] | DAY[S] | HOUR[S] | MINUTE[S] | SECOND[S] |
0454       MILLISECOND[S] | MICROSECOND[S]
0455 
0456 #### Examples
0457 
0458 ```sql
0459 SELECT INTERVAL 3 YEAR AS col;
0460 +-------+
0461 |    col|
0462 +-------+
0463 |3 years|
0464 +-------+
0465 
0466 SELECT INTERVAL -2 HOUR '3' MINUTE AS col;
0467 +--------------------+
0468 |                 col|
0469 +--------------------+
0470 |-1 hours -57 minutes|
0471 +--------------------+
0472 
0473 SELECT INTERVAL '1 YEAR 2 DAYS 3 HOURS';
0474 +----------------------+
0475 |                   col|
0476 +----------------------+
0477 |1 years 2 days 3 hours|
0478 +----------------------+
0479 
0480 SELECT INTERVAL 1 YEARS 2 MONTH 3 WEEK 4 DAYS 5 HOUR 6 MINUTES 7 SECOND 8
0481     MILLISECOND 9 MICROSECONDS AS col;
0482 +-----------------------------------------------------------+
0483 |                                                        col|
0484 +-----------------------------------------------------------+
0485 |1 years 2 months 25 days 5 hours 6 minutes 7.008009 seconds|
0486 +-----------------------------------------------------------+
0487 
0488 SELECT INTERVAL '2-3' YEAR TO MONTH AS col;
0489 +----------------+
0490 |             col|
0491 +----------------+
0492 |2 years 3 months|
0493 +----------------+
0494 
0495 SELECT INTERVAL '20 15:40:32.99899999' DAY TO SECOND AS col;
0496 +---------------------------------------------+
0497 |                                          col|
0498 +---------------------------------------------+
0499 |20 days 15 hours 40 minutes 32.998999 seconds|
0500 +---------------------------------------------+
0501 ```