Built-in Functions

    Examples

    Window Functions

    FunctionDescription
    cume_dist()Computes the position of a value relative to all values in the partition.
    dense_rank()Computes the rank of a value in a group of values. The result is one plus the previously assigned rank value. Unlike the function rank, dense_rank will not produce gaps in the ranking sequence.
    lag(input[, offset[, default]])Returns the value of input at the offsetth row before the current row in the window. The default value of offset is 1 and the default value of default is null. If the value of input at the offsetth row is null, null is returned. If there is no such offset row (e.g., when the offset is 1, the first row of the window does not have any previous row), default is returned.
    lead(input[, offset[, default]])Returns the value of input at the offsetth row after the current row in the window. The default value of offset is 1 and the default value of default is null. If the value of input at the offsetth row is null, null is returned. If there is no such an offset row (e.g., when the offset is 1, the last row of the window does not have any subsequent row), default is returned.
    nth_value(input[, offset])Returns the value of input at the row that is the offsetth row from beginning of the window frame. Offset starts at 1. If ignoreNulls=true, we will skip nulls when finding the offsetth row. Otherwise, every row counts for the offset. If there is no such an offsetth row (e.g., when the offset is 10, size of the window frame is less than 10), null is returned.
    ntile(n)Divides the rows for each window partition into n buckets ranging from 1 to at most n.
    percent_rank()Computes the percentage ranking of a value in a group of values.
    rank()Computes the rank of a value in a group of values. The result is one plus the number of rows preceding or equal to the current row in the ordering of the partition. The values will produce gaps in the sequence.
    row_number()Assigns a unique, sequential number to each row, starting with one, according to the ordering of rows within the window partition.

    Examples

    1. -- cume_dist
    2. SELECT a, b, cume_dist() OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    3. +---+---+--------------------------------------------------------------------------------------------------------------+
    4. | a| b|cume_dist() OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    5. +---+---+--------------------------------------------------------------------------------------------------------------+
    6. | A1| 1| 0.6666666666666666|
    7. | A1| 1| 0.6666666666666666|
    8. | A1| 2| 1.0|
    9. | A2| 3| 1.0|
    10. +---+---+--------------------------------------------------------------------------------------------------------------+
    11. -- dense_rank
    12. SELECT a, b, dense_rank(b) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    13. +---+---+--------------------------------------------------------------------------------------------------------------+
    14. | a| b|DENSE_RANK() OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    15. +---+---+--------------------------------------------------------------------------------------------------------------+
    16. | A1| 1| 1|
    17. | A1| 1| 1|
    18. | A1| 2| 2|
    19. | A2| 3| 1|
    20. +---+---+--------------------------------------------------------------------------------------------------------------+
    21. -- lag
    22. SELECT a, b, lag(b) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    23. +---+---+-----------------------------------------------------------------------------------------------------------+
    24. | a| b|lag(b, 1, NULL) OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN -1 FOLLOWING AND -1 FOLLOWING)|
    25. +---+---+-----------------------------------------------------------------------------------------------------------+
    26. | A1| 1| null|
    27. | A1| 1| 1|
    28. | A1| 2| 1|
    29. | A2| 3| null|
    30. +---+---+-----------------------------------------------------------------------------------------------------------+
    31. -- lead
    32. SELECT a, b, lead(b) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    33. +---+---+----------------------------------------------------------------------------------------------------------+
    34. | a| b|lead(b, 1, NULL) OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)|
    35. +---+---+----------------------------------------------------------------------------------------------------------+
    36. | A1| 1| 1|
    37. | A1| 1| 2|
    38. | A1| 2| null|
    39. | A2| 3| null|
    40. +---+---+----------------------------------------------------------------------------------------------------------+
    41. -- nth_value
    42. SELECT a, b, nth_value(b, 2) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    43. +---+---+------------------------------------------------------------------------------------------------------------------+
    44. | a| b|nth_value(b, 2) OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    45. +---+---+------------------------------------------------------------------------------------------------------------------+
    46. | A1| 1| 1|
    47. | A1| 1| 1|
    48. | A1| 2| 1|
    49. | A2| 3| null|
    50. +---+---+------------------------------------------------------------------------------------------------------------------+
    51. -- ntile
    52. SELECT a, b, ntile(2) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    53. +---+---+----------------------------------------------------------------------------------------------------------+
    54. | a| b|ntile(2) OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    55. +---+---+----------------------------------------------------------------------------------------------------------+
    56. | A1| 1| 1|
    57. | A1| 1| 1|
    58. | A1| 2| 2|
    59. | A2| 3| 1|
    60. +---+---+----------------------------------------------------------------------------------------------------------+
    61. -- percent_rank
    62. SELECT a, b, percent_rank(b) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    63. +---+---+----------------------------------------------------------------------------------------------------------------+
    64. | a| b|PERCENT_RANK() OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    65. +---+---+----------------------------------------------------------------------------------------------------------------+
    66. | A1| 1| 0.0|
    67. | A1| 1| 0.0|
    68. | A1| 2| 1.0|
    69. | A2| 3| 0.0|
    70. +---+---+----------------------------------------------------------------------------------------------------------------+
    71. -- rank
    72. SELECT a, b, rank(b) OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    73. +---+---+--------------------------------------------------------------------------------------------------------+
    74. | a| b|RANK() OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    75. +---+---+--------------------------------------------------------------------------------------------------------+
    76. | A1| 1| 1|
    77. | A1| 1| 1|
    78. | A1| 2| 3|
    79. | A2| 3| 1|
    80. +---+---+--------------------------------------------------------------------------------------------------------+
    81. -- row_number
    82. SELECT a, b, row_number() OVER (PARTITION BY a ORDER BY b) FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
    83. +---+---+--------------------------------------------------------------------------------------------------------------+
    84. | a| b|row_number() OVER (PARTITION BY a ORDER BY b ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)|
    85. +---+---+--------------------------------------------------------------------------------------------------------------+
    86. | A1| 1| 1|
    87. | A1| 1| 2|
    88. | A1| 2| 3|
    89. | A2| 3| 1|
    90. +---+---+--------------------------------------------------------------------------------------------------------------+

    Array Functions

    FunctionDescription
    array(expr, …)Returns an array with the given elements.
    array_append(array, element)Add the element at the end of the array passed as first argument. Type of element should be similar to type of the elements of the array. Null element is also appended into the array. But if the array passed, is NULL output is NULL
    array_compact(array)Removes null values from the array.
    array_contains(array, value)Returns true if the array contains the value.
    array_distinct(array)Removes duplicate values from the array.
    array_except(array1, array2)Returns an array of the elements in array1 but not in array2, without duplicates.
    array_insert(x, pos, val)Places val into index pos of array x. Array indices start at 1, or start from the end if index is negative. Index above array size appends the array, or prepends the array if index is negative, with ‘null’ elements.
    array_intersect(array1, array2)Returns an array of the elements in the intersection of array1 and array2, without duplicates.
    array_join(array, delimiter[, nullReplacement])Concatenates the elements of the given array using the delimiter and an optional string to replace nulls. If no value is set for nullReplacement, any null value is filtered.
    array_max(array)Returns the maximum value in the array. NaN is greater than any non-NaN elements for double/float type. NULL elements are skipped.
    array_min(array)Returns the minimum value in the array. NaN is greater than any non-NaN elements for double/float type. NULL elements are skipped.
    array_position(array, element)Returns the (1-based) index of the first element of the array as long.
    array_remove(array, element)Remove all elements that equal to element from array.
    array_repeat(element, count)Returns the array containing element count times.
    array_union(array1, array2)Returns an array of the elements in the union of array1 and array2, without duplicates.
    arrays_overlap(a1, a2)Returns true if a1 contains at least a non-null element present also in a2. If the arrays have no common element and they are both non-empty and either of them contains a null element null is returned, false otherwise.
    arrays_zip(a1, a2, …)Returns a merged array of structs in which the N-th struct contains all N-th values of input arrays.
    flatten(arrayOfArrays)Transforms an array of arrays into a single array.
    get(array, index)Returns element of array at given (0-based) index. If the index points outside of the array boundaries, then this function returns NULL.
    sequence(start, stop, step)Generates an array of elements from start to stop (inclusive), incrementing by step. The type of the returned elements is the same as the type of argument expressions. Supported types are: byte, short, integer, long, date, timestamp. The start and stop expressions must resolve to the same type. If start and stop expressions resolve to the ‘date’ or ‘timestamp’ type then the step expression must resolve to the ‘interval’ or ‘year-month interval’ or ‘day-time interval’ type, otherwise to the same type as the start and stop expressions.
    shuffle(array)Returns a random permutation of the given array.
    slice(x, start, length)Subsets array x starting from index start (array indices start at 1, or starting from the end if start is negative) with the specified length.
    sort_array(array[, ascendingOrder])Sorts the input array in ascending or descending order according to the natural ordering of the array elements. NaN is greater than any non-NaN elements for double/float type. Null elements will be placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order.

    Examples

    1. -- array
    2. SELECT array(1, 2, 3);
    3. +--------------+
    4. |array(1, 2, 3)|
    5. +--------------+
    6. | [1, 2, 3]|
    7. +--------------+
    8. -- array_append
    9. SELECT array_append(array('b', 'd', 'c', 'a'), 'd');
    10. +----------------------------------+
    11. |array_append(array(b, d, c, a), d)|
    12. +----------------------------------+
    13. | [b, d, c, a, d]|
    14. +----------------------------------+
    15. SELECT array_append(array(1, 2, 3, null), null);
    16. +----------------------------------------+
    17. |array_append(array(1, 2, 3, NULL), NULL)|
    18. +----------------------------------------+
    19. | [1, 2, 3, null, n...|
    20. +----------------------------------------+
    21. SELECT array_append(CAST(null as Array<Int>), 2);
    22. +---------------------+
    23. |array_append(NULL, 2)|
    24. +---------------------+
    25. | null|
    26. +---------------------+
    27. -- array_compact
    28. SELECT array_compact(array(1, 2, 3, null));
    29. +-----------------------------------+
    30. |array_compact(array(1, 2, 3, NULL))|
    31. +-----------------------------------+
    32. | [1, 2, 3]|
    33. +-----------------------------------+
    34. SELECT array_compact(array("a", "b", "c"));
    35. +-----------------------------+
    36. |array_compact(array(a, b, c))|
    37. +-----------------------------+
    38. | [a, b, c]|
    39. +-----------------------------+
    40. -- array_contains
    41. SELECT array_contains(array(1, 2, 3), 2);
    42. +---------------------------------+
    43. |array_contains(array(1, 2, 3), 2)|
    44. +---------------------------------+
    45. | true|
    46. +---------------------------------+
    47. -- array_distinct
    48. SELECT array_distinct(array(1, 2, 3, null, 3));
    49. +---------------------------------------+
    50. |array_distinct(array(1, 2, 3, NULL, 3))|
    51. +---------------------------------------+
    52. | [1, 2, 3, null]|
    53. +---------------------------------------+
    54. -- array_except
    55. SELECT array_except(array(1, 2, 3), array(1, 3, 5));
    56. +--------------------------------------------+
    57. |array_except(array(1, 2, 3), array(1, 3, 5))|
    58. +--------------------------------------------+
    59. | [2]|
    60. +--------------------------------------------+
    61. -- array_insert
    62. SELECT array_insert(array(1, 2, 3, 4), 5, 5);
    63. +-------------------------------------+
    64. |array_insert(array(1, 2, 3, 4), 5, 5)|
    65. +-------------------------------------+
    66. | [1, 2, 3, 4, 5]|
    67. +-------------------------------------+
    68. SELECT array_insert(array(5, 3, 2, 1), -3, 4);
    69. +--------------------------------------+
    70. |array_insert(array(5, 3, 2, 1), -3, 4)|
    71. +--------------------------------------+
    72. | [5, 4, 3, 2, 1]|
    73. +--------------------------------------+
    74. -- array_intersect
    75. SELECT array_intersect(array(1, 2, 3), array(1, 3, 5));
    76. +-----------------------------------------------+
    77. |array_intersect(array(1, 2, 3), array(1, 3, 5))|
    78. +-----------------------------------------------+
    79. | [1, 3]|
    80. +-----------------------------------------------+
    81. -- array_join
    82. SELECT array_join(array('hello', 'world'), ' ');
    83. +----------------------------------+
    84. |array_join(array(hello, world), )|
    85. +----------------------------------+
    86. | hello world|
    87. +----------------------------------+
    88. SELECT array_join(array('hello', null ,'world'), ' ');
    89. +----------------------------------------+
    90. |array_join(array(hello, NULL, world), )|
    91. +----------------------------------------+
    92. | hello world|
    93. +----------------------------------------+
    94. SELECT array_join(array('hello', null ,'world'), ' ', ',');
    95. +-------------------------------------------+
    96. |array_join(array(hello, NULL, world), , ,)|
    97. +-------------------------------------------+
    98. | hello , world|
    99. +-------------------------------------------+
    100. -- array_max
    101. SELECT array_max(array(1, 20, null, 3));
    102. +--------------------------------+
    103. |array_max(array(1, 20, NULL, 3))|
    104. +--------------------------------+
    105. | 20|
    106. +--------------------------------+
    107. -- array_min
    108. SELECT array_min(array(1, 20, null, 3));
    109. +--------------------------------+
    110. |array_min(array(1, 20, NULL, 3))|
    111. +--------------------------------+
    112. | 1|
    113. +--------------------------------+
    114. -- array_position
    115. SELECT array_position(array(3, 2, 1), 1);
    116. +---------------------------------+
    117. |array_position(array(3, 2, 1), 1)|
    118. +---------------------------------+
    119. | 3|
    120. +---------------------------------+
    121. -- array_remove
    122. SELECT array_remove(array(1, 2, 3, null, 3), 3);
    123. +----------------------------------------+
    124. |array_remove(array(1, 2, 3, NULL, 3), 3)|
    125. +----------------------------------------+
    126. | [1, 2, null]|
    127. +----------------------------------------+
    128. -- array_repeat
    129. SELECT array_repeat('123', 2);
    130. +--------------------+
    131. |array_repeat(123, 2)|
    132. +--------------------+
    133. | [123, 123]|
    134. +--------------------+
    135. -- array_union
    136. SELECT array_union(array(1, 2, 3), array(1, 3, 5));
    137. +-------------------------------------------+
    138. |array_union(array(1, 2, 3), array(1, 3, 5))|
    139. +-------------------------------------------+
    140. | [1, 2, 3, 5]|
    141. +-------------------------------------------+
    142. -- arrays_overlap
    143. SELECT arrays_overlap(array(1, 2, 3), array(3, 4, 5));
    144. +----------------------------------------------+
    145. |arrays_overlap(array(1, 2, 3), array(3, 4, 5))|
    146. +----------------------------------------------+
    147. | true|
    148. +----------------------------------------------+
    149. -- arrays_zip
    150. SELECT arrays_zip(array(1, 2, 3), array(2, 3, 4));
    151. +------------------------------------------+
    152. |arrays_zip(array(1, 2, 3), array(2, 3, 4))|
    153. +------------------------------------------+
    154. | [{1, 2}, {2, 3}, ...|
    155. +------------------------------------------+
    156. SELECT arrays_zip(array(1, 2), array(2, 3), array(3, 4));
    157. +-------------------------------------------------+
    158. |arrays_zip(array(1, 2), array(2, 3), array(3, 4))|
    159. +-------------------------------------------------+
    160. | [{1, 2, 3}, {2, 3...|
    161. +-------------------------------------------------+
    162. -- flatten
    163. SELECT flatten(array(array(1, 2), array(3, 4)));
    164. +----------------------------------------+
    165. |flatten(array(array(1, 2), array(3, 4)))|
    166. +----------------------------------------+
    167. | [1, 2, 3, 4]|
    168. +----------------------------------------+
    169. -- get
    170. SELECT get(array(1, 2, 3), 0);
    171. +----------------------+
    172. |get(array(1, 2, 3), 0)|
    173. +----------------------+
    174. | 1|
    175. +----------------------+
    176. SELECT get(array(1, 2, 3), 3);
    177. +----------------------+
    178. |get(array(1, 2, 3), 3)|
    179. +----------------------+
    180. | null|
    181. +----------------------+
    182. SELECT get(array(1, 2, 3), -1);
    183. +-----------------------+
    184. |get(array(1, 2, 3), -1)|
    185. +-----------------------+
    186. | null|
    187. +-----------------------+
    188. -- sequence
    189. SELECT sequence(1, 5);
    190. +---------------+
    191. | sequence(1, 5)|
    192. +---------------+
    193. |[1, 2, 3, 4, 5]|
    194. +---------------+
    195. SELECT sequence(5, 1);
    196. +---------------+
    197. | sequence(5, 1)|
    198. +---------------+
    199. |[5, 4, 3, 2, 1]|
    200. +---------------+
    201. SELECT sequence(to_date('2018-01-01'), to_date('2018-03-01'), interval 1 month);
    202. +----------------------------------------------------------------------+
    203. |sequence(to_date(2018-01-01), to_date(2018-03-01), INTERVAL '1' MONTH)|
    204. +----------------------------------------------------------------------+
    205. | [2018-01-01, 2018...|
    206. +----------------------------------------------------------------------+
    207. SELECT sequence(to_date('2018-01-01'), to_date('2018-03-01'), interval '0-1' year to month);
    208. +--------------------------------------------------------------------------------+
    209. |sequence(to_date(2018-01-01), to_date(2018-03-01), INTERVAL '0-1' YEAR TO MONTH)|
    210. +--------------------------------------------------------------------------------+
    211. | [2018-01-01, 2018...|
    212. +--------------------------------------------------------------------------------+
    213. -- shuffle
    214. SELECT shuffle(array(1, 20, 3, 5));
    215. +---------------------------+
    216. |shuffle(array(1, 20, 3, 5))|
    217. +---------------------------+
    218. | [20, 3, 5, 1]|
    219. +---------------------------+
    220. SELECT shuffle(array(1, 20, null, 3));
    221. +------------------------------+
    222. |shuffle(array(1, 20, NULL, 3))|
    223. +------------------------------+
    224. | [3, 1, 20, null]|
    225. +------------------------------+
    226. -- slice
    227. SELECT slice(array(1, 2, 3, 4), 2, 2);
    228. +------------------------------+
    229. |slice(array(1, 2, 3, 4), 2, 2)|
    230. +------------------------------+
    231. | [2, 3]|
    232. +------------------------------+
    233. SELECT slice(array(1, 2, 3, 4), -2, 2);
    234. +-------------------------------+
    235. |slice(array(1, 2, 3, 4), -2, 2)|
    236. +-------------------------------+
    237. | [3, 4]|
    238. +-------------------------------+
    239. -- sort_array
    240. SELECT sort_array(array('b', 'd', null, 'c', 'a'), true);
    241. +-----------------------------------------+
    242. |sort_array(array(b, d, NULL, c, a), true)|
    243. +-----------------------------------------+
    244. | [null, a, b, c, d]|
    245. +-----------------------------------------+

    Map Functions

    FunctionDescription
    element_at(array, index)Returns element of array at given (1-based) index. If Index is 0, Spark will throw an error. If index < 0, accesses elements from the last to the first. The function returns NULL if the index exceeds the length of the array and spark.sql.ansi.enabled is set to false. If spark.sql.ansi.enabled is set to true, it throws ArrayIndexOutOfBoundsException for invalid indices.
    element_at(map, key)Returns value for given key. The function returns NULL if the key is not contained in the map.
    map(key0, value0, key1, value1, …)Creates a map with the given key/value pairs.
    map_concat(map, …)Returns the union of all the given maps
    map_contains_key(map, key)Returns true if the map contains the key.
    map_entries(map)Returns an unordered array of all entries in the given map.
    map_from_arrays(keys, values)Creates a map with a pair of the given key/value arrays. All elements in keys should not be null
    map_from_entries(arrayOfEntries)Returns a map created from the given array of entries.
    map_keys(map)Returns an unordered array containing the keys of the map.
    map_values(map)Returns an unordered array containing the values of the map.
    str_to_map(text[, pairDelim[, keyValueDelim]])Creates a map after splitting the text into key/value pairs using delimiters. Default delimiters are ‘,’ for pairDelim and ‘:’ for keyValueDelim. Both pairDelim and keyValueDelim are treated as regular expressions.
    try_element_at(array, index)Returns element of array at given (1-based) index. If Index is 0, Spark will throw an error. If index < 0, accesses elements from the last to the first. The function always returns NULL if the index exceeds the length of the array.
    try_element_at(map, key)Returns value for given key. The function always returns NULL if the key is not contained in the map.

    Examples

    1. -- element_at
    2. SELECT element_at(array(1, 2, 3), 2);
    3. +-----------------------------+
    4. |element_at(array(1, 2, 3), 2)|
    5. +-----------------------------+
    6. | 2|
    7. +-----------------------------+
    8. SELECT element_at(map(1, 'a', 2, 'b'), 2);
    9. +------------------------------+
    10. |element_at(map(1, a, 2, b), 2)|
    11. +------------------------------+
    12. | b|
    13. +------------------------------+
    14. -- map
    15. SELECT map(1.0, '2', 3.0, '4');
    16. +--------------------+
    17. | map(1.0, 2, 3.0, 4)|
    18. +--------------------+
    19. |{1.0 -> 2, 3.0 -> 4}|
    20. +--------------------+
    21. -- map_concat
    22. SELECT map_concat(map(1, 'a', 2, 'b'), map(3, 'c'));
    23. +--------------------------------------+
    24. |map_concat(map(1, a, 2, b), map(3, c))|
    25. +--------------------------------------+
    26. | {1 -> a, 2 -> b, ...|
    27. +--------------------------------------+
    28. -- map_contains_key
    29. SELECT map_contains_key(map(1, 'a', 2, 'b'), 1);
    30. +------------------------------------+
    31. |map_contains_key(map(1, a, 2, b), 1)|
    32. +------------------------------------+
    33. | true|
    34. +------------------------------------+
    35. SELECT map_contains_key(map(1, 'a', 2, 'b'), 3);
    36. +------------------------------------+
    37. |map_contains_key(map(1, a, 2, b), 3)|
    38. +------------------------------------+
    39. | false|
    40. +------------------------------------+
    41. -- map_entries
    42. SELECT map_entries(map(1, 'a', 2, 'b'));
    43. +----------------------------+
    44. |map_entries(map(1, a, 2, b))|
    45. +----------------------------+
    46. | [{1, a}, {2, b}]|
    47. +----------------------------+
    48. -- map_from_arrays
    49. SELECT map_from_arrays(array(1.0, 3.0), array('2', '4'));
    50. +---------------------------------------------+
    51. |map_from_arrays(array(1.0, 3.0), array(2, 4))|
    52. +---------------------------------------------+
    53. | {1.0 -> 2, 3.0 -> 4}|
    54. +---------------------------------------------+
    55. -- map_from_entries
    56. SELECT map_from_entries(array(struct(1, 'a'), struct(2, 'b')));
    57. +---------------------------------------------------+
    58. |map_from_entries(array(struct(1, a), struct(2, b)))|
    59. +---------------------------------------------------+
    60. | {1 -> a, 2 -> b}|
    61. +---------------------------------------------------+
    62. -- map_keys
    63. SELECT map_keys(map(1, 'a', 2, 'b'));
    64. +-------------------------+
    65. |map_keys(map(1, a, 2, b))|
    66. +-------------------------+
    67. | [1, 2]|
    68. +-------------------------+
    69. -- map_values
    70. SELECT map_values(map(1, 'a', 2, 'b'));
    71. +---------------------------+
    72. |map_values(map(1, a, 2, b))|
    73. +---------------------------+
    74. | [a, b]|
    75. +---------------------------+
    76. -- str_to_map
    77. SELECT str_to_map('a:1,b:2,c:3', ',', ':');
    78. +-----------------------------+
    79. |str_to_map(a:1,b:2,c:3, ,, :)|
    80. +-----------------------------+
    81. | {a -> 1, b -> 2, ...|
    82. +-----------------------------+
    83. SELECT str_to_map('a');
    84. +-------------------+
    85. |str_to_map(a, ,, :)|
    86. +-------------------+
    87. | {a -> null}|
    88. +-------------------+
    89. -- try_element_at
    90. SELECT try_element_at(array(1, 2, 3), 2);
    91. +---------------------------------+
    92. |try_element_at(array(1, 2, 3), 2)|
    93. +---------------------------------+
    94. | 2|
    95. +---------------------------------+
    96. SELECT try_element_at(map(1, 'a', 2, 'b'), 2);
    97. +----------------------------------+
    98. |try_element_at(map(1, a, 2, b), 2)|
    99. +----------------------------------+
    100. | b|
    101. +----------------------------------+

    Date and Timestamp Functions

    FunctionDescription
    add_months(start_date, num_months)Returns the date that is num_months after start_date.
    convert_timezone([sourceTz, ]targetTz, sourceTs)Converts the timestamp without time zone sourceTs from the sourceTz time zone to targetTz.
    curdate()Returns the current date at the start of query evaluation. All calls of curdate within the same query return the same value.
    current_date()Returns the current date at the start of query evaluation. All calls of current_date within the same query return the same value.
    current_dateReturns the current date at the start of query evaluation.
    current_timestamp()Returns the current timestamp at the start of query evaluation. All calls of current_timestamp within the same query return the same value.
    current_timestampReturns the current timestamp at the start of query evaluation.
    current_timezone()Returns the current session local timezone.
    date_add(start_date, num_days)Returns the date that is num_days after start_date.
    date_diff(endDate, startDate)Returns the number of days from startDate to endDate.
    date_format(timestamp, fmt)Converts timestamp to a value of string in the format specified by the date format fmt.
    date_from_unix_date(days)Create date from the number of days since 1970-01-01.
    date_part(field, source)Extracts a part of the date/timestamp or interval source.
    date_sub(start_date, num_days)Returns the date that is num_days before start_date.
    date_trunc(fmt, ts)Returns timestamp ts truncated to the unit specified by the format model fmt.
    dateadd(start_date, num_days)Returns the date that is num_days after start_date.
    datediff(endDate, startDate)Returns the number of days from startDate to endDate.
    datepart(field, source)Extracts a part of the date/timestamp or interval source.
    day(date)Returns the day of month of the date/timestamp.
    dayofmonth(date)Returns the day of month of the date/timestamp.
    dayofweek(date)Returns the day of the week for date/timestamp (1 = Sunday, 2 = Monday, …, 7 = Saturday).
    dayofyear(date)Returns the day of year of the date/timestamp.
    extract(field FROM source)Extracts a part of the date/timestamp or interval source.
    from_unixtime(unix_time[, fmt])Returns unix_time in the specified fmt.
    from_utc_timestamp(timestamp, timezone)Given a timestamp like ‘2017-07-14 02:40:00.0’, interprets it as a time in UTC, and renders that time as a timestamp in the given time zone. For example, ‘GMT+1’ would yield ‘2017-07-14 03:40:00.0’.
    hour(timestamp)Returns the hour component of the string/timestamp.
    last_day(date)Returns the last day of the month which the date belongs to.
    localtimestamp()Returns the current timestamp without time zone at the start of query evaluation. All calls of localtimestamp within the same query return the same value.
    localtimestampReturns the current local date-time at the session time zone at the start of query evaluation.
    make_date(year, month, day)Create date from year, month and day fields. If the configuration spark.sql.ansi.enabled is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.
    make_dt_interval([days[, hours[, mins[, secs]]]])Make DayTimeIntervalType duration from days, hours, mins and secs.
    make_interval([years[, months[, weeks[, days[, hours[, mins[, secs]]]]]]])Make interval from years, months, weeks, days, hours, mins and secs.
    make_timestamp(year, month, day, hour, min, sec[, timezone])Create timestamp from year, month, day, hour, min, sec and timezone fields. The result data type is consistent with the value of configuration spark.sql.timestampType. If the configuration spark.sql.ansi.enabled is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.
    make_timestamp_ltz(year, month, day, hour, min, sec[, timezone])Create the current timestamp with local time zone from year, month, day, hour, min, sec and timezone fields. If the configuration spark.sql.ansi.enabled is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.
    make_timestamp_ntz(year, month, day, hour, min, sec)Create local date-time from year, month, day, hour, min, sec fields. If the configuration spark.sql.ansi.enabled is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.
    make_ym_interval([years[, months]])Make year-month interval from years, months.
    minute(timestamp)Returns the minute component of the string/timestamp.
    month(date)Returns the month component of the date/timestamp.
    months_between(timestamp1, timestamp2[, roundOff])If timestamp1 is later than timestamp2, then the result is positive. If timestamp1 and timestamp2 are on the same day of month, or both are the last day of month, time of day will be ignored. Otherwise, the difference is calculated based on 31 days per month, and rounded to 8 digits unless roundOff=false.
    next_day(start_date, day_of_week)Returns the first date which is later than start_date and named as indicated. The function returns NULL if at least one of the input parameters is NULL. When both of the input parameters are not NULL and day_of_week is an invalid input, the function throws IllegalArgumentException if spark.sql.ansi.enabled is set to true, otherwise NULL.
    now()Returns the current timestamp at the start of query evaluation.
    quarter(date)Returns the quarter of the year for date, in the range 1 to 4.
    second(timestamp)Returns the second component of the string/timestamp.
    session_window(time_column, gap_duration)Generates session window given a timestamp specifying column and gap duration. See in Structured Streaming guide doc for detailed explanation and examples.
    timestamp_micros(microseconds)Creates timestamp from the number of microseconds since UTC epoch.
    timestamp_millis(milliseconds)Creates timestamp from the number of milliseconds since UTC epoch.
    timestamp_seconds(seconds)Creates timestamp from the number of seconds (can be fractional) since UTC epoch.
    to_date(date_str[, fmt])Parses the date_str expression with the fmt expression to a date. Returns null with invalid input. By default, it follows casting rules to a date if the fmt is omitted.
    to_timestamp(timestamp_str[, fmt])Parses the timestamp_str expression with the fmt expression to a timestamp. Returns null with invalid input. By default, it follows casting rules to a timestamp if the fmt is omitted. The result data type is consistent with the value of configuration spark.sql.timestampType.
    to_timestamp_ltz(timestamp_str[, fmt])Parses the timestamp_str expression with the fmt expression to a timestamp with local time zone. Returns null with invalid input. By default, it follows casting rules to a timestamp if the fmt is omitted.
    to_timestamp_ntz(timestamp_str[, fmt])Parses the timestamp_str expression with the fmt expression to a timestamp without time zone. Returns null with invalid input. By default, it follows casting rules to a timestamp if the fmt is omitted.
    to_unix_timestamp(timeExp[, fmt])Returns the UNIX timestamp of the given time.
    to_utc_timestamp(timestamp, timezone)Given a timestamp like ‘2017-07-14 02:40:00.0’, interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, ‘GMT+1’ would yield ‘2017-07-14 01:40:00.0’.
    trunc(date, fmt)Returns date with the time portion of the day truncated to the unit specified by the format model fmt.
    try_to_timestamp(timestamp_str[, fmt])Parses the timestamp_str expression with the fmt expression to a timestamp. The function always returns null on an invalid input with/without ANSI SQL mode enabled. By default, it follows casting rules to a timestamp if the fmt is omitted. The result data type is consistent with the value of configuration spark.sql.timestampType.
    unix_date(date)Returns the number of days since 1970-01-01.
    unix_micros(timestamp)Returns the number of microseconds since 1970-01-01 00:00:00 UTC.
    unix_millis(timestamp)Returns the number of milliseconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.
    unix_seconds(timestamp)Returns the number of seconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.
    unix_timestamp([timeExp[, fmt]])Returns the UNIX timestamp of current or specified time.
    weekday(date)Returns the day of the week for date/timestamp (0 = Monday, 1 = Tuesday, …, 6 = Sunday).
    weekofyear(date)Returns the week of the year of the given date. A week is considered to start on a Monday and week 1 is the first week with >3 days.
    window(time_column, window_duration[, slide_duration[, start_time]])Bucketize rows into one or more time windows given a timestamp specifying column. Window starts are inclusive but the window ends are exclusive, e.g. 12:05 will be in the window [12:05,12:10) but not in [12:00,12:05). Windows can support microsecond precision. Windows in the order of months are not supported. See ‘Window Operations on Event Time’ in Structured Streaming guide doc for detailed explanation and examples.
    window_time(window_column)Extract the time value from time/session window column which can be used for event time value of window. The extracted time is (window.end - 1) which reflects the fact that the the aggregating windows have exclusive upper bound - [start, end) See in Structured Streaming guide doc for detailed explanation and examples.
    year(date)Returns the year component of the date/timestamp.

    Examples

    1. -- add_months
    2. SELECT add_months('2016-08-31', 1);
    3. +-------------------------+
    4. |add_months(2016-08-31, 1)|
    5. +-------------------------+
    6. | 2016-09-30|
    7. +-------------------------+
    8. -- convert_timezone
    9. SELECT convert_timezone('Europe/Brussels', 'America/Los_Angeles', timestamp_ntz'2021-12-06 00:00:00');
    10. +-------------------------------------------------------------------------------------------+
    11. |convert_timezone(Europe/Brussels, America/Los_Angeles, TIMESTAMP_NTZ '2021-12-06 00:00:00')|
    12. +-------------------------------------------------------------------------------------------+
    13. | 2021-12-05 15:00:00|
    14. +-------------------------------------------------------------------------------------------+
    15. SELECT convert_timezone('Europe/Brussels', timestamp_ntz'2021-12-05 15:00:00');
    16. +------------------------------------------------------------------------------------------+
    17. |convert_timezone(current_timezone(), Europe/Brussels, TIMESTAMP_NTZ '2021-12-05 15:00:00')|
    18. +------------------------------------------------------------------------------------------+
    19. | 2021-12-05 16:00:00|
    20. +------------------------------------------------------------------------------------------+
    21. -- curdate
    22. SELECT curdate();
    23. +--------------+
    24. |current_date()|
    25. +--------------+
    26. | 2023-04-14|
    27. +--------------+
    28. -- current_date
    29. SELECT current_date();
    30. +--------------+
    31. |current_date()|
    32. +--------------+
    33. | 2023-04-14|
    34. +--------------+
    35. SELECT current_date;
    36. +--------------+
    37. |current_date()|
    38. +--------------+
    39. | 2023-04-14|
    40. +--------------+
    41. -- current_timestamp
    42. SELECT current_timestamp();
    43. +--------------------+
    44. | current_timestamp()|
    45. +--------------------+
    46. |2023-04-14 18:42:...|
    47. +--------------------+
    48. SELECT current_timestamp;
    49. +--------------------+
    50. | current_timestamp()|
    51. +--------------------+
    52. |2023-04-14 18:42:...|
    53. +--------------------+
    54. -- current_timezone
    55. SELECT current_timezone();
    56. +------------------+
    57. |current_timezone()|
    58. +------------------+
    59. | Etc/UTC|
    60. +------------------+
    61. -- date_add
    62. SELECT date_add('2016-07-30', 1);
    63. +-----------------------+
    64. |date_add(2016-07-30, 1)|
    65. +-----------------------+
    66. | 2016-07-31|
    67. +-----------------------+
    68. -- date_diff
    69. SELECT date_diff('2009-07-31', '2009-07-30');
    70. +---------------------------------+
    71. |date_diff(2009-07-31, 2009-07-30)|
    72. +---------------------------------+
    73. | 1|
    74. +---------------------------------+
    75. SELECT date_diff('2009-07-30', '2009-07-31');
    76. +---------------------------------+
    77. |date_diff(2009-07-30, 2009-07-31)|
    78. +---------------------------------+
    79. | -1|
    80. +---------------------------------+
    81. -- date_format
    82. SELECT date_format('2016-04-08', 'y');
    83. +--------------------------+
    84. |date_format(2016-04-08, y)|
    85. +--------------------------+
    86. | 2016|
    87. +--------------------------+
    88. -- date_from_unix_date
    89. SELECT date_from_unix_date(1);
    90. +----------------------+
    91. |date_from_unix_date(1)|
    92. +----------------------+
    93. | 1970-01-02|
    94. +----------------------+
    95. -- date_part
    96. SELECT date_part('YEAR', TIMESTAMP '2019-08-12 01:00:00.123456');
    97. +-------------------------------------------------------+
    98. |date_part(YEAR, TIMESTAMP '2019-08-12 01:00:00.123456')|
    99. +-------------------------------------------------------+
    100. | 2019|
    101. +-------------------------------------------------------+
    102. SELECT date_part('week', timestamp'2019-08-12 01:00:00.123456');
    103. +-------------------------------------------------------+
    104. |date_part(week, TIMESTAMP '2019-08-12 01:00:00.123456')|
    105. +-------------------------------------------------------+
    106. | 33|
    107. +-------------------------------------------------------+
    108. SELECT date_part('doy', DATE'2019-08-12');
    109. +---------------------------------+
    110. |date_part(doy, DATE '2019-08-12')|
    111. +---------------------------------+
    112. | 224|
    113. +---------------------------------+
    114. SELECT date_part('SECONDS', timestamp'2019-10-01 00:00:01.000001');
    115. +----------------------------------------------------------+
    116. |date_part(SECONDS, TIMESTAMP '2019-10-01 00:00:01.000001')|
    117. +----------------------------------------------------------+
    118. | 1.000001|
    119. +----------------------------------------------------------+
    120. SELECT date_part('days', interval 5 days 3 hours 7 minutes);
    121. +-------------------------------------------------+
    122. |date_part(days, INTERVAL '5 03:07' DAY TO MINUTE)|
    123. +-------------------------------------------------+
    124. | 5|
    125. +-------------------------------------------------+
    126. SELECT date_part('seconds', interval 5 hours 30 seconds 1 milliseconds 1 microseconds);
    127. +-------------------------------------------------------------+
    128. |date_part(seconds, INTERVAL '05:00:30.001001' HOUR TO SECOND)|
    129. +-------------------------------------------------------------+
    130. | 30.001001|
    131. +-------------------------------------------------------------+
    132. SELECT date_part('MONTH', INTERVAL '2021-11' YEAR TO MONTH);
    133. +--------------------------------------------------+
    134. |date_part(MONTH, INTERVAL '2021-11' YEAR TO MONTH)|
    135. +--------------------------------------------------+
    136. | 11|
    137. +--------------------------------------------------+
    138. SELECT date_part('MINUTE', INTERVAL '123 23:55:59.002001' DAY TO SECOND);
    139. +---------------------------------------------------------------+
    140. |date_part(MINUTE, INTERVAL '123 23:55:59.002001' DAY TO SECOND)|
    141. +---------------------------------------------------------------+
    142. | 55|
    143. +---------------------------------------------------------------+
    144. -- date_sub
    145. SELECT date_sub('2016-07-30', 1);
    146. +-----------------------+
    147. |date_sub(2016-07-30, 1)|
    148. +-----------------------+
    149. | 2016-07-29|
    150. +-----------------------+
    151. -- date_trunc
    152. SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
    153. +-----------------------------------------+
    154. |date_trunc(YEAR, 2015-03-05T09:32:05.359)|
    155. +-----------------------------------------+
    156. | 2015-01-01 00:00:00|
    157. +-----------------------------------------+
    158. SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
    159. +---------------------------------------+
    160. |date_trunc(MM, 2015-03-05T09:32:05.359)|
    161. +---------------------------------------+
    162. | 2015-03-01 00:00:00|
    163. +---------------------------------------+
    164. SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
    165. +---------------------------------------+
    166. |date_trunc(DD, 2015-03-05T09:32:05.359)|
    167. +---------------------------------------+
    168. | 2015-03-05 00:00:00|
    169. +---------------------------------------+
    170. SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
    171. +-----------------------------------------+
    172. |date_trunc(HOUR, 2015-03-05T09:32:05.359)|
    173. +-----------------------------------------+
    174. | 2015-03-05 09:00:00|
    175. +-----------------------------------------+
    176. SELECT date_trunc('MILLISECOND', '2015-03-05T09:32:05.123456');
    177. +---------------------------------------------------+
    178. |date_trunc(MILLISECOND, 2015-03-05T09:32:05.123456)|
    179. +---------------------------------------------------+
    180. | 2015-03-05 09:32:...|
    181. +---------------------------------------------------+
    182. -- dateadd
    183. SELECT dateadd('2016-07-30', 1);
    184. +-----------------------+
    185. |date_add(2016-07-30, 1)|
    186. +-----------------------+
    187. | 2016-07-31|
    188. +-----------------------+
    189. -- datediff
    190. SELECT datediff('2009-07-31', '2009-07-30');
    191. +--------------------------------+
    192. |datediff(2009-07-31, 2009-07-30)|
    193. +--------------------------------+
    194. | 1|
    195. +--------------------------------+
    196. SELECT datediff('2009-07-30', '2009-07-31');
    197. +--------------------------------+
    198. |datediff(2009-07-30, 2009-07-31)|
    199. +--------------------------------+
    200. | -1|
    201. +--------------------------------+
    202. -- datepart
    203. SELECT datepart('YEAR', TIMESTAMP '2019-08-12 01:00:00.123456');
    204. +----------------------------------------------------------+
    205. |datepart(YEAR FROM TIMESTAMP '2019-08-12 01:00:00.123456')|
    206. +----------------------------------------------------------+
    207. | 2019|
    208. +----------------------------------------------------------+
    209. SELECT datepart('week', timestamp'2019-08-12 01:00:00.123456');
    210. +----------------------------------------------------------+
    211. |datepart(week FROM TIMESTAMP '2019-08-12 01:00:00.123456')|
    212. +----------------------------------------------------------+
    213. | 33|
    214. +----------------------------------------------------------+
    215. SELECT datepart('doy', DATE'2019-08-12');
    216. +------------------------------------+
    217. |datepart(doy FROM DATE '2019-08-12')|
    218. +------------------------------------+
    219. | 224|
    220. +------------------------------------+
    221. SELECT datepart('SECONDS', timestamp'2019-10-01 00:00:01.000001');
    222. +-------------------------------------------------------------+
    223. |datepart(SECONDS FROM TIMESTAMP '2019-10-01 00:00:01.000001')|
    224. +-------------------------------------------------------------+
    225. | 1.000001|
    226. +-------------------------------------------------------------+
    227. SELECT datepart('days', interval 5 days 3 hours 7 minutes);
    228. +----------------------------------------------------+
    229. |datepart(days FROM INTERVAL '5 03:07' DAY TO MINUTE)|
    230. +----------------------------------------------------+
    231. | 5|
    232. +----------------------------------------------------+
    233. SELECT datepart('seconds', interval 5 hours 30 seconds 1 milliseconds 1 microseconds);
    234. +----------------------------------------------------------------+
    235. |datepart(seconds FROM INTERVAL '05:00:30.001001' HOUR TO SECOND)|
    236. +----------------------------------------------------------------+
    237. | 30.001001|
    238. +----------------------------------------------------------------+
    239. SELECT datepart('MONTH', INTERVAL '2021-11' YEAR TO MONTH);
    240. +-----------------------------------------------------+
    241. |datepart(MONTH FROM INTERVAL '2021-11' YEAR TO MONTH)|
    242. +-----------------------------------------------------+
    243. | 11|
    244. +-----------------------------------------------------+
    245. SELECT datepart('MINUTE', INTERVAL '123 23:55:59.002001' DAY TO SECOND);
    246. +------------------------------------------------------------------+
    247. |datepart(MINUTE FROM INTERVAL '123 23:55:59.002001' DAY TO SECOND)|
    248. +------------------------------------------------------------------+
    249. | 55|
    250. +------------------------------------------------------------------+
    251. -- day
    252. SELECT day('2009-07-30');
    253. +---------------+
    254. |day(2009-07-30)|
    255. +---------------+
    256. | 30|
    257. +---------------+
    258. -- dayofmonth
    259. SELECT dayofmonth('2009-07-30');
    260. +----------------------+
    261. |dayofmonth(2009-07-30)|
    262. +----------------------+
    263. | 30|
    264. +----------------------+
    265. -- dayofweek
    266. SELECT dayofweek('2009-07-30');
    267. +---------------------+
    268. |dayofweek(2009-07-30)|
    269. +---------------------+
    270. | 5|
    271. +---------------------+
    272. -- dayofyear
    273. SELECT dayofyear('2016-04-09');
    274. +---------------------+
    275. |dayofyear(2016-04-09)|
    276. +---------------------+
    277. | 100|
    278. +---------------------+
    279. -- extract
    280. SELECT extract(YEAR FROM TIMESTAMP '2019-08-12 01:00:00.123456');
    281. +---------------------------------------------------------+
    282. |extract(YEAR FROM TIMESTAMP '2019-08-12 01:00:00.123456')|
    283. +---------------------------------------------------------+
    284. | 2019|
    285. +---------------------------------------------------------+
    286. SELECT extract(week FROM timestamp'2019-08-12 01:00:00.123456');
    287. +---------------------------------------------------------+
    288. |extract(week FROM TIMESTAMP '2019-08-12 01:00:00.123456')|
    289. +---------------------------------------------------------+
    290. | 33|
    291. +---------------------------------------------------------+
    292. SELECT extract(doy FROM DATE'2019-08-12');
    293. +-----------------------------------+
    294. |extract(doy FROM DATE '2019-08-12')|
    295. +-----------------------------------+
    296. | 224|
    297. +-----------------------------------+
    298. SELECT extract(SECONDS FROM timestamp'2019-10-01 00:00:01.000001');
    299. +------------------------------------------------------------+
    300. |extract(SECONDS FROM TIMESTAMP '2019-10-01 00:00:01.000001')|
    301. +------------------------------------------------------------+
    302. | 1.000001|
    303. +------------------------------------------------------------+
    304. SELECT extract(days FROM interval 5 days 3 hours 7 minutes);
    305. +---------------------------------------------------+
    306. |extract(days FROM INTERVAL '5 03:07' DAY TO MINUTE)|
    307. +---------------------------------------------------+
    308. | 5|
    309. +---------------------------------------------------+
    310. SELECT extract(seconds FROM interval 5 hours 30 seconds 1 milliseconds 1 microseconds);
    311. +---------------------------------------------------------------+
    312. |extract(seconds FROM INTERVAL '05:00:30.001001' HOUR TO SECOND)|
    313. +---------------------------------------------------------------+
    314. | 30.001001|
    315. +---------------------------------------------------------------+
    316. SELECT extract(MONTH FROM INTERVAL '2021-11' YEAR TO MONTH);
    317. +----------------------------------------------------+
    318. |extract(MONTH FROM INTERVAL '2021-11' YEAR TO MONTH)|
    319. +----------------------------------------------------+
    320. | 11|
    321. +----------------------------------------------------+
    322. SELECT extract(MINUTE FROM INTERVAL '123 23:55:59.002001' DAY TO SECOND);
    323. +-----------------------------------------------------------------+
    324. |extract(MINUTE FROM INTERVAL '123 23:55:59.002001' DAY TO SECOND)|
    325. +-----------------------------------------------------------------+
    326. | 55|
    327. +-----------------------------------------------------------------+
    328. -- from_unixtime
    329. SELECT from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');
    330. +-------------------------------------+
    331. |from_unixtime(0, yyyy-MM-dd HH:mm:ss)|
    332. +-------------------------------------+
    333. | 1970-01-01 00:00:00|
    334. +-------------------------------------+
    335. SELECT from_unixtime(0);
    336. +-------------------------------------+
    337. |from_unixtime(0, yyyy-MM-dd HH:mm:ss)|
    338. +-------------------------------------+
    339. | 1970-01-01 00:00:00|
    340. +-------------------------------------+
    341. -- from_utc_timestamp
    342. SELECT from_utc_timestamp('2016-08-31', 'Asia/Seoul');
    343. +------------------------------------------+
    344. |from_utc_timestamp(2016-08-31, Asia/Seoul)|
    345. +------------------------------------------+
    346. | 2016-08-31 09:00:00|
    347. +------------------------------------------+
    348. -- hour
    349. SELECT hour('2009-07-30 12:58:59');
    350. +-------------------------+
    351. |hour(2009-07-30 12:58:59)|
    352. +-------------------------+
    353. | 12|
    354. +-------------------------+
    355. -- last_day
    356. SELECT last_day('2009-01-12');
    357. +--------------------+
    358. |last_day(2009-01-12)|
    359. +--------------------+
    360. | 2009-01-31|
    361. +--------------------+
    362. -- localtimestamp
    363. SELECT localtimestamp();
    364. +--------------------+
    365. | localtimestamp()|
    366. +--------------------+
    367. |2023-04-14 18:42:...|
    368. +--------------------+
    369. -- make_date
    370. SELECT make_date(2013, 7, 15);
    371. +----------------------+
    372. |make_date(2013, 7, 15)|
    373. +----------------------+
    374. | 2013-07-15|
    375. +----------------------+
    376. SELECT make_date(2019, 7, NULL);
    377. +------------------------+
    378. |make_date(2019, 7, NULL)|
    379. +------------------------+
    380. | null|
    381. +------------------------+
    382. -- make_dt_interval
    383. SELECT make_dt_interval(1, 12, 30, 01.001001);
    384. +-------------------------------------+
    385. |make_dt_interval(1, 12, 30, 1.001001)|
    386. +-------------------------------------+
    387. | INTERVAL '1 12:30...|
    388. +-------------------------------------+
    389. SELECT make_dt_interval(2);
    390. +-----------------------------------+
    391. |make_dt_interval(2, 0, 0, 0.000000)|
    392. +-----------------------------------+
    393. | INTERVAL '2 00:00...|
    394. +-----------------------------------+
    395. SELECT make_dt_interval(100, null, 3);
    396. +----------------------------------------+
    397. |make_dt_interval(100, NULL, 3, 0.000000)|
    398. +----------------------------------------+
    399. | null|
    400. +----------------------------------------+
    401. -- make_interval
    402. SELECT make_interval(100, 11, 1, 1, 12, 30, 01.001001);
    403. +----------------------------------------------+
    404. |make_interval(100, 11, 1, 1, 12, 30, 1.001001)|
    405. +----------------------------------------------+
    406. | 100 years 11 mont...|
    407. +----------------------------------------------+
    408. SELECT make_interval(100, null, 3);
    409. +----------------------------------------------+
    410. |make_interval(100, NULL, 3, 0, 0, 0, 0.000000)|
    411. +----------------------------------------------+
    412. | null|
    413. +----------------------------------------------+
    414. SELECT make_interval(0, 1, 0, 1, 0, 0, 100.000001);
    415. +-------------------------------------------+
    416. |make_interval(0, 1, 0, 1, 0, 0, 100.000001)|
    417. +-------------------------------------------+
    418. | 1 months 1 days 1...|
    419. +-------------------------------------------+
    420. -- make_timestamp
    421. SELECT make_timestamp(2014, 12, 28, 6, 30, 45.887);
    422. +-------------------------------------------+
    423. |make_timestamp(2014, 12, 28, 6, 30, 45.887)|
    424. +-------------------------------------------+
    425. | 2014-12-28 06:30:...|
    426. +-------------------------------------------+
    427. SELECT make_timestamp(2014, 12, 28, 6, 30, 45.887, 'CET');
    428. +------------------------------------------------+
    429. |make_timestamp(2014, 12, 28, 6, 30, 45.887, CET)|
    430. +------------------------------------------------+
    431. | 2014-12-28 05:30:...|
    432. +------------------------------------------------+
    433. SELECT make_timestamp(2019, 6, 30, 23, 59, 60);
    434. +---------------------------------------+
    435. |make_timestamp(2019, 6, 30, 23, 59, 60)|
    436. +---------------------------------------+
    437. | 2019-07-01 00:00:00|
    438. +---------------------------------------+
    439. SELECT make_timestamp(2019, 6, 30, 23, 59, 1);
    440. +--------------------------------------+
    441. |make_timestamp(2019, 6, 30, 23, 59, 1)|
    442. +--------------------------------------+
    443. | 2019-06-30 23:59:01|
    444. +--------------------------------------+
    445. SELECT make_timestamp(null, 7, 22, 15, 30, 0);
    446. +--------------------------------------+
    447. |make_timestamp(NULL, 7, 22, 15, 30, 0)|
    448. +--------------------------------------+
    449. | null|
    450. +--------------------------------------+
    451. -- make_timestamp_ltz
    452. SELECT make_timestamp_ltz(2014, 12, 28, 6, 30, 45.887);
    453. +-----------------------------------------------+
    454. |make_timestamp_ltz(2014, 12, 28, 6, 30, 45.887)|
    455. +-----------------------------------------------+
    456. | 2014-12-28 06:30:...|
    457. +-----------------------------------------------+
    458. SELECT make_timestamp_ltz(2014, 12, 28, 6, 30, 45.887, 'CET');
    459. +----------------------------------------------------+
    460. |make_timestamp_ltz(2014, 12, 28, 6, 30, 45.887, CET)|
    461. +----------------------------------------------------+
    462. | 2014-12-28 05:30:...|
    463. +----------------------------------------------------+
    464. SELECT make_timestamp_ltz(2019, 6, 30, 23, 59, 60);
    465. +-------------------------------------------+
    466. |make_timestamp_ltz(2019, 6, 30, 23, 59, 60)|
    467. +-------------------------------------------+
    468. | 2019-07-01 00:00:00|
    469. +-------------------------------------------+
    470. SELECT make_timestamp_ltz(null, 7, 22, 15, 30, 0);
    471. +------------------------------------------+
    472. |make_timestamp_ltz(NULL, 7, 22, 15, 30, 0)|
    473. +------------------------------------------+
    474. | null|
    475. +------------------------------------------+
    476. -- make_timestamp_ntz
    477. SELECT make_timestamp_ntz(2014, 12, 28, 6, 30, 45.887);
    478. +-----------------------------------------------+
    479. |make_timestamp_ntz(2014, 12, 28, 6, 30, 45.887)|
    480. +-----------------------------------------------+
    481. | 2014-12-28 06:30:...|
    482. +-----------------------------------------------+
    483. SELECT make_timestamp_ntz(2019, 6, 30, 23, 59, 60);
    484. +-------------------------------------------+
    485. |make_timestamp_ntz(2019, 6, 30, 23, 59, 60)|
    486. +-------------------------------------------+
    487. | 2019-07-01 00:00:00|
    488. +-------------------------------------------+
    489. SELECT make_timestamp_ntz(null, 7, 22, 15, 30, 0);
    490. +------------------------------------------+
    491. |make_timestamp_ntz(NULL, 7, 22, 15, 30, 0)|
    492. +------------------------------------------+
    493. | null|
    494. +------------------------------------------+
    495. -- make_ym_interval
    496. SELECT make_ym_interval(1, 2);
    497. +----------------------+
    498. |make_ym_interval(1, 2)|
    499. +----------------------+
    500. | INTERVAL '1-2' YE...|
    501. +----------------------+
    502. SELECT make_ym_interval(1, 0);
    503. +----------------------+
    504. |make_ym_interval(1, 0)|
    505. +----------------------+
    506. | INTERVAL '1-0' YE...|
    507. +----------------------+
    508. SELECT make_ym_interval(-1, 1);
    509. +-----------------------+
    510. |make_ym_interval(-1, 1)|
    511. +-----------------------+
    512. | INTERVAL '-0-11' ...|
    513. +-----------------------+
    514. SELECT make_ym_interval(2);
    515. +----------------------+
    516. |make_ym_interval(2, 0)|
    517. +----------------------+
    518. | INTERVAL '2-0' YE...|
    519. +----------------------+
    520. -- minute
    521. SELECT minute('2009-07-30 12:58:59');
    522. +---------------------------+
    523. |minute(2009-07-30 12:58:59)|
    524. +---------------------------+
    525. | 58|
    526. +---------------------------+
    527. -- month
    528. SELECT month('2016-07-30');
    529. +-----------------+
    530. |month(2016-07-30)|
    531. +-----------------+
    532. | 7|
    533. +-----------------+
    534. -- months_between
    535. SELECT months_between('1997-02-28 10:30:00', '1996-10-30');
    536. +-----------------------------------------------------+
    537. |months_between(1997-02-28 10:30:00, 1996-10-30, true)|
    538. +-----------------------------------------------------+
    539. | 3.94959677|
    540. +-----------------------------------------------------+
    541. SELECT months_between('1997-02-28 10:30:00', '1996-10-30', false);
    542. +------------------------------------------------------+
    543. |months_between(1997-02-28 10:30:00, 1996-10-30, false)|
    544. +------------------------------------------------------+
    545. | 3.9495967741935485|
    546. +------------------------------------------------------+
    547. -- next_day
    548. SELECT next_day('2015-01-14', 'TU');
    549. +------------------------+
    550. |next_day(2015-01-14, TU)|
    551. +------------------------+
    552. | 2015-01-20|
    553. +------------------------+
    554. -- now
    555. SELECT now();
    556. +--------------------+
    557. | now()|
    558. +--------------------+
    559. |2023-04-14 18:42:...|
    560. +--------------------+
    561. -- quarter
    562. SELECT quarter('2016-08-31');
    563. +-------------------+
    564. |quarter(2016-08-31)|
    565. +-------------------+
    566. | 3|
    567. +-------------------+
    568. -- second
    569. SELECT second('2009-07-30 12:58:59');
    570. +---------------------------+
    571. |second(2009-07-30 12:58:59)|
    572. +---------------------------+
    573. | 59|
    574. +---------------------------+
    575. -- session_window
    576. SELECT a, session_window.start, session_window.end, count(*) as cnt FROM VALUES ('A1', '2021-01-01 00:00:00'), ('A1', '2021-01-01 00:04:30'), ('A1', '2021-01-01 00:10:00'), ('A2', '2021-01-01 00:01:00') AS tab(a, b) GROUP by a, session_window(b, '5 minutes') ORDER BY a, start;
    577. +---+-------------------+-------------------+---+
    578. | a| start| end|cnt|
    579. +---+-------------------+-------------------+---+
    580. | A1|2021-01-01 00:00:00|2021-01-01 00:09:30| 2|
    581. | A1|2021-01-01 00:10:00|2021-01-01 00:15:00| 1|
    582. | A2|2021-01-01 00:01:00|2021-01-01 00:06:00| 1|
    583. +---+-------------------+-------------------+---+
    584. SELECT a, session_window.start, session_window.end, count(*) as cnt FROM VALUES ('A1', '2021-01-01 00:00:00'), ('A1', '2021-01-01 00:04:30'), ('A1', '2021-01-01 00:10:00'), ('A2', '2021-01-01 00:01:00'), ('A2', '2021-01-01 00:04:30') AS tab(a, b) GROUP by a, session_window(b, CASE WHEN a = 'A1' THEN '5 minutes' WHEN a = 'A2' THEN '1 minute' ELSE '10 minutes' END) ORDER BY a, start;
    585. +---+-------------------+-------------------+---+
    586. | a| start| end|cnt|
    587. +---+-------------------+-------------------+---+
    588. | A1|2021-01-01 00:00:00|2021-01-01 00:09:30| 2|
    589. | A1|2021-01-01 00:10:00|2021-01-01 00:15:00| 1|
    590. | A2|2021-01-01 00:01:00|2021-01-01 00:02:00| 1|
    591. | A2|2021-01-01 00:04:30|2021-01-01 00:05:30| 1|
    592. +---+-------------------+-------------------+---+
    593. -- timestamp_micros
    594. SELECT timestamp_micros(1230219000123123);
    595. +----------------------------------+
    596. |timestamp_micros(1230219000123123)|
    597. +----------------------------------+
    598. +----------------------------------+
    599. -- timestamp_millis
    600. SELECT timestamp_millis(1230219000123);
    601. +-------------------------------+
    602. |timestamp_millis(1230219000123)|
    603. +-------------------------------+
    604. | 2008-12-25 15:30:...|
    605. +-------------------------------+
    606. -- timestamp_seconds
    607. SELECT timestamp_seconds(1230219000);
    608. +-----------------------------+
    609. |timestamp_seconds(1230219000)|
    610. +-----------------------------+
    611. | 2008-12-25 15:30:00|
    612. +-----------------------------+
    613. SELECT timestamp_seconds(1230219000.123);
    614. +---------------------------------+
    615. |timestamp_seconds(1230219000.123)|
    616. +---------------------------------+
    617. | 2008-12-25 15:30:...|
    618. +---------------------------------+
    619. -- to_date
    620. SELECT to_date('2009-07-30 04:17:52');
    621. +----------------------------+
    622. |to_date(2009-07-30 04:17:52)|
    623. +----------------------------+
    624. | 2009-07-30|
    625. +----------------------------+
    626. SELECT to_date('2016-12-31', 'yyyy-MM-dd');
    627. +-------------------------------+
    628. |to_date(2016-12-31, yyyy-MM-dd)|
    629. +-------------------------------+
    630. | 2016-12-31|
    631. +-------------------------------+
    632. -- to_timestamp
    633. SELECT to_timestamp('2016-12-31 00:12:00');
    634. +---------------------------------+
    635. |to_timestamp(2016-12-31 00:12:00)|
    636. +---------------------------------+
    637. | 2016-12-31 00:12:00|
    638. SELECT to_timestamp('2016-12-31', 'yyyy-MM-dd');
    639. +------------------------------------+
    640. |to_timestamp(2016-12-31, yyyy-MM-dd)|
    641. +------------------------------------+
    642. | 2016-12-31 00:00:00|
    643. +------------------------------------+
    644. -- to_timestamp_ltz
    645. SELECT to_timestamp_ltz('2016-12-31 00:12:00');
    646. +-------------------------------------+
    647. |to_timestamp_ltz(2016-12-31 00:12:00)|
    648. +-------------------------------------+
    649. | 2016-12-31 00:12:00|
    650. +-------------------------------------+
    651. SELECT to_timestamp_ltz('2016-12-31', 'yyyy-MM-dd');
    652. +----------------------------------------+
    653. |to_timestamp_ltz(2016-12-31, yyyy-MM-dd)|
    654. +----------------------------------------+
    655. | 2016-12-31 00:00:00|
    656. +----------------------------------------+
    657. -- to_timestamp_ntz
    658. SELECT to_timestamp_ntz('2016-12-31 00:12:00');
    659. +-------------------------------------+
    660. |to_timestamp_ntz(2016-12-31 00:12:00)|
    661. +-------------------------------------+
    662. | 2016-12-31 00:12:00|
    663. +-------------------------------------+
    664. SELECT to_timestamp_ntz('2016-12-31', 'yyyy-MM-dd');
    665. +----------------------------------------+
    666. |to_timestamp_ntz(2016-12-31, yyyy-MM-dd)|
    667. +----------------------------------------+
    668. | 2016-12-31 00:00:00|
    669. +----------------------------------------+
    670. -- to_unix_timestamp
    671. SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');
    672. +-----------------------------------------+
    673. |to_unix_timestamp(2016-04-08, yyyy-MM-dd)|
    674. +-----------------------------------------+
    675. | 1460073600|
    676. +-----------------------------------------+
    677. -- to_utc_timestamp
    678. SELECT to_utc_timestamp('2016-08-31', 'Asia/Seoul');
    679. +----------------------------------------+
    680. |to_utc_timestamp(2016-08-31, Asia/Seoul)|
    681. +----------------------------------------+
    682. | 2016-08-30 15:00:00|
    683. +----------------------------------------+
    684. -- trunc
    685. SELECT trunc('2019-08-04', 'week');
    686. +-----------------------+
    687. |trunc(2019-08-04, week)|
    688. +-----------------------+
    689. | 2019-07-29|
    690. +-----------------------+
    691. SELECT trunc('2019-08-04', 'quarter');
    692. +--------------------------+
    693. |trunc(2019-08-04, quarter)|
    694. +--------------------------+
    695. | 2019-07-01|
    696. +--------------------------+
    697. SELECT trunc('2009-02-12', 'MM');
    698. +---------------------+
    699. |trunc(2009-02-12, MM)|
    700. +---------------------+
    701. | 2009-02-01|
    702. +---------------------+
    703. SELECT trunc('2015-10-27', 'YEAR');
    704. +-----------------------+
    705. |trunc(2015-10-27, YEAR)|
    706. +-----------------------+
    707. | 2015-01-01|
    708. +-----------------------+
    709. -- try_to_timestamp
    710. SELECT try_to_timestamp('2016-12-31 00:12:00');
    711. +-------------------------------------+
    712. |try_to_timestamp(2016-12-31 00:12:00)|
    713. +-------------------------------------+
    714. | 2016-12-31 00:12:00|
    715. +-------------------------------------+
    716. SELECT try_to_timestamp('2016-12-31', 'yyyy-MM-dd');
    717. +----------------------------------------+
    718. |try_to_timestamp(2016-12-31, yyyy-MM-dd)|
    719. +----------------------------------------+
    720. | 2016-12-31 00:00:00|
    721. +----------------------------------------+
    722. SELECT try_to_timestamp('foo', 'yyyy-MM-dd');
    723. +---------------------------------+
    724. |try_to_timestamp(foo, yyyy-MM-dd)|
    725. +---------------------------------+
    726. | null|
    727. +---------------------------------+
    728. -- unix_date
    729. SELECT unix_date(DATE("1970-01-02"));
    730. +---------------------+
    731. |unix_date(1970-01-02)|
    732. +---------------------+
    733. | 1|
    734. +---------------------+
    735. -- unix_micros
    736. SELECT unix_micros(TIMESTAMP('1970-01-01 00:00:01Z'));
    737. +---------------------------------+
    738. |unix_micros(1970-01-01 00:00:01Z)|
    739. +---------------------------------+
    740. | 1000000|
    741. +---------------------------------+
    742. -- unix_millis
    743. SELECT unix_millis(TIMESTAMP('1970-01-01 00:00:01Z'));
    744. +---------------------------------+
    745. |unix_millis(1970-01-01 00:00:01Z)|
    746. +---------------------------------+
    747. | 1000|
    748. +---------------------------------+
    749. -- unix_seconds
    750. SELECT unix_seconds(TIMESTAMP('1970-01-01 00:00:01Z'));
    751. +----------------------------------+
    752. |unix_seconds(1970-01-01 00:00:01Z)|
    753. +----------------------------------+
    754. | 1|
    755. +----------------------------------+
    756. -- unix_timestamp
    757. SELECT unix_timestamp();
    758. +--------------------------------------------------------+
    759. |unix_timestamp(current_timestamp(), yyyy-MM-dd HH:mm:ss)|
    760. +--------------------------------------------------------+
    761. | 1681497755|
    762. +--------------------------------------------------------+
    763. SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');
    764. +--------------------------------------+
    765. |unix_timestamp(2016-04-08, yyyy-MM-dd)|
    766. +--------------------------------------+
    767. | 1460073600|
    768. +--------------------------------------+
    769. -- weekday
    770. SELECT weekday('2009-07-30');
    771. +-------------------+
    772. |weekday(2009-07-30)|
    773. +-------------------+
    774. | 3|
    775. +-------------------+
    776. -- weekofyear
    777. SELECT weekofyear('2008-02-20');
    778. +----------------------+
    779. |weekofyear(2008-02-20)|
    780. +----------------------+
    781. | 8|
    782. +----------------------+
    783. -- window
    784. SELECT a, window.start, window.end, count(*) as cnt FROM VALUES ('A1', '2021-01-01 00:00:00'), ('A1', '2021-01-01 00:04:30'), ('A1', '2021-01-01 00:06:00'), ('A2', '2021-01-01 00:01:00') AS tab(a, b) GROUP by a, window(b, '5 minutes') ORDER BY a, start;
    785. +---+-------------------+-------------------+---+
    786. | a| start| end|cnt|
    787. +---+-------------------+-------------------+---+
    788. | A1|2021-01-01 00:00:00|2021-01-01 00:05:00| 2|
    789. | A1|2021-01-01 00:05:00|2021-01-01 00:10:00| 1|
    790. | A2|2021-01-01 00:00:00|2021-01-01 00:05:00| 1|
    791. +---+-------------------+-------------------+---+
    792. SELECT a, window.start, window.end, count(*) as cnt FROM VALUES ('A1', '2021-01-01 00:00:00'), ('A1', '2021-01-01 00:04:30'), ('A1', '2021-01-01 00:06:00'), ('A2', '2021-01-01 00:01:00') AS tab(a, b) GROUP by a, window(b, '10 minutes', '5 minutes') ORDER BY a, start;
    793. +---+-------------------+-------------------+---+
    794. | a| start| end|cnt|
    795. +---+-------------------+-------------------+---+
    796. | A1|2020-12-31 23:55:00|2021-01-01 00:05:00| 2|
    797. | A1|2021-01-01 00:00:00|2021-01-01 00:10:00| 3|
    798. | A1|2021-01-01 00:05:00|2021-01-01 00:15:00| 1|
    799. | A2|2020-12-31 23:55:00|2021-01-01 00:05:00| 1|
    800. | A2|2021-01-01 00:00:00|2021-01-01 00:10:00| 1|
    801. +---+-------------------+-------------------+---+
    802. -- window_time
    803. SELECT a, window.start as start, window.end as end, window_time(window), cnt FROM (SELECT a, window, count(*) as cnt FROM VALUES ('A1', '2021-01-01 00:00:00'), ('A1', '2021-01-01 00:04:30'), ('A1', '2021-01-01 00:06:00'), ('A2', '2021-01-01 00:01:00') AS tab(a, b) GROUP by a, window(b, '5 minutes') ORDER BY a, window.start);
    804. +---+-------------------+-------------------+--------------------+---+
    805. | a| start| end| window_time(window)|cnt|
    806. +---+-------------------+-------------------+--------------------+---+
    807. | A1|2021-01-01 00:00:00|2021-01-01 00:05:00|2021-01-01 00:04:...| 2|
    808. | A1|2021-01-01 00:05:00|2021-01-01 00:10:00|2021-01-01 00:09:...| 1|
    809. | A2|2021-01-01 00:00:00|2021-01-01 00:05:00|2021-01-01 00:04:...| 1|
    810. +---+-------------------+-------------------+--------------------+---+
    811. -- year
    812. SELECT year('2016-07-30');
    813. +----------------+
    814. |year(2016-07-30)|
    815. +----------------+
    816. | 2016|
    817. +----------------+

    Examples

    Mathematical Functions

    FunctionDescription
    expr1 % expr2Returns the remainder after expr1/expr2.
    expr1 expr2Returns expr1expr2.
    expr1 + expr2Returns expr1+expr2.
    expr1 - expr2Returns expr1-expr2.
    expr1 / expr2Returns expr1/expr2. It always performs floating point division.
    abs(expr)Returns the absolute value of the numeric or interval value.
    acos(expr)Returns the inverse cosine (a.k.a. arc cosine) of expr, as if computed by java.lang.Math.acos.
    acosh(expr)Returns inverse hyperbolic cosine of expr.
    asin(expr)Returns the inverse sine (a.k.a. arc sine) the arc sin of expr, as if computed by java.lang.Math.asin.
    asinh(expr)Returns inverse hyperbolic sine of expr.
    atan(expr)Returns the inverse tangent (a.k.a. arc tangent) of expr, as if computed by java.lang.Math.atan
    atan2(exprY, exprX)Returns the angle in radians between the positive x-axis of a plane and the point given by the coordinates (exprX, exprY), as if computed by java.lang.Math.atan2.
    atanh(expr)Returns inverse hyperbolic tangent of expr.
    bin(expr)Returns the string representation of the long value expr represented in binary.
    bround(expr, d)Returns expr rounded to d decimal places using HALF_EVEN rounding mode.
    cbrt(expr)Returns the cube root of expr.
    ceil(expr[, scale])Returns the smallest number after rounding up that is not smaller than expr. An optional scale parameter can be specified to control the rounding behavior.
    ceiling(expr[, scale])Returns the smallest number after rounding up that is not smaller than expr. An optional scale parameter can be specified to control the rounding behavior.
    conv(num, from_base, to_base)Convert num from from_base to to_base.
    cos(expr)Returns the cosine of expr, as if computed by java.lang.Math.cos.
    cosh(expr)Returns the hyperbolic cosine of expr, as if computed by java.lang.Math.cosh.
    cot(expr)Returns the cotangent of expr, as if computed by 1/java.lang.Math.tan.
    csc(expr)Returns the cosecant of expr, as if computed by 1/java.lang.Math.sin.
    degrees(expr)Converts radians to degrees.
    expr1 div expr2Divide expr1 by expr2. It returns NULL if an operand is NULL or expr2 is 0. The result is casted to long.
    e()Returns Euler’s number, e.
    exp(expr)Returns e to the power of expr.
    expm1(expr) - Returns exp(expr)1.
    factorial(expr)Returns the factorial of expr. expr is [0..20]. Otherwise, null.
    floor(expr[, scale])Returns the largest number after rounding down that is not greater than expr. An optional scale parameter can be specified to control the rounding behavior.
    greatest(expr, …)Returns the greatest value of all parameters, skipping null values.
    hex(expr)Converts expr to hexadecimal.
    hypot(expr1, expr2)Returns sqrt(expr12 + expr22).
    least(expr, …)Returns the least value of all parameters, skipping null values.
    ln(expr)Returns the natural logarithm (base e) of expr.
    log(base, expr)Returns the logarithm of expr with base.
    log10(expr)Returns the logarithm of expr with base 10.
    log1p(expr)Returns log(1 + expr).
    log2(expr)Returns the logarithm of expr with base 2.
    expr1 mod expr2Returns the remainder after expr1/expr2.
    negative(expr)Returns the negated value of expr.
    pi()Returns pi.
    pmod(expr1, expr2)Returns the positive value of expr1 mod expr2.
    positive(expr)Returns the value of expr.
    pow(expr1, expr2)Raises expr1 to the power of expr2.
    power(expr1, expr2)Raises expr1 to the power of expr2.
    radians(expr)Converts degrees to radians.
    rand([seed])Returns a random value with independent and identically distributed (i.i.d.) uniformly distributed values in [0, 1).
    randn([seed])Returns a random value with independent and identically distributed (i.i.d.) values drawn from the standard normal distribution.
    random([seed])Returns a random value with independent and identically distributed (i.i.d.) uniformly distributed values in [0, 1).
    rint(expr)Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
    round(expr, d)Returns expr rounded to d decimal places using HALF_UP rounding mode.
    sec(expr)Returns the secant of expr, as if computed by 1/java.lang.Math.cos.
    shiftleft(base, expr)Bitwise left shift.
    sign(expr)Returns -1.0, 0.0 or 1.0 as expr is negative, 0 or positive.
    signum(expr)Returns -1.0, 0.0 or 1.0 as expr is negative, 0 or positive.
    sin(expr)Returns the sine of expr, as if computed by java.lang.Math.sin.
    sinh(expr)Returns hyperbolic sine of expr, as if computed by java.lang.Math.sinh.
    sqrt(expr)Returns the square root of expr.
    tan(expr)Returns the tangent of expr, as if computed by java.lang.Math.tan.
    tanh(expr)Returns the hyperbolic tangent of expr, as if computed by java.lang.Math.tanh.
    try_add(expr1, expr2)Returns the sum of expr1and expr2 and the result is null on overflow. The acceptable input types are the same with the + operator.
    try_divide(dividend, divisor)Returns dividend/divisor. It always performs floating point division. Its result is always null if expr2 is 0. dividend must be a numeric or an interval. divisor must be a numeric.
    try_multiply(expr1, expr2)Returns expr1expr2 and the result is null on overflow. The acceptable input types are the same with the `operator.</td></tr><tr><td>try_subtract(expr1, expr2)</td><td>Returnsexpr1-expr2and the result is null on overflow. The acceptable input types are the same with the-operator.</td></tr><tr><td>unhex(expr)</td><td>Converts hexadecimalexprto binary.</td></tr><tr><td>width_bucket(value, min_value, max_value, num_bucket)</td><td>Returns the bucket number to whichvaluewould be assigned in an equiwidth histogram withnum_bucketbuckets, in the rangemin_valuetomax_value`.”

    Examples

    1. -- %
    2. SELECT 2 % 1.8;
    3. +---------+
    4. |(2 % 1.8)|
    5. +---------+
    6. | 0.2|
    7. +---------+
    8. SELECT MOD(2, 1.8);
    9. +-----------+
    10. |mod(2, 1.8)|
    11. +-----------+
    12. | 0.2|
    13. +-----------+
    14. -- *
    15. SELECT 2 * 3;
    16. +-------+
    17. |(2 * 3)|
    18. +-------+
    19. | 6|
    20. +-------+
    21. -- +
    22. SELECT 1 + 2;
    23. +-------+
    24. |(1 + 2)|
    25. +-------+
    26. | 3|
    27. +-------+
    28. -- -
    29. SELECT 2 - 1;
    30. +-------+
    31. |(2 - 1)|
    32. +-------+
    33. | 1|
    34. +-------+
    35. -- /
    36. SELECT 3 / 2;
    37. +-------+
    38. |(3 / 2)|
    39. +-------+
    40. | 1.5|
    41. +-------+
    42. SELECT 2L / 2L;
    43. +-------+
    44. |(2 / 2)|
    45. +-------+
    46. | 1.0|
    47. +-------+
    48. -- abs
    49. SELECT abs(-1);
    50. +-------+
    51. |abs(-1)|
    52. +-------+
    53. | 1|
    54. +-------+
    55. SELECT abs(INTERVAL -'1-1' YEAR TO MONTH);
    56. +----------------------------------+
    57. |abs(INTERVAL '-1-1' YEAR TO MONTH)|
    58. +----------------------------------+
    59. | INTERVAL '1-1' YE...|
    60. +----------------------------------+
    61. -- acos
    62. SELECT acos(1);
    63. +-------+
    64. |ACOS(1)|
    65. +-------+
    66. | 0.0|
    67. +-------+
    68. SELECT acos(2);
    69. +-------+
    70. |ACOS(2)|
    71. +-------+
    72. | NaN|
    73. +-------+
    74. -- acosh
    75. SELECT acosh(1);
    76. +--------+
    77. |ACOSH(1)|
    78. +--------+
    79. | 0.0|
    80. +--------+
    81. SELECT acosh(0);
    82. +--------+
    83. |ACOSH(0)|
    84. +--------+
    85. | NaN|
    86. +--------+
    87. -- asin
    88. SELECT asin(0);
    89. +-------+
    90. |ASIN(0)|
    91. +-------+
    92. | 0.0|
    93. +-------+
    94. SELECT asin(2);
    95. +-------+
    96. |ASIN(2)|
    97. +-------+
    98. | NaN|
    99. +-------+
    100. -- asinh
    101. SELECT asinh(0);
    102. +--------+
    103. |ASINH(0)|
    104. +--------+
    105. | 0.0|
    106. +--------+
    107. -- atan
    108. SELECT atan(0);
    109. +-------+
    110. |ATAN(0)|
    111. +-------+
    112. | 0.0|
    113. +-------+
    114. -- atan2
    115. SELECT atan2(0, 0);
    116. +-----------+
    117. |ATAN2(0, 0)|
    118. +-----------+
    119. | 0.0|
    120. +-----------+
    121. -- atanh
    122. SELECT atanh(0);
    123. +--------+
    124. |ATANH(0)|
    125. +--------+
    126. | 0.0|
    127. +--------+
    128. SELECT atanh(2);
    129. +--------+
    130. |ATANH(2)|
    131. +--------+
    132. | NaN|
    133. +--------+
    134. -- bin
    135. SELECT bin(13);
    136. +-------+
    137. |bin(13)|
    138. +-------+
    139. | 1101|
    140. +-------+
    141. SELECT bin(-13);
    142. +--------------------+
    143. | bin(-13)|
    144. +--------------------+
    145. |11111111111111111...|
    146. +--------------------+
    147. SELECT bin(13.3);
    148. +---------+
    149. |bin(13.3)|
    150. +---------+
    151. | 1101|
    152. +---------+
    153. -- bround
    154. SELECT bround(2.5, 0);
    155. +--------------+
    156. |bround(2.5, 0)|
    157. +--------------+
    158. | 2|
    159. +--------------+
    160. SELECT bround(25, -1);
    161. +--------------+
    162. |bround(25, -1)|
    163. +--------------+
    164. | 20|
    165. +--------------+
    166. -- cbrt
    167. SELECT cbrt(27.0);
    168. +----------+
    169. |CBRT(27.0)|
    170. +----------+
    171. | 3.0|
    172. +----------+
    173. -- ceil
    174. SELECT ceil(-0.1);
    175. +----------+
    176. |CEIL(-0.1)|
    177. +----------+
    178. | 0|
    179. +----------+
    180. SELECT ceil(5);
    181. +-------+
    182. |CEIL(5)|
    183. +-------+
    184. | 5|
    185. +-------+
    186. SELECT ceil(3.1411, 3);
    187. +---------------+
    188. |ceil(3.1411, 3)|
    189. +---------------+
    190. | 3.142|
    191. +---------------+
    192. SELECT ceil(3.1411, -3);
    193. +----------------+
    194. |ceil(3.1411, -3)|
    195. +----------------+
    196. | 1000|
    197. +----------------+
    198. -- ceiling
    199. SELECT ceiling(-0.1);
    200. +-------------+
    201. |ceiling(-0.1)|
    202. +-------------+
    203. | 0|
    204. +-------------+
    205. SELECT ceiling(5);
    206. +----------+
    207. |ceiling(5)|
    208. +----------+
    209. | 5|
    210. +----------+
    211. SELECT ceiling(3.1411, 3);
    212. +------------------+
    213. |ceiling(3.1411, 3)|
    214. +------------------+
    215. | 3.142|
    216. +------------------+
    217. SELECT ceiling(3.1411, -3);
    218. +-------------------+
    219. |ceiling(3.1411, -3)|
    220. +-------------------+
    221. | 1000|
    222. +-------------------+
    223. -- conv
    224. SELECT conv('100', 2, 10);
    225. +----------------+
    226. |conv(100, 2, 10)|
    227. +----------------+
    228. | 4|
    229. +----------------+
    230. SELECT conv(-10, 16, -10);
    231. +------------------+
    232. |conv(-10, 16, -10)|
    233. +------------------+
    234. | -16|
    235. +------------------+
    236. -- cos
    237. SELECT cos(0);
    238. +------+
    239. |COS(0)|
    240. +------+
    241. | 1.0|
    242. +------+
    243. -- cosh
    244. SELECT cosh(0);
    245. +-------+
    246. |COSH(0)|
    247. +-------+
    248. | 1.0|
    249. +-------+
    250. -- cot
    251. SELECT cot(1);
    252. +------------------+
    253. | COT(1)|
    254. +------------------+
    255. |0.6420926159343306|
    256. +------------------+
    257. -- csc
    258. SELECT csc(1);
    259. +------------------+
    260. | CSC(1)|
    261. +------------------+
    262. |1.1883951057781212|
    263. +------------------+
    264. -- degrees
    265. SELECT degrees(3.141592653589793);
    266. +--------------------------+
    267. |DEGREES(3.141592653589793)|
    268. +--------------------------+
    269. | 180.0|
    270. +--------------------------+
    271. -- div
    272. SELECT 3 div 2;
    273. +---------+
    274. |(3 div 2)|
    275. +---------+
    276. | 1|
    277. +---------+
    278. SELECT INTERVAL '1-1' YEAR TO MONTH div INTERVAL '-1' MONTH;
    279. +------------------------------------------------------+
    280. |(INTERVAL '1-1' YEAR TO MONTH div INTERVAL '-1' MONTH)|
    281. +------------------------------------------------------+
    282. | -13|
    283. +------------------------------------------------------+
    284. -- e
    285. SELECT e();
    286. +-----------------+
    287. | E()|
    288. +-----------------+
    289. |2.718281828459045|
    290. +-----------------+
    291. -- exp
    292. SELECT exp(0);
    293. +------+
    294. |EXP(0)|
    295. +------+
    296. | 1.0|
    297. +------+
    298. -- expm1
    299. SELECT expm1(0);
    300. +--------+
    301. |EXPM1(0)|
    302. +--------+
    303. | 0.0|
    304. +--------+
    305. -- factorial
    306. SELECT factorial(5);
    307. +------------+
    308. |factorial(5)|
    309. +------------+
    310. | 120|
    311. +------------+
    312. -- floor
    313. SELECT floor(-0.1);
    314. +-----------+
    315. |FLOOR(-0.1)|
    316. +-----------+
    317. | -1|
    318. +-----------+
    319. SELECT floor(5);
    320. +--------+
    321. |FLOOR(5)|
    322. +--------+
    323. | 5|
    324. +--------+
    325. SELECT floor(3.1411, 3);
    326. +----------------+
    327. |floor(3.1411, 3)|
    328. +----------------+
    329. | 3.141|
    330. +----------------+
    331. SELECT floor(3.1411, -3);
    332. +-----------------+
    333. |floor(3.1411, -3)|
    334. +-----------------+
    335. | 0|
    336. +-----------------+
    337. -- greatest
    338. SELECT greatest(10, 9, 2, 4, 3);
    339. +------------------------+
    340. |greatest(10, 9, 2, 4, 3)|
    341. +------------------------+
    342. | 10|
    343. +------------------------+
    344. -- hex
    345. SELECT hex(17);
    346. +-------+
    347. |hex(17)|
    348. +-------+
    349. | 11|
    350. +-------+
    351. SELECT hex('Spark SQL');
    352. +------------------+
    353. | hex(Spark SQL)|
    354. +------------------+
    355. |537061726B2053514C|
    356. +------------------+
    357. -- hypot
    358. SELECT hypot(3, 4);
    359. +-----------+
    360. |HYPOT(3, 4)|
    361. +-----------+
    362. | 5.0|
    363. +-----------+
    364. -- least
    365. SELECT least(10, 9, 2, 4, 3);
    366. +---------------------+
    367. |least(10, 9, 2, 4, 3)|
    368. +---------------------+
    369. | 2|
    370. +---------------------+
    371. -- ln
    372. SELECT ln(1);
    373. +-----+
    374. |ln(1)|
    375. +-----+
    376. | 0.0|
    377. +-----+
    378. -- log
    379. SELECT log(10, 100);
    380. +------------+
    381. |LOG(10, 100)|
    382. +------------+
    383. | 2.0|
    384. +------------+
    385. -- log10
    386. SELECT log10(10);
    387. +---------+
    388. |LOG10(10)|
    389. +---------+
    390. | 1.0|
    391. +---------+
    392. -- log1p
    393. SELECT log1p(0);
    394. +--------+
    395. |LOG1P(0)|
    396. +--------+
    397. | 0.0|
    398. +--------+
    399. -- log2
    400. SELECT log2(2);
    401. +-------+
    402. |LOG2(2)|
    403. +-------+
    404. | 1.0|
    405. +-------+
    406. -- mod
    407. SELECT 2 % 1.8;
    408. +---------+
    409. |(2 % 1.8)|
    410. +---------+
    411. | 0.2|
    412. +---------+
    413. SELECT MOD(2, 1.8);
    414. +-----------+
    415. |mod(2, 1.8)|
    416. +-----------+
    417. | 0.2|
    418. +-----------+
    419. -- negative
    420. SELECT negative(1);
    421. +-----------+
    422. |negative(1)|
    423. +-----------+
    424. | -1|
    425. +-----------+
    426. -- pi
    427. SELECT pi();
    428. +-----------------+
    429. | PI()|
    430. +-----------------+
    431. |3.141592653589793|
    432. +-----------------+
    433. -- pmod
    434. SELECT pmod(10, 3);
    435. +-----------+
    436. |pmod(10, 3)|
    437. +-----------+
    438. | 1|
    439. +-----------+
    440. SELECT pmod(-10, 3);
    441. +------------+
    442. |pmod(-10, 3)|
    443. +------------+
    444. | 2|
    445. +------------+
    446. -- positive
    447. SELECT positive(1);
    448. +-----+
    449. |(+ 1)|
    450. +-----+
    451. | 1|
    452. +-----+
    453. -- pow
    454. SELECT pow(2, 3);
    455. +---------+
    456. |pow(2, 3)|
    457. +---------+
    458. | 8.0|
    459. +---------+
    460. -- power
    461. SELECT power(2, 3);
    462. +-----------+
    463. |POWER(2, 3)|
    464. +-----------+
    465. | 8.0|
    466. +-----------+
    467. -- radians
    468. SELECT radians(180);
    469. +-----------------+
    470. | RADIANS(180)|
    471. +-----------------+
    472. |3.141592653589793|
    473. +-----------------+
    474. -- rand
    475. SELECT rand();
    476. +-------------------+
    477. | rand()|
    478. +-------------------+
    479. |0.15928853362060658|
    480. +-------------------+
    481. SELECT rand(0);
    482. +------------------+
    483. | rand(0)|
    484. +------------------+
    485. |0.7604953758285915|
    486. +------------------+
    487. SELECT rand(null);
    488. +------------------+
    489. | rand(NULL)|
    490. +------------------+
    491. |0.7604953758285915|
    492. +------------------+
    493. -- randn
    494. SELECT randn();
    495. +--------------------+
    496. | randn()|
    497. +--------------------+
    498. |0.050851021833532115|
    499. +--------------------+
    500. SELECT randn(0);
    501. +------------------+
    502. | randn(0)|
    503. +------------------+
    504. |1.6034991609278433|
    505. +------------------+
    506. SELECT randn(null);
    507. +------------------+
    508. | randn(NULL)|
    509. +------------------+
    510. |1.6034991609278433|
    511. +------------------+
    512. -- random
    513. SELECT random();
    514. +------------------+
    515. | rand()|
    516. +------------------+
    517. |0.5860400306778014|
    518. +------------------+
    519. SELECT random(0);
    520. +------------------+
    521. | rand(0)|
    522. +------------------+
    523. |0.7604953758285915|
    524. +------------------+
    525. SELECT random(null);
    526. +------------------+
    527. | rand(NULL)|
    528. +------------------+
    529. |0.7604953758285915|
    530. +------------------+
    531. -- rint
    532. SELECT rint(12.3456);
    533. +-------------+
    534. |rint(12.3456)|
    535. +-------------+
    536. | 12.0|
    537. +-------------+
    538. -- round
    539. SELECT round(2.5, 0);
    540. +-------------+
    541. |round(2.5, 0)|
    542. +-------------+
    543. | 3|
    544. +-------------+
    545. -- sec
    546. SELECT sec(0);
    547. +------+
    548. |SEC(0)|
    549. +------+
    550. | 1.0|
    551. +------+
    552. -- shiftleft
    553. SELECT shiftleft(2, 1);
    554. +---------------+
    555. |shiftleft(2, 1)|
    556. +---------------+
    557. | 4|
    558. +---------------+
    559. -- sign
    560. SELECT sign(40);
    561. +--------+
    562. |sign(40)|
    563. +--------+
    564. | 1.0|
    565. +--------+
    566. SELECT sign(INTERVAL -'100' YEAR);
    567. +--------------------------+
    568. |sign(INTERVAL '-100' YEAR)|
    569. +--------------------------+
    570. | -1.0|
    571. +--------------------------+
    572. -- signum
    573. SELECT signum(40);
    574. +----------+
    575. |SIGNUM(40)|
    576. +----------+
    577. | 1.0|
    578. +----------+
    579. SELECT signum(INTERVAL -'100' YEAR);
    580. +----------------------------+
    581. |SIGNUM(INTERVAL '-100' YEAR)|
    582. +----------------------------+
    583. | -1.0|
    584. +----------------------------+
    585. -- sin
    586. SELECT sin(0);
    587. +------+
    588. |SIN(0)|
    589. +------+
    590. | 0.0|
    591. +------+
    592. -- sinh
    593. SELECT sinh(0);
    594. +-------+
    595. |SINH(0)|
    596. +-------+
    597. | 0.0|
    598. +-------+
    599. -- sqrt
    600. SELECT sqrt(4);
    601. +-------+
    602. |SQRT(4)|
    603. +-------+
    604. | 2.0|
    605. +-------+
    606. -- tan
    607. SELECT tan(0);
    608. +------+
    609. |TAN(0)|
    610. +------+
    611. | 0.0|
    612. +------+
    613. -- tanh
    614. SELECT tanh(0);
    615. +-------+
    616. |TANH(0)|
    617. +-------+
    618. | 0.0|
    619. +-------+
    620. -- try_add
    621. SELECT try_add(1, 2);
    622. +-------------+
    623. |try_add(1, 2)|
    624. +-------------+
    625. | 3|
    626. +-------------+
    627. SELECT try_add(2147483647, 1);
    628. +----------------------+
    629. |try_add(2147483647, 1)|
    630. +----------------------+
    631. | null|
    632. +----------------------+
    633. SELECT try_add(date'2021-01-01', 1);
    634. +-----------------------------+
    635. |try_add(DATE '2021-01-01', 1)|
    636. +-----------------------------+
    637. | 2021-01-02|
    638. +-----------------------------+
    639. SELECT try_add(date'2021-01-01', interval 1 year);
    640. +---------------------------------------------+
    641. |try_add(DATE '2021-01-01', INTERVAL '1' YEAR)|
    642. +---------------------------------------------+
    643. | 2022-01-01|
    644. +---------------------------------------------+
    645. SELECT try_add(timestamp'2021-01-01 00:00:00', interval 1 day);
    646. +----------------------------------------------------------+
    647. |try_add(TIMESTAMP '2021-01-01 00:00:00', INTERVAL '1' DAY)|
    648. +----------------------------------------------------------+
    649. | 2021-01-02 00:00:00|
    650. +----------------------------------------------------------+
    651. SELECT try_add(interval 1 year, interval 2 year);
    652. +---------------------------------------------+
    653. |try_add(INTERVAL '1' YEAR, INTERVAL '2' YEAR)|
    654. +---------------------------------------------+
    655. | INTERVAL '3' YEAR|
    656. +---------------------------------------------+
    657. -- try_divide
    658. SELECT try_divide(3, 2);
    659. +----------------+
    660. |try_divide(3, 2)|
    661. +----------------+
    662. | 1.5|
    663. +----------------+
    664. SELECT try_divide(2L, 2L);
    665. +----------------+
    666. |try_divide(2, 2)|
    667. +----------------+
    668. | 1.0|
    669. +----------------+
    670. SELECT try_divide(1, 0);
    671. +----------------+
    672. |try_divide(1, 0)|
    673. +----------------+
    674. | null|
    675. +----------------+
    676. SELECT try_divide(interval 2 month, 2);
    677. +---------------------------------+
    678. |try_divide(INTERVAL '2' MONTH, 2)|
    679. +---------------------------------+
    680. | INTERVAL '0-1' YE...|
    681. +---------------------------------+
    682. SELECT try_divide(interval 2 month, 0);
    683. +---------------------------------+
    684. |try_divide(INTERVAL '2' MONTH, 0)|
    685. +---------------------------------+
    686. | null|
    687. +---------------------------------+
    688. -- try_multiply
    689. SELECT try_multiply(2, 3);
    690. +------------------+
    691. |try_multiply(2, 3)|
    692. +------------------+
    693. | 6|
    694. +------------------+
    695. SELECT try_multiply(-2147483648, 10);
    696. +-----------------------------+
    697. |try_multiply(-2147483648, 10)|
    698. +-----------------------------+
    699. | null|
    700. +-----------------------------+
    701. SELECT try_multiply(interval 2 year, 3);
    702. +----------------------------------+
    703. |try_multiply(INTERVAL '2' YEAR, 3)|
    704. +----------------------------------+
    705. | INTERVAL '6-0' YE...|
    706. +----------------------------------+
    707. -- try_subtract
    708. SELECT try_subtract(2, 1);
    709. +------------------+
    710. |try_subtract(2, 1)|
    711. +------------------+
    712. | 1|
    713. +------------------+
    714. SELECT try_subtract(-2147483648, 1);
    715. +----------------------------+
    716. |try_subtract(-2147483648, 1)|
    717. +----------------------------+
    718. | null|
    719. +----------------------------+
    720. SELECT try_subtract(date'2021-01-02', 1);
    721. +----------------------------------+
    722. |try_subtract(DATE '2021-01-02', 1)|
    723. +----------------------------------+
    724. | 2021-01-01|
    725. +----------------------------------+
    726. SELECT try_subtract(date'2021-01-01', interval 1 year);
    727. +--------------------------------------------------+
    728. |try_subtract(DATE '2021-01-01', INTERVAL '1' YEAR)|
    729. +--------------------------------------------------+
    730. | 2020-01-01|
    731. +--------------------------------------------------+
    732. SELECT try_subtract(timestamp'2021-01-02 00:00:00', interval 1 day);
    733. +---------------------------------------------------------------+
    734. |try_subtract(TIMESTAMP '2021-01-02 00:00:00', INTERVAL '1' DAY)|
    735. +---------------------------------------------------------------+
    736. | 2021-01-01 00:00:00|
    737. +---------------------------------------------------------------+
    738. SELECT try_subtract(interval 2 year, interval 1 year);
    739. +--------------------------------------------------+
    740. |try_subtract(INTERVAL '2' YEAR, INTERVAL '1' YEAR)|
    741. +--------------------------------------------------+
    742. | INTERVAL '1' YEAR|
    743. +--------------------------------------------------+
    744. -- unhex
    745. SELECT decode(unhex('537061726B2053514C'), 'UTF-8');
    746. +----------------------------------------+
    747. |decode(unhex(537061726B2053514C), UTF-8)|
    748. +----------------------------------------+
    749. | Spark SQL|
    750. +----------------------------------------+
    751. -- width_bucket
    752. SELECT width_bucket(5.3, 0.2, 10.6, 5);
    753. +-------------------------------+
    754. |width_bucket(5.3, 0.2, 10.6, 5)|
    755. +-------------------------------+
    756. | 3|
    757. +-------------------------------+
    758. SELECT width_bucket(-2.1, 1.3, 3.4, 3);
    759. +-------------------------------+
    760. |width_bucket(-2.1, 1.3, 3.4, 3)|
    761. +-------------------------------+
    762. | 0|
    763. +-------------------------------+
    764. SELECT width_bucket(8.1, 0.0, 5.7, 4);
    765. +------------------------------+
    766. |width_bucket(8.1, 0.0, 5.7, 4)|
    767. +------------------------------+
    768. | 5|
    769. +------------------------------+
    770. SELECT width_bucket(-0.9, 5.2, 0.5, 2);
    771. +-------------------------------+
    772. |width_bucket(-0.9, 5.2, 0.5, 2)|
    773. +-------------------------------+
    774. | 3|
    775. +-------------------------------+
    776. SELECT width_bucket(INTERVAL '0' YEAR, INTERVAL '0' YEAR, INTERVAL '10' YEAR, 10);
    777. +--------------------------------------------------------------------------+
    778. |width_bucket(INTERVAL '0' YEAR, INTERVAL '0' YEAR, INTERVAL '10' YEAR, 10)|
    779. +--------------------------------------------------------------------------+
    780. | 1|
    781. +--------------------------------------------------------------------------+
    782. SELECT width_bucket(INTERVAL '1' YEAR, INTERVAL '0' YEAR, INTERVAL '10' YEAR, 10);
    783. +--------------------------------------------------------------------------+
    784. |width_bucket(INTERVAL '1' YEAR, INTERVAL '0' YEAR, INTERVAL '10' YEAR, 10)|
    785. +--------------------------------------------------------------------------+
    786. | 2|
    787. +--------------------------------------------------------------------------+
    788. SELECT width_bucket(INTERVAL '0' DAY, INTERVAL '0' DAY, INTERVAL '10' DAY, 10);
    789. +-----------------------------------------------------------------------+
    790. |width_bucket(INTERVAL '0' DAY, INTERVAL '0' DAY, INTERVAL '10' DAY, 10)|
    791. +-----------------------------------------------------------------------+
    792. | 1|
    793. +-----------------------------------------------------------------------+
    794. SELECT width_bucket(INTERVAL '1' DAY, INTERVAL '0' DAY, INTERVAL '10' DAY, 10);
    795. +-----------------------------------------------------------------------+
    796. |width_bucket(INTERVAL '1' DAY, INTERVAL '0' DAY, INTERVAL '10' DAY, 10)|
    797. +-----------------------------------------------------------------------+
    798. | 2|
    799. +-----------------------------------------------------------------------+

    String Functions

    FunctionDescription
    ascii(str)Returns the numeric value of the first character of str.
    base64(bin)Converts the argument from a binary bin to a base 64 string.
    bit_length(expr)Returns the bit length of string data or number of bits of binary data.
    btrim(str)Removes the leading and trailing space characters from str.
    btrim(str, trimStr)Remove the leading and trailing trimStr characters from str.
    char(expr)Returns the ASCII character having the binary equivalent to expr. If n is larger than 256 the result is equivalent to chr(n % 256)
    char_length(expr)Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.
    character_length(expr)Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.
    chr(expr)Returns the ASCII character having the binary equivalent to expr. If n is larger than 256 the result is equivalent to chr(n % 256)
    concat_ws(sep[, str | array(str)]+)Returns the concatenation of the strings separated by sep.
    contains(left, right)Returns a boolean. The value is True if right is found inside left. Returns NULL if either input expression is NULL. Otherwise, returns False. Both left or right must be of STRING or BINARY type.
    decode(bin, charset)Decodes the first argument using the second argument character set.
    decode(expr, search, result [, search, result ] … [, default])Compares expr to each search value in order. If expr is equal to a search value, decode returns the corresponding result. If no match is found, then it returns default. If default is omitted, it returns null.
    elt(n, input1, input2, …)Returns the n-th input, e.g., returns input2 when n is 2. The function returns NULL if the index exceeds the length of the array and spark.sql.ansi.enabled is set to false. If spark.sql.ansi.enabled is set to true, it throws ArrayIndexOutOfBoundsException for invalid indices.
    encode(str, charset)Encodes the first argument using the second argument character set.
    endswith(left, right)Returns a boolean. The value is True if left ends with right. Returns NULL if either input expression is NULL. Otherwise, returns False. Both left or right must be of STRING or BINARY type.
    find_in_set(str, str_array)Returns the index (1-based) of the given string (str) in the comma-delimited list (str_array). Returns 0, if the string was not found or if the given string (str) contains a comma.
    format_number(expr1, expr2)Formats the number expr1 like ‘#,###,###.##’, rounded to expr2 decimal places. If expr2 is 0, the result has no decimal point or fractional part. expr2 also accept a user specified format. This is supposed to function like MySQL’s FORMAT.
    format_string(strfmt, obj, …)Returns a formatted string from printf-style format strings.
    initcap(str)Returns str with the first letter of each word in uppercase. All other letters are in lowercase. Words are delimited by white space.
    instr(str, substr)Returns the (1-based) index of the first occurrence of substr in str.
    lcase(str)Returns str with all characters changed to lowercase.
    left(str, len)Returns the leftmost len(len can be string type) characters from the string str,if len is less or equal than 0 the result is an empty string.
    len(expr)Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.
    length(expr)Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.
    levenshtein(str1, str2)Returns the Levenshtein distance between the two given strings.
    locate(substr, str[, pos])Returns the position of the first occurrence of substr in str after position pos. The given pos and return value are 1-based.
    lower(str)Returns str with all characters changed to lowercase.
    lpad(str, len[, pad])Returns str, left-padded with pad to a length of len. If str is longer than len, the return value is shortened to len characters or bytes. If pad is not specified, str will be padded to the left with space characters if it is a character string, and with zeros if it is a byte sequence.
    ltrim(str)Removes the leading space characters from str.
    mask(input[, upperChar, lowerChar, digitChar, otherChar])masks the given string value. The function replaces characters with ‘X’ or ‘x’, and numbers with ‘n’. This can be useful for creating copies of tables with sensitive information removed.
    octet_length(expr)Returns the byte length of string data or number of bytes of binary data.
    overlay(input, replace, pos[, len])Replace input with replace that starts at pos and is of length len.
    position(substr, str[, pos])Returns the position of the first occurrence of substr in str after position pos. The given pos and return value are 1-based.
    printf(strfmt, obj, …)Returns a formatted string from printf-style format strings.
    regexp_count(str, regexp)Returns a count of the number of times that the regular expression pattern regexp is matched in the string str.
    regexp_extract(str, regexp[, idx])Extract the first string in the str that match the regexp expression and corresponding to the regex group index.
    regexp_extract_all(str, regexp[, idx])Extract all strings in the str that match the regexp expression and corresponding to the regex group index.
    regexp_instr(str, regexp)Searches a string for a regular expression and returns an integer that indicates the beginning position of the matched substring. Positions are 1-based, not 0-based. If no match is found, returns 0.
    regexp_replace(str, regexp, rep[, position])Replaces all substrings of str that match regexp with rep.
    regexp_substr(str, regexp)Returns the substring that matches the regular expression regexp within the string str. If the regular expression is not found, the result is null.
    repeat(str, n)Returns the string which repeats the given string value n times.
    replace(str, search[, replace])Replaces all occurrences of search with replace.
    right(str, len)Returns the rightmost len(len can be string type) characters from the string str,if len is less or equal than 0 the result is an empty string.
    rpad(str, len[, pad])Returns str, right-padded with pad to a length of len. If str is longer than len, the return value is shortened to len characters. If pad is not specified, str will be padded to the right with space characters if it is a character string, and with zeros if it is a binary string.
    rtrim(str)Removes the trailing space characters from str.
    sentences(str[, lang, country])Splits str into an array of array of words.
    soundex(str)Returns Soundex code of the string.
    space(n)Returns a string consisting of n spaces.
    split(str, regex, limit)Splits str around occurrences that match regex and returns an array with a length of at most limit
    split_part(str, delimiter, partNum)Splits str by delimiter and return requested part of the split (1-based). If any input is null, returns null. if partNum is out of range of split parts, returns empty string. If partNum is 0, throws an error. If partNum is negative, the parts are counted backward from the end of the string. If the delimiter is an empty string, the str is not split.
    startswith(left, right)Returns a boolean. The value is True if left starts with right. Returns NULL if either input expression is NULL. Otherwise, returns False. Both left or right must be of STRING or BINARY type.
    substr(str, pos[, len])Returns the substring of str that starts at pos and is of length len, or the slice of byte array that starts at pos and is of length len.
    substr(str FROM pos[ FOR len]])Returns the substring of str that starts at pos and is of length len, or the slice of byte array that starts at pos and is of length len.
    substring(str, pos[, len])Returns the substring of str that starts at pos and is of length len, or the slice of byte array that starts at pos and is of length len.
    substring(str FROM pos[ FOR len]])Returns the substring of str that starts at pos and is of length len, or the slice of byte array that starts at pos and is of length len.
    substring_index(str, delim, count)Returns the substring from str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. The function substring_index performs a case-sensitive match when searching for delim.
    to_binary(str[, fmt])Converts the input str to a binary value based on the supplied fmt. fmt can be a case-insensitive string literal of “hex”, “utf-8”, “utf8”, or “base64”. By default, the binary format for conversion is “hex” if fmt is omitted. The function returns NULL if at least one of the input parameters is NULL.
    to_char(numberExpr, formatExpr)Convert numberExpr to a string based on the formatExpr. Throws an exception if the conversion fails. The format can consist of the following characters, case insensitive: ‘0’ or ‘9’: Specifies an expected digit between 0 and 9. A sequence of 0 or 9 in the format string matches a sequence of digits in the input value, generating a result string of the same length as the corresponding sequence in the format string. The result string is left-padded with zeros if the 0/9 sequence comprises more digits than the matching part of the decimal value, starts with 0, and is before the decimal point. Otherwise, it is padded with spaces. ‘.’ or ‘D’: Specifies the position of the decimal point (optional, only allowed once). ‘,’ or ‘G’: Specifies the position of the grouping (thousands) separator (,). There must be a 0 or 9 to the left and right of each grouping separator. ‘$’: Specifies the location of the $ currency sign. This character may only be specified once. ‘S’ or ‘MI’: Specifies the position of a ‘-‘ or ‘+’ sign (optional, only allowed once at the beginning or end of the format string). Note that ‘S’ prints ‘+’ for positive values but ‘MI’ prints a space. ‘PR’: Only allowed at the end of the format string; specifies that the result string will be wrapped by angle brackets if the input value is negative. (‘<1>’).
    to_number(expr, fmt)Convert string ‘expr’ to a number based on the string format ‘fmt’. Throws an exception if the conversion fails. The format can consist of the following characters, case insensitive: ‘0’ or ‘9’: Specifies an expected digit between 0 and 9. A sequence of 0 or 9 in the format string matches a sequence of digits in the input string. If the 0/9 sequence starts with 0 and is before the decimal point, it can only match a digit sequence of the same size. Otherwise, if the sequence starts with 9 or is after the decimal point, it can match a digit sequence that has the same or smaller size. ‘.’ or ‘D’: Specifies the position of the decimal point (optional, only allowed once). ‘,’ or ‘G’: Specifies the position of the grouping (thousands) separator (,). There must be a 0 or 9 to the left and right of each grouping separator. ‘expr’ must match the grouping separator relevant for the size of the number. ‘$’: Specifies the location of the $ currency sign. This character may only be specified once. ‘S’ or ‘MI’: Specifies the position of a ‘-‘ or ‘+’ sign (optional, only allowed once at the beginning or end of the format string). Note that ‘S’ allows ‘-‘ but ‘MI’ does not. ‘PR’: Only allowed at the end of the format string; specifies that ‘expr’ indicates a negative number with wrapping angled brackets. (‘<1>’).
    translate(input, from, to)Translates the input string by replacing the characters present in the from string with the corresponding characters in the to string.
    trim(str)Removes the leading and trailing space characters from str.
    trim(BOTH FROM str)Removes the leading and trailing space characters from str.
    trim(LEADING FROM str)Removes the leading space characters from str.
    trim(TRAILING FROM str)Removes the trailing space characters from str.
    trim(trimStr FROM str)Remove the leading and trailing trimStr characters from str.
    trim(BOTH trimStr FROM str)Remove the leading and trailing trimStr characters from str.
    trim(LEADING trimStr FROM str)Remove the leading trimStr characters from str.
    trim(TRAILING trimStr FROM str)Remove the trailing trimStr characters from str.
    try_to_binary(str[, fmt])This is a special version of to_binary that performs the same operation, but returns a NULL value instead of raising an error if the conversion cannot be performed.
    try_to_number(expr, fmt)Convert string ‘expr’ to a number based on the string format fmt. Returns NULL if the string ‘expr’ does not match the expected format. The format follows the same semantics as the to_number function.
    ucase(str)Returns str with all characters changed to uppercase.
    unbase64(str)Converts the argument from a base 64 string str to a binary.
    upper(str)Returns str with all characters changed to uppercase.

    Examples

    1. -- ascii
    2. SELECT ascii('222');
    3. +----------+
    4. +----------+
    5. | 50|
    6. +----------+
    7. SELECT ascii(2);
    8. +--------+
    9. |ascii(2)|
    10. +--------+
    11. | 50|
    12. +--------+
    13. -- base64
    14. SELECT base64('Spark SQL');
    15. +-----------------+
    16. |base64(Spark SQL)|
    17. +-----------------+
    18. | U3BhcmsgU1FM|
    19. +-----------------+
    20. SELECT base64(x'537061726b2053514c');
    21. +-----------------------------+
    22. |base64(X'537061726B2053514C')|
    23. +-----------------------------+
    24. | U3BhcmsgU1FM|
    25. +-----------------------------+
    26. -- bit_length
    27. SELECT bit_length('Spark SQL');
    28. +---------------------+
    29. |bit_length(Spark SQL)|
    30. +---------------------+
    31. | 72|
    32. +---------------------+
    33. SELECT bit_length(x'537061726b2053514c');
    34. |bit_length(X'537061726B2053514C')|
    35. +---------------------------------+
    36. | 72|
    37. +---------------------------------+
    38. -- btrim
    39. SELECT btrim(' SparkSQL ');
    40. +----------------------+
    41. |btrim( SparkSQL )|
    42. +----------------------+
    43. | SparkSQL|
    44. +----------------------+
    45. SELECT btrim(encode(' SparkSQL ', 'utf-8'));
    46. +-------------------------------------+
    47. |btrim(encode( SparkSQL , utf-8))|
    48. +-------------------------------------+
    49. | SparkSQL|
    50. +-------------------------------------+
    51. SELECT btrim('SSparkSQLS', 'SL');
    52. +---------------------+
    53. |btrim(SSparkSQLS, SL)|
    54. +---------------------+
    55. | parkSQ|
    56. +---------------------+
    57. SELECT btrim(encode('SSparkSQLS', 'utf-8'), encode('SL', 'utf-8'));
    58. +---------------------------------------------------+
    59. |btrim(encode(SSparkSQLS, utf-8), encode(SL, utf-8))|
    60. +---------------------------------------------------+
    61. | parkSQ|
    62. +---------------------------------------------------+
    63. -- char
    64. SELECT char(65);
    65. +--------+
    66. |char(65)|
    67. +--------+
    68. | A|
    69. +--------+
    70. -- char_length
    71. SELECT char_length('Spark SQL ');
    72. +-----------------------+
    73. |char_length(Spark SQL )|
    74. +-----------------------+
    75. | 10|
    76. +-----------------------+
    77. SELECT char_length(x'537061726b2053514c');
    78. +----------------------------------+
    79. |char_length(X'537061726B2053514C')|
    80. +----------------------------------+
    81. | 9|
    82. +----------------------------------+
    83. SELECT CHAR_LENGTH('Spark SQL ');
    84. +-----------------------+
    85. |char_length(Spark SQL )|
    86. +-----------------------+
    87. | 10|
    88. +-----------------------+
    89. SELECT CHARACTER_LENGTH('Spark SQL ');
    90. +----------------------------+
    91. |character_length(Spark SQL )|
    92. +----------------------------+
    93. | 10|
    94. +----------------------------+
    95. -- character_length
    96. SELECT character_length('Spark SQL ');
    97. +----------------------------+
    98. |character_length(Spark SQL )|
    99. +----------------------------+
    100. | 10|
    101. +----------------------------+
    102. SELECT character_length(x'537061726b2053514c');
    103. +---------------------------------------+
    104. |character_length(X'537061726B2053514C')|
    105. +---------------------------------------+
    106. | 9|
    107. +---------------------------------------+
    108. SELECT CHAR_LENGTH('Spark SQL ');
    109. +-----------------------+
    110. |char_length(Spark SQL )|
    111. +-----------------------+
    112. | 10|
    113. +-----------------------+
    114. SELECT CHARACTER_LENGTH('Spark SQL ');
    115. +----------------------------+
    116. |character_length(Spark SQL )|
    117. +----------------------------+
    118. | 10|
    119. +----------------------------+
    120. -- chr
    121. SELECT chr(65);
    122. +-------+
    123. |chr(65)|
    124. +-------+
    125. | A|
    126. +-------+
    127. -- concat_ws
    128. SELECT concat_ws(' ', 'Spark', 'SQL');
    129. +------------------------+
    130. |concat_ws( , Spark, SQL)|
    131. +------------------------+
    132. | Spark SQL|
    133. +------------------------+
    134. SELECT concat_ws('s');
    135. +------------+
    136. |concat_ws(s)|
    137. +------------+
    138. | |
    139. +------------+
    140. -- contains
    141. SELECT contains('Spark SQL', 'Spark');
    142. +--------------------------+
    143. |contains(Spark SQL, Spark)|
    144. +--------------------------+
    145. | true|
    146. +--------------------------+
    147. SELECT contains('Spark SQL', 'SPARK');
    148. +--------------------------+
    149. |contains(Spark SQL, SPARK)|
    150. +--------------------------+
    151. | false|
    152. +--------------------------+
    153. SELECT contains('Spark SQL', null);
    154. +-------------------------+
    155. |contains(Spark SQL, NULL)|
    156. +-------------------------+
    157. | null|
    158. +-------------------------+
    159. SELECT contains(x'537061726b2053514c', x'537061726b');
    160. +----------------------------------------------+
    161. |contains(X'537061726B2053514C', X'537061726B')|
    162. +----------------------------------------------+
    163. | true|
    164. +----------------------------------------------+
    165. -- decode
    166. SELECT decode(encode('abc', 'utf-8'), 'utf-8');
    167. +---------------------------------+
    168. |decode(encode(abc, utf-8), utf-8)|
    169. +---------------------------------+
    170. | abc|
    171. +---------------------------------+
    172. SELECT decode(2, 1, 'Southlake', 2, 'San Francisco', 3, 'New Jersey', 4, 'Seattle', 'Non domestic');
    173. +----------------------------------------------------------------------------------+
    174. |decode(2, 1, Southlake, 2, San Francisco, 3, New Jersey, 4, Seattle, Non domestic)|
    175. +----------------------------------------------------------------------------------+
    176. | San Francisco|
    177. +----------------------------------------------------------------------------------+
    178. SELECT decode(6, 1, 'Southlake', 2, 'San Francisco', 3, 'New Jersey', 4, 'Seattle', 'Non domestic');
    179. +----------------------------------------------------------------------------------+
    180. |decode(6, 1, Southlake, 2, San Francisco, 3, New Jersey, 4, Seattle, Non domestic)|
    181. +----------------------------------------------------------------------------------+
    182. | Non domestic|
    183. +----------------------------------------------------------------------------------+
    184. SELECT decode(6, 1, 'Southlake', 2, 'San Francisco', 3, 'New Jersey', 4, 'Seattle');
    185. +--------------------------------------------------------------------+
    186. |decode(6, 1, Southlake, 2, San Francisco, 3, New Jersey, 4, Seattle)|
    187. +--------------------------------------------------------------------+
    188. | null|
    189. +--------------------------------------------------------------------+
    190. SELECT decode(null, 6, 'Spark', NULL, 'SQL', 4, 'rocks');
    191. +-------------------------------------------+
    192. |decode(NULL, 6, Spark, NULL, SQL, 4, rocks)|
    193. +-------------------------------------------+
    194. | SQL|
    195. +-------------------------------------------+
    196. -- elt
    197. SELECT elt(1, 'scala', 'java');
    198. +-------------------+
    199. |elt(1, scala, java)|
    200. +-------------------+
    201. | scala|
    202. +-------------------+
    203. SELECT elt(2, 'a', 1);
    204. +------------+
    205. |elt(2, a, 1)|
    206. +------------+
    207. | 1|
    208. +------------+
    209. -- encode
    210. SELECT encode('abc', 'utf-8');
    211. +------------------+
    212. |encode(abc, utf-8)|
    213. +------------------+
    214. | [61 62 63]|
    215. +------------------+
    216. -- endswith
    217. SELECT endswith('Spark SQL', 'SQL');
    218. +------------------------+
    219. |endswith(Spark SQL, SQL)|
    220. +------------------------+
    221. | true|
    222. +------------------------+
    223. SELECT endswith('Spark SQL', 'Spark');
    224. +--------------------------+
    225. |endswith(Spark SQL, Spark)|
    226. +--------------------------+
    227. | false|
    228. +--------------------------+
    229. SELECT endswith('Spark SQL', null);
    230. +-------------------------+
    231. |endswith(Spark SQL, NULL)|
    232. +-------------------------+
    233. | null|
    234. +-------------------------+
    235. SELECT endswith(x'537061726b2053514c', x'537061726b');
    236. +----------------------------------------------+
    237. |endswith(X'537061726B2053514C', X'537061726B')|
    238. +----------------------------------------------+
    239. | false|
    240. +----------------------------------------------+
    241. SELECT endswith(x'537061726b2053514c', x'53514c');
    242. +------------------------------------------+
    243. |endswith(X'537061726B2053514C', X'53514C')|
    244. +------------------------------------------+
    245. | true|
    246. +------------------------------------------+
    247. -- find_in_set
    248. SELECT find_in_set('ab','abc,b,ab,c,def');
    249. +-------------------------------+
    250. |find_in_set(ab, abc,b,ab,c,def)|
    251. +-------------------------------+
    252. | 3|
    253. +-------------------------------+
    254. -- format_number
    255. SELECT format_number(12332.123456, 4);
    256. +------------------------------+
    257. |format_number(12332.123456, 4)|
    258. +------------------------------+
    259. | 12,332.1235|
    260. +------------------------------+
    261. SELECT format_number(12332.123456, '##################.###');
    262. +---------------------------------------------------+
    263. |format_number(12332.123456, ##################.###)|
    264. +---------------------------------------------------+
    265. | 12332.123|
    266. +---------------------------------------------------+
    267. -- format_string
    268. SELECT format_string("Hello World %d %s", 100, "days");
    269. +-------------------------------------------+
    270. |format_string(Hello World %d %s, 100, days)|
    271. +-------------------------------------------+
    272. | Hello World 100 days|
    273. +-------------------------------------------+
    274. -- initcap
    275. SELECT initcap('sPark sql');
    276. +------------------+
    277. |initcap(sPark sql)|
    278. +------------------+
    279. | Spark Sql|
    280. +------------------+
    281. -- instr
    282. SELECT instr('SparkSQL', 'SQL');
    283. +--------------------+
    284. |instr(SparkSQL, SQL)|
    285. +--------------------+
    286. | 6|
    287. +--------------------+
    288. -- lcase
    289. SELECT lcase('SparkSql');
    290. +---------------+
    291. |lcase(SparkSql)|
    292. +---------------+
    293. | sparksql|
    294. +---------------+
    295. -- left
    296. SELECT left('Spark SQL', 3);
    297. +------------------+
    298. |left(Spark SQL, 3)|
    299. +------------------+
    300. | Spa|
    301. +------------------+
    302. SELECT left(encode('Spark SQL', 'utf-8'), 3);
    303. +---------------------------------+
    304. |left(encode(Spark SQL, utf-8), 3)|
    305. +---------------------------------+
    306. | [53 70 61]|
    307. +---------------------------------+
    308. -- len
    309. SELECT len('Spark SQL ');
    310. +---------------+
    311. |len(Spark SQL )|
    312. +---------------+
    313. | 10|
    314. +---------------+
    315. SELECT len(x'537061726b2053514c');
    316. +--------------------------+
    317. |len(X'537061726B2053514C')|
    318. +--------------------------+
    319. | 9|
    320. +--------------------------+
    321. SELECT CHAR_LENGTH('Spark SQL ');
    322. +-----------------------+
    323. |char_length(Spark SQL )|
    324. +-----------------------+
    325. | 10|
    326. +-----------------------+
    327. SELECT CHARACTER_LENGTH('Spark SQL ');
    328. +----------------------------+
    329. |character_length(Spark SQL )|
    330. +----------------------------+
    331. | 10|
    332. +----------------------------+
    333. -- length
    334. SELECT length('Spark SQL ');
    335. +------------------+
    336. |length(Spark SQL )|
    337. +------------------+
    338. | 10|
    339. +------------------+
    340. SELECT length(x'537061726b2053514c');
    341. +-----------------------------+
    342. |length(X'537061726B2053514C')|
    343. +-----------------------------+
    344. | 9|
    345. +-----------------------------+
    346. SELECT CHAR_LENGTH('Spark SQL ');
    347. +-----------------------+
    348. |char_length(Spark SQL )|
    349. +-----------------------+
    350. | 10|
    351. +-----------------------+
    352. SELECT CHARACTER_LENGTH('Spark SQL ');
    353. +----------------------------+
    354. |character_length(Spark SQL )|
    355. +----------------------------+
    356. | 10|
    357. +----------------------------+
    358. -- levenshtein
    359. SELECT levenshtein('kitten', 'sitting');
    360. +----------------------------+
    361. |levenshtein(kitten, sitting)|
    362. +----------------------------+
    363. | 3|
    364. +----------------------------+
    365. -- locate
    366. SELECT locate('bar', 'foobarbar');
    367. +-------------------------+
    368. |locate(bar, foobarbar, 1)|
    369. +-------------------------+
    370. | 4|
    371. +-------------------------+
    372. SELECT locate('bar', 'foobarbar', 5);
    373. +-------------------------+
    374. |locate(bar, foobarbar, 5)|
    375. +-------------------------+
    376. | 7|
    377. +-------------------------+
    378. SELECT POSITION('bar' IN 'foobarbar');
    379. +-------------------------+
    380. |locate(bar, foobarbar, 1)|
    381. +-------------------------+
    382. | 4|
    383. +-------------------------+
    384. -- lower
    385. SELECT lower('SparkSql');
    386. +---------------+
    387. |lower(SparkSql)|
    388. +---------------+
    389. | sparksql|
    390. +---------------+
    391. -- lpad
    392. SELECT lpad('hi', 5, '??');
    393. +---------------+
    394. |lpad(hi, 5, ??)|
    395. +---------------+
    396. | ???hi|
    397. +---------------+
    398. SELECT lpad('hi', 1, '??');
    399. +---------------+
    400. |lpad(hi, 1, ??)|
    401. +---------------+
    402. | h|
    403. +---------------+
    404. SELECT lpad('hi', 5);
    405. +--------------+
    406. |lpad(hi, 5, )|
    407. +--------------+
    408. | hi|
    409. +--------------+
    410. SELECT hex(lpad(unhex('aabb'), 5));
    411. +--------------------------------+
    412. |hex(lpad(unhex(aabb), 5, X'00'))|
    413. +--------------------------------+
    414. | 000000AABB|
    415. +--------------------------------+
    416. SELECT hex(lpad(unhex('aabb'), 5, unhex('1122')));
    417. +--------------------------------------+
    418. |hex(lpad(unhex(aabb), 5, unhex(1122)))|
    419. +--------------------------------------+
    420. | 112211AABB|
    421. +--------------------------------------+
    422. -- ltrim
    423. SELECT ltrim(' SparkSQL ');
    424. +----------------------+
    425. |ltrim( SparkSQL )|
    426. +----------------------+
    427. | SparkSQL |
    428. +----------------------+
    429. -- mask
    430. SELECT mask('abcd-EFGH-8765-4321');
    431. +----------------------------------------+
    432. |mask(abcd-EFGH-8765-4321, X, x, n, NULL)|
    433. +----------------------------------------+
    434. | xxxx-XXXX-nnnn-nnnn|
    435. +----------------------------------------+
    436. SELECT mask('abcd-EFGH-8765-4321', 'Q');
    437. +----------------------------------------+
    438. |mask(abcd-EFGH-8765-4321, Q, x, n, NULL)|
    439. +----------------------------------------+
    440. | xxxx-QQQQ-nnnn-nnnn|
    441. +----------------------------------------+
    442. SELECT mask('AbCD123-@$#', 'Q', 'q');
    443. +--------------------------------+
    444. |mask(AbCD123-@$#, Q, q, n, NULL)|
    445. +--------------------------------+
    446. | QqQQnnn-@$#|
    447. +--------------------------------+
    448. SELECT mask('AbCD123-@$#');
    449. +--------------------------------+
    450. |mask(AbCD123-@$#, X, x, n, NULL)|
    451. +--------------------------------+
    452. | XxXXnnn-@$#|
    453. +--------------------------------+
    454. SELECT mask('AbCD123-@$#', 'Q');
    455. +--------------------------------+
    456. |mask(AbCD123-@$#, Q, x, n, NULL)|
    457. +--------------------------------+
    458. | QxQQnnn-@$#|
    459. +--------------------------------+
    460. SELECT mask('AbCD123-@$#', 'Q', 'q');
    461. +--------------------------------+
    462. |mask(AbCD123-@$#, Q, q, n, NULL)|
    463. +--------------------------------+
    464. | QqQQnnn-@$#|
    465. +--------------------------------+
    466. SELECT mask('AbCD123-@$#', 'Q', 'q', 'd');
    467. +--------------------------------+
    468. |mask(AbCD123-@$#, Q, q, d, NULL)|
    469. +--------------------------------+
    470. | QqQQddd-@$#|
    471. +--------------------------------+
    472. SELECT mask('AbCD123-@$#', 'Q', 'q', 'd', 'o');
    473. +-----------------------------+
    474. |mask(AbCD123-@$#, Q, q, d, o)|
    475. +-----------------------------+
    476. | QqQQdddoooo|
    477. +-----------------------------+
    478. SELECT mask('AbCD123-@$#', NULL, 'q', 'd', 'o');
    479. +--------------------------------+
    480. |mask(AbCD123-@$#, NULL, q, d, o)|
    481. +--------------------------------+
    482. | AqCDdddoooo|
    483. +--------------------------------+
    484. SELECT mask('AbCD123-@$#', NULL, NULL, 'd', 'o');
    485. +-----------------------------------+
    486. |mask(AbCD123-@$#, NULL, NULL, d, o)|
    487. +-----------------------------------+
    488. | AbCDdddoooo|
    489. +-----------------------------------+
    490. SELECT mask('AbCD123-@$#', NULL, NULL, NULL, 'o');
    491. +--------------------------------------+
    492. |mask(AbCD123-@$#, NULL, NULL, NULL, o)|
    493. +--------------------------------------+
    494. | AbCD123oooo|
    495. +--------------------------------------+
    496. SELECT mask(NULL, NULL, NULL, NULL, 'o');
    497. +-------------------------------+
    498. |mask(NULL, NULL, NULL, NULL, o)|
    499. +-------------------------------+
    500. | null|
    501. +-------------------------------+
    502. SELECT mask(NULL);
    503. +-------------------------+
    504. |mask(NULL, X, x, n, NULL)|
    505. +-------------------------+
    506. | null|
    507. +-------------------------+
    508. SELECT mask('AbCD123-@$#', NULL, NULL, NULL, NULL);
    509. +-----------------------------------------+
    510. |mask(AbCD123-@$#, NULL, NULL, NULL, NULL)|
    511. +-----------------------------------------+
    512. | AbCD123-@$#|
    513. +-----------------------------------------+
    514. -- octet_length
    515. SELECT octet_length('Spark SQL');
    516. +-----------------------+
    517. |octet_length(Spark SQL)|
    518. +-----------------------+
    519. | 9|
    520. +-----------------------+
    521. SELECT octet_length(x'537061726b2053514c');
    522. +-----------------------------------+
    523. |octet_length(X'537061726B2053514C')|
    524. +-----------------------------------+
    525. | 9|
    526. +-----------------------------------+
    527. -- overlay
    528. SELECT overlay('Spark SQL' PLACING '_' FROM 6);
    529. +----------------------------+
    530. |overlay(Spark SQL, _, 6, -1)|
    531. +----------------------------+
    532. | Spark_SQL|
    533. +----------------------------+
    534. SELECT overlay('Spark SQL' PLACING 'CORE' FROM 7);
    535. +-------------------------------+
    536. |overlay(Spark SQL, CORE, 7, -1)|
    537. +-------------------------------+
    538. | Spark CORE|
    539. +-------------------------------+
    540. SELECT overlay('Spark SQL' PLACING 'ANSI ' FROM 7 FOR 0);
    541. +-------------------------------+
    542. |overlay(Spark SQL, ANSI , 7, 0)|
    543. +-------------------------------+
    544. | Spark ANSI SQL|
    545. +-------------------------------+
    546. SELECT overlay('Spark SQL' PLACING 'tructured' FROM 2 FOR 4);
    547. +-----------------------------------+
    548. |overlay(Spark SQL, tructured, 2, 4)|
    549. +-----------------------------------+
    550. | Structured SQL|
    551. +-----------------------------------+
    552. SELECT overlay(encode('Spark SQL', 'utf-8') PLACING encode('_', 'utf-8') FROM 6);
    553. +----------------------------------------------------------+
    554. |overlay(encode(Spark SQL, utf-8), encode(_, utf-8), 6, -1)|
    555. +----------------------------------------------------------+
    556. | [53 70 61 72 6B 5...|
    557. +----------------------------------------------------------+
    558. SELECT overlay(encode('Spark SQL', 'utf-8') PLACING encode('CORE', 'utf-8') FROM 7);
    559. +-------------------------------------------------------------+
    560. |overlay(encode(Spark SQL, utf-8), encode(CORE, utf-8), 7, -1)|
    561. +-------------------------------------------------------------+
    562. | [53 70 61 72 6B 2...|
    563. +-------------------------------------------------------------+
    564. SELECT overlay(encode('Spark SQL', 'utf-8') PLACING encode('ANSI ', 'utf-8') FROM 7 FOR 0);
    565. +-------------------------------------------------------------+
    566. |overlay(encode(Spark SQL, utf-8), encode(ANSI , utf-8), 7, 0)|
    567. +-------------------------------------------------------------+
    568. | [53 70 61 72 6B 2...|
    569. +-------------------------------------------------------------+
    570. SELECT overlay(encode('Spark SQL', 'utf-8') PLACING encode('tructured', 'utf-8') FROM 2 FOR 4);
    571. +-----------------------------------------------------------------+
    572. |overlay(encode(Spark SQL, utf-8), encode(tructured, utf-8), 2, 4)|
    573. +-----------------------------------------------------------------+
    574. | [53 74 72 75 63 7...|
    575. +-----------------------------------------------------------------+
    576. -- position
    577. SELECT position('bar', 'foobarbar');
    578. +---------------------------+
    579. |position(bar, foobarbar, 1)|
    580. +---------------------------+
    581. | 4|
    582. +---------------------------+
    583. SELECT position('bar', 'foobarbar', 5);
    584. +---------------------------+
    585. |position(bar, foobarbar, 5)|
    586. +---------------------------+
    587. | 7|
    588. +---------------------------+
    589. SELECT POSITION('bar' IN 'foobarbar');
    590. +-------------------------+
    591. |locate(bar, foobarbar, 1)|
    592. +-------------------------+
    593. | 4|
    594. +-------------------------+
    595. -- printf
    596. SELECT printf("Hello World %d %s", 100, "days");
    597. +------------------------------------+
    598. |printf(Hello World %d %s, 100, days)|
    599. +------------------------------------+
    600. | Hello World 100 days|
    601. +------------------------------------+
    602. -- regexp_count
    603. SELECT regexp_count('Steven Jones and Stephen Smith are the best players', 'Ste(v|ph)en');
    604. +------------------------------------------------------------------------------+
    605. |regexp_count(Steven Jones and Stephen Smith are the best players, Ste(v|ph)en)|
    606. +------------------------------------------------------------------------------+
    607. | 2|
    608. +------------------------------------------------------------------------------+
    609. SELECT regexp_count('abcdefghijklmnopqrstuvwxyz', '[a-z]{3}');
    610. +--------------------------------------------------+
    611. |regexp_count(abcdefghijklmnopqrstuvwxyz, [a-z]{3})|
    612. +--------------------------------------------------+
    613. | 8|
    614. +--------------------------------------------------+
    615. -- regexp_extract
    616. SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1);
    617. +---------------------------------------+
    618. |regexp_extract(100-200, (\d+)-(\d+), 1)|
    619. +---------------------------------------+
    620. | 100|
    621. +---------------------------------------+
    622. -- regexp_extract_all
    623. SELECT regexp_extract_all('100-200, 300-400', '(\\d+)-(\\d+)', 1);
    624. +----------------------------------------------------+
    625. |regexp_extract_all(100-200, 300-400, (\d+)-(\d+), 1)|
    626. +----------------------------------------------------+
    627. | [100, 300]|
    628. +----------------------------------------------------+
    629. -- regexp_instr
    630. SELECT regexp_instr('user@spark.apache.org', '@[^.]*');
    631. +----------------------------------------------+
    632. |regexp_instr(user@spark.apache.org, @[^.]*, 0)|
    633. +----------------------------------------------+
    634. | 5|
    635. +----------------------------------------------+
    636. -- regexp_replace
    637. SELECT regexp_replace('100-200', '(\\d+)', 'num');
    638. +--------------------------------------+
    639. |regexp_replace(100-200, (\d+), num, 1)|
    640. +--------------------------------------+
    641. | num-num|
    642. +--------------------------------------+
    643. -- regexp_substr
    644. SELECT regexp_substr('Steven Jones and Stephen Smith are the best players', 'Ste(v|ph)en');
    645. +-------------------------------------------------------------------------------+
    646. |regexp_substr(Steven Jones and Stephen Smith are the best players, Ste(v|ph)en)|
    647. +-------------------------------------------------------------------------------+
    648. | Steven|
    649. +-------------------------------------------------------------------------------+
    650. SELECT regexp_substr('Steven Jones and Stephen Smith are the best players', 'Jeck');
    651. +------------------------------------------------------------------------+
    652. |regexp_substr(Steven Jones and Stephen Smith are the best players, Jeck)|
    653. +------------------------------------------------------------------------+
    654. | null|
    655. +------------------------------------------------------------------------+
    656. -- repeat
    657. SELECT repeat('123', 2);
    658. +--------------+
    659. |repeat(123, 2)|
    660. +--------------+
    661. | 123123|
    662. +--------------+
    663. -- replace
    664. SELECT replace('ABCabc', 'abc', 'DEF');
    665. +-------------------------+
    666. |replace(ABCabc, abc, DEF)|
    667. +-------------------------+
    668. | ABCDEF|
    669. +-------------------------+
    670. -- right
    671. SELECT right('Spark SQL', 3);
    672. +-------------------+
    673. |right(Spark SQL, 3)|
    674. +-------------------+
    675. | SQL|
    676. +-------------------+
    677. -- rpad
    678. SELECT rpad('hi', 5, '??');
    679. +---------------+
    680. |rpad(hi, 5, ??)|
    681. +---------------+
    682. | hi???|
    683. +---------------+
    684. SELECT rpad('hi', 1, '??');
    685. +---------------+
    686. |rpad(hi, 1, ??)|
    687. +---------------+
    688. | h|
    689. +---------------+
    690. SELECT rpad('hi', 5);
    691. +--------------+
    692. |rpad(hi, 5, )|
    693. +--------------+
    694. | hi |
    695. +--------------+
    696. SELECT hex(rpad(unhex('aabb'), 5));
    697. +--------------------------------+
    698. |hex(rpad(unhex(aabb), 5, X'00'))|
    699. +--------------------------------+
    700. | AABB000000|
    701. +--------------------------------+
    702. SELECT hex(rpad(unhex('aabb'), 5, unhex('1122')));
    703. +--------------------------------------+
    704. |hex(rpad(unhex(aabb), 5, unhex(1122)))|
    705. +--------------------------------------+
    706. | AABB112211|
    707. +--------------------------------------+
    708. -- rtrim
    709. SELECT rtrim(' SparkSQL ');
    710. +----------------------+
    711. |rtrim( SparkSQL )|
    712. +----------------------+
    713. | SparkSQL|
    714. +----------------------+
    715. -- sentences
    716. SELECT sentences('Hi there! Good morning.');
    717. +--------------------------------------+
    718. |sentences(Hi there! Good morning., , )|
    719. +--------------------------------------+
    720. | [[Hi, there], [Go...|
    721. +--------------------------------------+
    722. -- soundex
    723. SELECT soundex('Miller');
    724. +---------------+
    725. |soundex(Miller)|
    726. +---------------+
    727. | M460|
    728. +---------------+
    729. -- space
    730. SELECT concat(space(2), '1');
    731. +-------------------+
    732. |concat(space(2), 1)|
    733. +-------------------+
    734. | 1|
    735. +-------------------+
    736. -- split
    737. SELECT split('oneAtwoBthreeC', '[ABC]');
    738. +--------------------------------+
    739. |split(oneAtwoBthreeC, [ABC], -1)|
    740. +--------------------------------+
    741. | [one, two, three, ]|
    742. +--------------------------------+
    743. SELECT split('oneAtwoBthreeC', '[ABC]', -1);
    744. +--------------------------------+
    745. |split(oneAtwoBthreeC, [ABC], -1)|
    746. +--------------------------------+
    747. | [one, two, three, ]|
    748. +--------------------------------+
    749. SELECT split('oneAtwoBthreeC', '[ABC]', 2);
    750. +-------------------------------+
    751. |split(oneAtwoBthreeC, [ABC], 2)|
    752. +-------------------------------+
    753. | [one, twoBthreeC]|
    754. +-------------------------------+
    755. -- split_part
    756. SELECT split_part('11.12.13', '.', 3);
    757. +--------------------------+
    758. |split_part(11.12.13, ., 3)|
    759. +--------------------------+
    760. | 13|
    761. +--------------------------+
    762. -- startswith
    763. SELECT startswith('Spark SQL', 'Spark');
    764. +----------------------------+
    765. |startswith(Spark SQL, Spark)|
    766. +----------------------------+
    767. | true|
    768. +----------------------------+
    769. SELECT startswith('Spark SQL', 'SQL');
    770. +--------------------------+
    771. |startswith(Spark SQL, SQL)|
    772. +--------------------------+
    773. | false|
    774. +--------------------------+
    775. SELECT startswith('Spark SQL', null);
    776. +---------------------------+
    777. |startswith(Spark SQL, NULL)|
    778. +---------------------------+
    779. | null|
    780. +---------------------------+
    781. SELECT startswith(x'537061726b2053514c', x'537061726b');
    782. +------------------------------------------------+
    783. |startswith(X'537061726B2053514C', X'537061726B')|
    784. +------------------------------------------------+
    785. | true|
    786. +------------------------------------------------+
    787. SELECT startswith(x'537061726b2053514c', x'53514c');
    788. +--------------------------------------------+
    789. |startswith(X'537061726B2053514C', X'53514C')|
    790. +--------------------------------------------+
    791. | false|
    792. +--------------------------------------------+
    793. -- substr
    794. SELECT substr('Spark SQL', 5);
    795. +--------------------------------+
    796. |substr(Spark SQL, 5, 2147483647)|
    797. +--------------------------------+
    798. | k SQL|
    799. +--------------------------------+
    800. SELECT substr('Spark SQL', -3);
    801. +---------------------------------+
    802. |substr(Spark SQL, -3, 2147483647)|
    803. +---------------------------------+
    804. | SQL|
    805. +---------------------------------+
    806. SELECT substr('Spark SQL', 5, 1);
    807. +-----------------------+
    808. |substr(Spark SQL, 5, 1)|
    809. +-----------------------+
    810. | k|
    811. +-----------------------+
    812. SELECT substr('Spark SQL' FROM 5);
    813. +-----------------------------------+
    814. |substring(Spark SQL, 5, 2147483647)|
    815. +-----------------------------------+
    816. | k SQL|
    817. +-----------------------------------+
    818. SELECT substr('Spark SQL' FROM -3);
    819. +------------------------------------+
    820. |substring(Spark SQL, -3, 2147483647)|
    821. +------------------------------------+
    822. | SQL|
    823. +------------------------------------+
    824. SELECT substr('Spark SQL' FROM 5 FOR 1);
    825. +--------------------------+
    826. |substring(Spark SQL, 5, 1)|
    827. +--------------------------+
    828. | k|
    829. +--------------------------+
    830. SELECT substr(encode('Spark SQL', 'utf-8'), 5);
    831. +-----------------------------------------------+
    832. |substr(encode(Spark SQL, utf-8), 5, 2147483647)|
    833. +-----------------------------------------------+
    834. | [6B 20 53 51 4C]|
    835. +-----------------------------------------------+
    836. -- substring
    837. SELECT substring('Spark SQL', 5);
    838. +-----------------------------------+
    839. |substring(Spark SQL, 5, 2147483647)|
    840. +-----------------------------------+
    841. | k SQL|
    842. +-----------------------------------+
    843. SELECT substring('Spark SQL', -3);
    844. +------------------------------------+
    845. |substring(Spark SQL, -3, 2147483647)|
    846. +------------------------------------+
    847. | SQL|
    848. +------------------------------------+
    849. SELECT substring('Spark SQL', 5, 1);
    850. +--------------------------+
    851. |substring(Spark SQL, 5, 1)|
    852. +--------------------------+
    853. | k|
    854. +--------------------------+
    855. SELECT substring('Spark SQL' FROM 5);
    856. +-----------------------------------+
    857. |substring(Spark SQL, 5, 2147483647)|
    858. +-----------------------------------+
    859. | k SQL|
    860. +-----------------------------------+
    861. SELECT substring('Spark SQL' FROM -3);
    862. +------------------------------------+
    863. |substring(Spark SQL, -3, 2147483647)|
    864. +------------------------------------+
    865. | SQL|
    866. +------------------------------------+
    867. SELECT substring('Spark SQL' FROM 5 FOR 1);
    868. +--------------------------+
    869. |substring(Spark SQL, 5, 1)|
    870. +--------------------------+
    871. | k|
    872. +--------------------------+
    873. SELECT substring(encode('Spark SQL', 'utf-8'), 5);
    874. +--------------------------------------------------+
    875. |substring(encode(Spark SQL, utf-8), 5, 2147483647)|
    876. +--------------------------------------------------+
    877. | [6B 20 53 51 4C]|
    878. +--------------------------------------------------+
    879. -- substring_index
    880. SELECT substring_index('www.apache.org', '.', 2);
    881. +-------------------------------------+
    882. |substring_index(www.apache.org, ., 2)|
    883. +-------------------------------------+
    884. | www.apache|
    885. +-------------------------------------+
    886. -- to_binary
    887. SELECT to_binary('abc', 'utf-8');
    888. +---------------------+
    889. |to_binary(abc, utf-8)|
    890. +---------------------+
    891. | [61 62 63]|
    892. +---------------------+
    893. -- to_char
    894. SELECT to_char(454, '999');
    895. +-----------------+
    896. |to_char(454, 999)|
    897. +-----------------+
    898. | 454|
    899. +-----------------+
    900. SELECT to_char(454.00, '000D00');
    901. +-----------------------+
    902. |to_char(454.00, 000D00)|
    903. +-----------------------+
    904. | 454.00|
    905. +-----------------------+
    906. SELECT to_char(12454, '99G999');
    907. +----------------------+
    908. |to_char(12454, 99G999)|
    909. +----------------------+
    910. | 12,454|
    911. +----------------------+
    912. SELECT to_char(78.12, '$99.99');
    913. +----------------------+
    914. |to_char(78.12, $99.99)|
    915. +----------------------+
    916. | $78.12|
    917. +----------------------+
    918. SELECT to_char(-12454.8, '99G999D9S');
    919. +----------------------------+
    920. |to_char(-12454.8, 99G999D9S)|
    921. +----------------------------+
    922. | 12,454.8-|
    923. +----------------------------+
    924. -- to_number
    925. SELECT to_number('454', '999');
    926. +-------------------+
    927. |to_number(454, 999)|
    928. +-------------------+
    929. | 454|
    930. +-------------------+
    931. SELECT to_number('454.00', '000.00');
    932. +-------------------------+
    933. |to_number(454.00, 000.00)|
    934. +-------------------------+
    935. | 454.00|
    936. +-------------------------+
    937. SELECT to_number('12,454', '99,999');
    938. +-------------------------+
    939. |to_number(12,454, 99,999)|
    940. +-------------------------+
    941. | 12454|
    942. +-------------------------+
    943. SELECT to_number('$78.12', '$99.99');
    944. +-------------------------+
    945. |to_number($78.12, $99.99)|
    946. +-------------------------+
    947. | 78.12|
    948. +-------------------------+
    949. SELECT to_number('12,454.8-', '99,999.9S');
    950. +-------------------------------+
    951. |to_number(12,454.8-, 99,999.9S)|
    952. +-------------------------------+
    953. | -12454.8|
    954. +-------------------------------+
    955. -- translate
    956. SELECT translate('AaBbCc', 'abc', '123');
    957. +---------------------------+
    958. |translate(AaBbCc, abc, 123)|
    959. +---------------------------+
    960. | A1B2C3|
    961. +---------------------------+
    962. -- trim
    963. SELECT trim(' SparkSQL ');
    964. +---------------------+
    965. |trim( SparkSQL )|
    966. +---------------------+
    967. | SparkSQL|
    968. +---------------------+
    969. SELECT trim(BOTH FROM ' SparkSQL ');
    970. +---------------------+
    971. |trim( SparkSQL )|
    972. +---------------------+
    973. | SparkSQL|
    974. +---------------------+
    975. SELECT trim(LEADING FROM ' SparkSQL ');
    976. +----------------------+
    977. |ltrim( SparkSQL )|
    978. +----------------------+
    979. | SparkSQL |
    980. +----------------------+
    981. SELECT trim(TRAILING FROM ' SparkSQL ');
    982. +----------------------+
    983. |rtrim( SparkSQL )|
    984. +----------------------+
    985. | SparkSQL|
    986. +----------------------+
    987. SELECT trim('SL' FROM 'SSparkSQLS');
    988. +-----------------------------+
    989. |TRIM(BOTH SL FROM SSparkSQLS)|
    990. +-----------------------------+
    991. | parkSQ|
    992. +-----------------------------+
    993. SELECT trim(BOTH 'SL' FROM 'SSparkSQLS');
    994. +-----------------------------+
    995. |TRIM(BOTH SL FROM SSparkSQLS)|
    996. +-----------------------------+
    997. | parkSQ|
    998. +-----------------------------+
    999. SELECT trim(LEADING 'SL' FROM 'SSparkSQLS');
    1000. +--------------------------------+
    1001. |TRIM(LEADING SL FROM SSparkSQLS)|
    1002. +--------------------------------+
    1003. | parkSQLS|
    1004. +--------------------------------+
    1005. SELECT trim(TRAILING 'SL' FROM 'SSparkSQLS');
    1006. +---------------------------------+
    1007. |TRIM(TRAILING SL FROM SSparkSQLS)|
    1008. +---------------------------------+
    1009. | SSparkSQ|
    1010. +---------------------------------+
    1011. -- try_to_binary
    1012. SELECT try_to_binary('abc', 'utf-8');
    1013. +-------------------------+
    1014. |try_to_binary(abc, utf-8)|
    1015. +-------------------------+
    1016. | [61 62 63]|
    1017. +-------------------------+
    1018. select try_to_binary('a!', 'base64');
    1019. +-------------------------+
    1020. |try_to_binary(a!, base64)|
    1021. +-------------------------+
    1022. | null|
    1023. +-------------------------+
    1024. select try_to_binary('abc', 'invalidFormat');
    1025. +---------------------------------+
    1026. |try_to_binary(abc, invalidFormat)|
    1027. +---------------------------------+
    1028. | null|
    1029. +---------------------------------+
    1030. -- try_to_number
    1031. SELECT try_to_number('454', '999');
    1032. +-----------------------+
    1033. |try_to_number(454, 999)|
    1034. +-----------------------+
    1035. | 454|
    1036. +-----------------------+
    1037. SELECT try_to_number('454.00', '000.00');
    1038. +-----------------------------+
    1039. |try_to_number(454.00, 000.00)|
    1040. +-----------------------------+
    1041. | 454.00|
    1042. +-----------------------------+
    1043. SELECT try_to_number('12,454', '99,999');
    1044. +-----------------------------+
    1045. |try_to_number(12,454, 99,999)|
    1046. +-----------------------------+
    1047. | 12454|
    1048. +-----------------------------+
    1049. SELECT try_to_number('$78.12', '$99.99');
    1050. +-----------------------------+
    1051. |try_to_number($78.12, $99.99)|
    1052. +-----------------------------+
    1053. | 78.12|
    1054. +-----------------------------+
    1055. SELECT try_to_number('12,454.8-', '99,999.9S');
    1056. +-----------------------------------+
    1057. |try_to_number(12,454.8-, 99,999.9S)|
    1058. +-----------------------------------+
    1059. | -12454.8|
    1060. +-----------------------------------+
    1061. -- ucase
    1062. SELECT ucase('SparkSql');
    1063. +---------------+
    1064. |ucase(SparkSql)|
    1065. +---------------+
    1066. | SPARKSQL|
    1067. +---------------+
    1068. -- unbase64
    1069. SELECT unbase64('U3BhcmsgU1FM');
    1070. +----------------------+
    1071. |unbase64(U3BhcmsgU1FM)|
    1072. +----------------------+
    1073. | [53 70 61 72 6B 2...|
    1074. +----------------------+
    1075. -- upper
    1076. SELECT upper('SparkSql');
    1077. +---------------+
    1078. |upper(SparkSql)|
    1079. +---------------+
    1080. | SPARKSQL|
    1081. +---------------+

    Conditional Functions

    FunctionDescription
    coalesce(expr1, expr2, …)Returns the first non-null argument if exists. Otherwise, null.
    if(expr1, expr2, expr3)If expr1 evaluates to true, then returns expr2; otherwise returns expr3.
    ifnull(expr1, expr2)Returns expr2 if expr1 is null, or expr1 otherwise.
    nanvl(expr1, expr2)Returns expr1 if it’s not NaN, or expr2 otherwise.
    nullif(expr1, expr2)Returns null if expr1 equals to expr2, or expr1 otherwise.
    nvl(expr1, expr2)Returns expr2 if expr1 is null, or expr1 otherwise.
    nvl2(expr1, expr2, expr3)Returns expr2 if expr1 is not null, or expr3 otherwise.
    CASE WHEN expr1 THEN expr2 [WHEN expr3 THEN expr4]* [ELSE expr5] ENDWhen expr1 = true, returns expr2; else when expr3 = true, returns expr4; else returns expr5.

    Examples

    -- coalesce
    SELECT coalesce(NULL, 1, NULL);
    +-----------------------+
    |coalesce(NULL, 1, NULL)|
    +-----------------------+
    |                      1|
    +-----------------------+
    
    -- if
    SELECT if(1 < 2, 'a', 'b');
    +-------------------+
    |(IF((1 < 2), a, b))|
    +-------------------+
    |                  a|
    +-------------------+
    
    -- ifnull
    SELECT ifnull(NULL, array('2'));
    +----------------------+
    |ifnull(NULL, array(2))|
    +----------------------+
    |                   [2]|
    +----------------------+
    
    -- nanvl
    SELECT nanvl(cast('NaN' as double), 123);
    +-------------------------------+
    |nanvl(CAST(NaN AS DOUBLE), 123)|
    +-------------------------------+
    |                          123.0|
    +-------------------------------+
    
    -- nullif
    SELECT nullif(2, 2);
    +------------+
    |nullif(2, 2)|
    +------------+
    |        null|
    +------------+
    
    -- nvl
    SELECT nvl(NULL, array('2'));
    +-------------------+
    |nvl(NULL, array(2))|
    +-------------------+
    |                [2]|
    +-------------------+
    
    -- nvl2
    SELECT nvl2(NULL, 2, 1);
    +----------------+
    |nvl2(NULL, 2, 1)|
    +----------------+
    |               1|
    +----------------+
    
    -- when
    SELECT CASE WHEN 1 > 0 THEN 1 WHEN 2 > 0 THEN 2.0 ELSE 1.2 END;
    +-----------------------------------------------------------+
    |CASE WHEN (1 > 0) THEN 1 WHEN (2 > 0) THEN 2.0 ELSE 1.2 END|
    +-----------------------------------------------------------+
    |                                                        1.0|
    +-----------------------------------------------------------+
    
    SELECT CASE WHEN 1 < 0 THEN 1 WHEN 2 > 0 THEN 2.0 ELSE 1.2 END;
    +-----------------------------------------------------------+
    |CASE WHEN (1 < 0) THEN 1 WHEN (2 > 0) THEN 2.0 ELSE 1.2 END|
    +-----------------------------------------------------------+
    |                                                        2.0|
    +-----------------------------------------------------------+
    
    SELECT CASE WHEN 1 < 0 THEN 1 WHEN 2 < 0 THEN 2.0 END;
    +--------------------------------------------------+
    |CASE WHEN (1 < 0) THEN 1 WHEN (2 < 0) THEN 2.0 END|
    +--------------------------------------------------+
    |                                              null|
    +--------------------------------------------------+
    

    Bitwise Functions

    FunctionDescription
    expr1 & expr2Returns the result of bitwise AND of expr1 and expr2.
    expr1 ^ expr2Returns the result of bitwise exclusive OR of expr1 and expr2.
    bit_count(expr)Returns the number of bits that are set in the argument expr as an unsigned 64-bit integer, or NULL if the argument is NULL.
    bit_get(expr, pos)Returns the value of the bit (0 or 1) at the specified position. The positions are numbered from right to left, starting at zero. The position argument cannot be negative.
    getbit(expr, pos)Returns the value of the bit (0 or 1) at the specified position. The positions are numbered from right to left, starting at zero. The position argument cannot be negative.
    shiftright(base, expr)Bitwise (signed) right shift.
    shiftrightunsigned(base, expr)Bitwise unsigned right shift.
    expr1 | expr2Returns the result of bitwise OR of expr1 and expr2.
    ~ exprReturns the result of bitwise NOT of expr.

    Examples

    -- &
    SELECT 3 & 5;
    +-------+
    |(3 & 5)|
    +-------+
    |      1|
    +-------+
    
    -- ^
    SELECT 3 ^ 5;
    +-------+
    |(3 ^ 5)|
    +-------+
    |      6|
    +-------+
    
    -- bit_count
    SELECT bit_count(0);
    +------------+
    |bit_count(0)|
    +------------+
    |           0|
    +------------+
    
    -- bit_get
    SELECT bit_get(11, 0);
    +--------------+
    |bit_get(11, 0)|
    +--------------+
    |             1|
    +--------------+
    
    SELECT bit_get(11, 2);
    +--------------+
    |bit_get(11, 2)|
    +--------------+
    |             0|
    +--------------+
    
    -- getbit
    SELECT getbit(11, 0);
    +-------------+
    |getbit(11, 0)|
    +-------------+
    |            1|
    +-------------+
    
    SELECT getbit(11, 2);
    +-------------+
    |getbit(11, 2)|
    +-------------+
    |            0|
    +-------------+
    
    -- shiftright
    SELECT shiftright(4, 1);
    +----------------+
    |shiftright(4, 1)|
    +----------------+
    |               2|
    +----------------+
    
    -- shiftrightunsigned
    SELECT shiftrightunsigned(4, 1);
    +------------------------+
    |shiftrightunsigned(4, 1)|
    +------------------------+
    |                       2|
    +------------------------+
    
    -- |
    SELECT 3 | 5;
    +-------+
    |(3 | 5)|
    +-------+
    |      7|
    +-------+
    
    -- ~
    SELECT ~ 0;
    +---+
    | ~0|
    +---+
    | -1|
    +---+
    

    Examples

    Predicate Functions

    FunctionDescription
    ! exprLogical not.
    expr1 < expr2Returns true if expr1 is less than expr2.
    expr1 <= expr2Returns true if expr1 is less than or equal to expr2.
    expr1 <=> expr2Returns same result as the EQUAL(=) operator for non-null operands, but returns true if both are null, false if one of the them is null.
    expr1 = expr2Returns true if expr1 equals expr2, or false otherwise.
    expr1 == expr2Returns true if expr1 equals expr2, or false otherwise.
    expr1 > expr2Returns true if expr1 is greater than expr2.
    expr1 >= expr2Returns true if expr1 is greater than or equal to expr2.
    expr1 and expr2Logical AND.
    str ilike pattern[ ESCAPE escape]Returns true if str matches pattern with escape case-insensitively, null if any arguments are null, false otherwise.
    expr1 in(expr2, expr3, …)Returns true if expr equals to any valN.
    isnan(expr)Returns true if expr is NaN, or false otherwise.
    isnotnull(expr)Returns true if expr is not null, or false otherwise.
    isnull(expr)Returns true if expr is null, or false otherwise.
    str like pattern[ ESCAPE escape]Returns true if str matches pattern with escape, null if any arguments are null, false otherwise.
    not exprLogical not.
    expr1 or expr2Logical OR.
    regexp(str, regexp)Returns true if str matches regexp, or false otherwise.
    regexp_like(str, regexp)Returns true if str matches regexp, or false otherwise.
    rlike(str, regexp)Returns true if str matches regexp, or false otherwise.

    Examples

    -- !
    SELECT ! true;
    +----------+
    |(NOT true)|
    +----------+
    |     false|
    +----------+
    
    SELECT ! false;
    +-----------+
    |(NOT false)|
    +-----------+
    |       true|
    +-----------+
    
    SELECT ! NULL;
    +----------+
    |(NOT NULL)|
    +----------+
    |      null|
    +----------+
    
    -- <
    SELECT 1 < 2;
    +-------+
    |(1 < 2)|
    +-------+
    |   true|
    +-------+
    
    SELECT 1.1 < '1';
    +---------+
    |(1.1 < 1)|
    +---------+
    |    false|
    +---------+
    
    SELECT to_date('2009-07-30 04:17:52') < to_date('2009-07-30 04:17:52');
    +-------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) < to_date(2009-07-30 04:17:52))|
    +-------------------------------------------------------------+
    |                                                        false|
    +-------------------------------------------------------------+
    
    SELECT to_date('2009-07-30 04:17:52') < to_date('2009-08-01 04:17:52');
    +-------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) < to_date(2009-08-01 04:17:52))|
    +-------------------------------------------------------------+
    |                                                         true|
    +-------------------------------------------------------------+
    
    SELECT 1 < NULL;
    +----------+
    |(1 < NULL)|
    +----------+
    |      null|
    +----------+
    
    -- <=
    SELECT 2 <= 2;
    +--------+
    |(2 <= 2)|
    +--------+
    |    true|
    +--------+
    
    SELECT 1.0 <= '1';
    +----------+
    |(1.0 <= 1)|
    +----------+
    |      true|
    +----------+
    
    SELECT to_date('2009-07-30 04:17:52') <= to_date('2009-07-30 04:17:52');
    +--------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) <= to_date(2009-07-30 04:17:52))|
    +--------------------------------------------------------------+
    |                                                          true|
    +--------------------------------------------------------------+
    
    SELECT to_date('2009-07-30 04:17:52') <= to_date('2009-08-01 04:17:52');
    +--------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) <= to_date(2009-08-01 04:17:52))|
    +--------------------------------------------------------------+
    |                                                          true|
    +--------------------------------------------------------------+
    
    SELECT 1 <= NULL;
    +-----------+
    |(1 <= NULL)|
    +-----------+
    |       null|
    +-----------+
    
    -- <=>
    SELECT 2 <=> 2;
    +---------+
    |(2 <=> 2)|
    +---------+
    |     true|
    +---------+
    
    SELECT 1 <=> '1';
    +---------+
    |(1 <=> 1)|
    +---------+
    |     true|
    +---------+
    
    SELECT true <=> NULL;
    +---------------+
    |(true <=> NULL)|
    +---------------+
    |          false|
    +---------------+
    
    SELECT NULL <=> NULL;
    +---------------+
    |(NULL <=> NULL)|
    +---------------+
    |           true|
    +---------------+
    
    -- =
    SELECT 2 = 2;
    +-------+
    |(2 = 2)|
    +-------+
    |   true|
    +-------+
    
    SELECT 1 = '1';
    +-------+
    |(1 = 1)|
    +-------+
    |   true|
    +-------+
    
    SELECT true = NULL;
    +-------------+
    |(true = NULL)|
    +-------------+
    |         null|
    +-------------+
    
    SELECT NULL = NULL;
    +-------------+
    |(NULL = NULL)|
    +-------------+
    |         null|
    +-------------+
    
    -- ==
    SELECT 2 == 2;
    +-------+
    |(2 = 2)|
    +-------+
    |   true|
    +-------+
    
    SELECT 1 == '1';
    +-------+
    |(1 = 1)|
    +-------+
    |   true|
    +-------+
    
    SELECT true == NULL;
    +-------------+
    |(true = NULL)|
    +-------------+
    |         null|
    +-------------+
    
    SELECT NULL == NULL;
    +-------------+
    |(NULL = NULL)|
    +-------------+
    |         null|
    +-------------+
    
    -- >
    SELECT 2 > 1;
    +-------+
    |(2 > 1)|
    +-------+
    |   true|
    +-------+
    
    SELECT 2 > 1.1;
    +-------+
    |(2 > 1)|
    +-------+
    |   true|
    +-------+
    
    SELECT to_date('2009-07-30 04:17:52') > to_date('2009-07-30 04:17:52');
    +-------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) > to_date(2009-07-30 04:17:52))|
    +-------------------------------------------------------------+
    |                                                        false|
    +-------------------------------------------------------------+
    
    SELECT to_date('2009-07-30 04:17:52') > to_date('2009-08-01 04:17:52');
    +-------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) > to_date(2009-08-01 04:17:52))|
    +-------------------------------------------------------------+
    |                                                        false|
    +-------------------------------------------------------------+
    
    SELECT 1 > NULL;
    +----------+
    |(1 > NULL)|
    +----------+
    |      null|
    +----------+
    
    -- >=
    SELECT 2 >= 1;
    +--------+
    |(2 >= 1)|
    +--------+
    |    true|
    +--------+
    
    SELECT 2.0 >= '2.1';
    +------------+
    |(2.0 >= 2.1)|
    +------------+
    |       false|
    +------------+
    
    SELECT to_date('2009-07-30 04:17:52') >= to_date('2009-07-30 04:17:52');
    +--------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) >= to_date(2009-07-30 04:17:52))|
    +--------------------------------------------------------------+
    |                                                          true|
    +--------------------------------------------------------------+
    
    SELECT to_date('2009-07-30 04:17:52') >= to_date('2009-08-01 04:17:52');
    +--------------------------------------------------------------+
    |(to_date(2009-07-30 04:17:52) >= to_date(2009-08-01 04:17:52))|
    +--------------------------------------------------------------+
    |                                                         false|
    +--------------------------------------------------------------+
    
    SELECT 1 >= NULL;
    +-----------+
    |(1 >= NULL)|
    +-----------+
    |       null|
    +-----------+
    
    -- and
    SELECT true and true;
    +---------------+
    |(true AND true)|
    +---------------+
    |           true|
    +---------------+
    
    SELECT true and false;
    +----------------+
    |(true AND false)|
    +----------------+
    |           false|
    +----------------+
    
    SELECT true and NULL;
    +---------------+
    |(true AND NULL)|
    +---------------+
    |           null|
    +---------------+
    
    SELECT false and NULL;
    +----------------+
    |(false AND NULL)|
    +----------------+
    |           false|
    +----------------+
    
    -- ilike
    SELECT ilike('Spark', '_Park');
    +-------------------+
    |ilike(Spark, _Park)|
    +-------------------+
    |               true|
    +-------------------+
    
    SET spark.sql.parser.escapedStringLiterals=true;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....| true|
    +--------------------+-----+
    
    SELECT '%SystemDrive%\Users\John' ilike '\%SystemDrive\%\\users%';
    +--------------------------------------------------------+
    |ilike(%SystemDrive%\Users\John, \%SystemDrive\%\\users%)|
    +--------------------------------------------------------+
    |                                                    true|
    +--------------------------------------------------------+
    
    SET spark.sql.parser.escapedStringLiterals=false;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....|false|
    +--------------------+-----+
    
    SELECT '%SystemDrive%\\USERS\\John' ilike '\%SystemDrive\%\\\\Users%';
    +--------------------------------------------------------+
    |ilike(%SystemDrive%\USERS\John, \%SystemDrive\%\\Users%)|
    +--------------------------------------------------------+
    |                                                    true|
    +--------------------------------------------------------+
    
    SELECT '%SystemDrive%/Users/John' ilike '/%SYSTEMDrive/%//Users%' ESCAPE '/';
    +--------------------------------------------------------+
    |ilike(%SystemDrive%/Users/John, /%SYSTEMDrive/%//Users%)|
    +--------------------------------------------------------+
    |                                                    true|
    +--------------------------------------------------------+
    
    -- in
    SELECT 1 in(1, 2, 3);
    +----------------+
    |(1 IN (1, 2, 3))|
    +----------------+
    |            true|
    +----------------+
    
    SELECT 1 in(2, 3, 4);
    +----------------+
    |(1 IN (2, 3, 4))|
    +----------------+
    |           false|
    +----------------+
    
    SELECT named_struct('a', 1, 'b', 2) in(named_struct('a', 1, 'b', 1), named_struct('a', 1, 'b', 3));
    +----------------------------------------------------------------------------------+
    |(named_struct(a, 1, b, 2) IN (named_struct(a, 1, b, 1), named_struct(a, 1, b, 3)))|
    +----------------------------------------------------------------------------------+
    |                                                                             false|
    +----------------------------------------------------------------------------------+
    
    SELECT named_struct('a', 1, 'b', 2) in(named_struct('a', 1, 'b', 2), named_struct('a', 1, 'b', 3));
    +----------------------------------------------------------------------------------+
    |(named_struct(a, 1, b, 2) IN (named_struct(a, 1, b, 2), named_struct(a, 1, b, 3)))|
    +----------------------------------------------------------------------------------+
    |                                                                              true|
    +----------------------------------------------------------------------------------+
    
    -- isnan
    SELECT isnan(cast('NaN' as double));
    +--------------------------+
    |isnan(CAST(NaN AS DOUBLE))|
    +--------------------------+
    |                      true|
    +--------------------------+
    
    -- isnotnull
    SELECT isnotnull(1);
    +---------------+
    |(1 IS NOT NULL)|
    +---------------+
    |           true|
    +---------------+
    
    -- isnull
    SELECT isnull(1);
    +-----------+
    |(1 IS NULL)|
    +-----------+
    |      false|
    +-----------+
    
    -- like
    SELECT like('Spark', '_park');
    +----------------+
    |Spark LIKE _park|
    +----------------+
    |            true|
    +----------------+
    
    SET spark.sql.parser.escapedStringLiterals=true;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....| true|
    +--------------------+-----+
    
    SELECT '%SystemDrive%\Users\John' like '\%SystemDrive\%\\Users%';
    +-----------------------------------------------------+
    |%SystemDrive%\Users\John LIKE \%SystemDrive\%\\Users%|
    +-----------------------------------------------------+
    |                                                 true|
    +-----------------------------------------------------+
    
    SET spark.sql.parser.escapedStringLiterals=false;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....|false|
    +--------------------+-----+
    
    SELECT '%SystemDrive%\\Users\\John' like '\%SystemDrive\%\\\\Users%';
    +-----------------------------------------------------+
    |%SystemDrive%\Users\John LIKE \%SystemDrive\%\\Users%|
    +-----------------------------------------------------+
    |                                                 true|
    +-----------------------------------------------------+
    
    SELECT '%SystemDrive%/Users/John' like '/%SystemDrive/%//Users%' ESCAPE '/';
    +-----------------------------------------------------+
    |%SystemDrive%/Users/John LIKE /%SystemDrive/%//Users%|
    +-----------------------------------------------------+
    |                                                 true|
    +-----------------------------------------------------+
    
    -- not
    SELECT not true;
    +----------+
    |(NOT true)|
    +----------+
    |     false|
    +----------+
    
    SELECT not false;
    +-----------+
    |(NOT false)|
    +-----------+
    |       true|
    +-----------+
    
    SELECT not NULL;
    +----------+
    |(NOT NULL)|
    +----------+
    |      null|
    +----------+
    
    -- or
    SELECT true or false;
    +---------------+
    |(true OR false)|
    +---------------+
    |           true|
    +---------------+
    
    SELECT false or false;
    +----------------+
    |(false OR false)|
    +----------------+
    |           false|
    +----------------+
    
    SELECT true or NULL;
    +--------------+
    |(true OR NULL)|
    +--------------+
    |          true|
    +--------------+
    
    SELECT false or NULL;
    +---------------+
    |(false OR NULL)|
    +---------------+
    |           null|
    +---------------+
    
    -- regexp
    SET spark.sql.parser.escapedStringLiterals=true;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....| true|
    +--------------------+-----+
    
    SELECT regexp('%SystemDrive%\Users\John', '%SystemDrive%\\Users.*');
    +--------------------------------------------------------+
    |REGEXP(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +--------------------------------------------------------+
    |                                                    true|
    +--------------------------------------------------------+
    
    SET spark.sql.parser.escapedStringLiterals=false;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....|false|
    +--------------------+-----+
    
    SELECT regexp('%SystemDrive%\\Users\\John', '%SystemDrive%\\\\Users.*');
    +--------------------------------------------------------+
    |REGEXP(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +--------------------------------------------------------+
    |                                                    true|
    +--------------------------------------------------------+
    
    -- regexp_like
    SET spark.sql.parser.escapedStringLiterals=true;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....| true|
    +--------------------+-----+
    
    SELECT regexp_like('%SystemDrive%\Users\John', '%SystemDrive%\\Users.*');
    +-------------------------------------------------------------+
    |REGEXP_LIKE(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +-------------------------------------------------------------+
    |                                                         true|
    +-------------------------------------------------------------+
    
    SET spark.sql.parser.escapedStringLiterals=false;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....|false|
    +--------------------+-----+
    
    SELECT regexp_like('%SystemDrive%\\Users\\John', '%SystemDrive%\\\\Users.*');
    +-------------------------------------------------------------+
    |REGEXP_LIKE(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +-------------------------------------------------------------+
    |                                                         true|
    +-------------------------------------------------------------+
    
    -- rlike
    SET spark.sql.parser.escapedStringLiterals=true;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....| true|
    +--------------------+-----+
    
    SELECT rlike('%SystemDrive%\Users\John', '%SystemDrive%\\Users.*');
    +-------------------------------------------------------+
    |RLIKE(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +-------------------------------------------------------+
    |                                                   true|
    +-------------------------------------------------------+
    
    SET spark.sql.parser.escapedStringLiterals=false;
    +--------------------+-----+
    |                 key|value|
    +--------------------+-----+
    |spark.sql.parser....|false|
    +--------------------+-----+
    
    SELECT rlike('%SystemDrive%\\Users\\John', '%SystemDrive%\\\\Users.*');
    +-------------------------------------------------------+
    |RLIKE(%SystemDrive%\Users\John, %SystemDrive%\\Users.*)|
    +-------------------------------------------------------+
    |                                                   true|
    +-------------------------------------------------------+
    

    Csv Functions

    FunctionDescription
    from_csv(csvStr, schema[, options])Returns a struct value with the given csvStr and schema.
    schema_of_csv(csv[, options])Returns schema in the DDL format of CSV string.
    to_csv(expr[, options])Returns a CSV string with a given struct value

    Examples

    -- from_csv
    SELECT from_csv('1, 0.8', 'a INT, b DOUBLE');
    +----------------+
    |from_csv(1, 0.8)|
    +----------------+
    |        {1, 0.8}|
    +----------------+
    
    SELECT from_csv('26/08/2015', 'time Timestamp', map('timestampFormat', 'dd/MM/yyyy'));
    +--------------------+
    |from_csv(26/08/2015)|
    +--------------------+
    |{2015-08-26 00:00...|
    +--------------------+
    
    -- schema_of_csv
    SELECT schema_of_csv('1,abc');
    +--------------------+
    |schema_of_csv(1,abc)|
    +--------------------+
    |STRUCT<_c0: INT, ...|
    +--------------------+
    
    -- to_csv
    SELECT to_csv(named_struct('a', 1, 'b', 2));
    +--------------------------------+
    |to_csv(named_struct(a, 1, b, 2))|
    +--------------------------------+
    |                             1,2|
    +--------------------------------+
    
    SELECT to_csv(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy'));
    +----------------------------------------------------------------+
    |to_csv(named_struct(time, to_timestamp(2015-08-26, yyyy-MM-dd)))|
    +----------------------------------------------------------------+
    |                                                      26/08/2015|
    +----------------------------------------------------------------+
    

    Misc Functions

    FunctionDescription
    aes_decrypt(expr, key[, mode[, padding]])Returns a decrypted value of expr using AES in mode with padding. Key lengths of 16, 24 and 32 bits are supported. Supported combinations of (mode, padding) are (‘ECB’, ‘PKCS’) and (‘GCM’, ‘NONE’). The default mode is GCM.
    aes_encrypt(expr, key[, mode[, padding]])Returns an encrypted value of expr using AES in given mode with the specified padding. Key lengths of 16, 24 and 32 bits are supported. Supported combinations of (mode, padding) are (‘ECB’, ‘PKCS’) and (‘GCM’, ‘NONE’). The default mode is GCM.
    assert_true(expr)Throws an exception if expr is not true.
    current_catalog()Returns the current catalog.
    current_database()Returns the current database.
    current_schema()Returns the current database.
    current_user()user name of current execution context.
    equal_null(expr1, expr2)Returns same result as the EQUAL(=) operator for non-null operands, but returns true if both are null, false if one of the them is null.
    input_file_block_length()Returns the length of the block being read, or -1 if not available.
    input_file_block_start()Returns the start offset of the block being read, or -1 if not available.
    input_file_name()Returns the name of the file being read, or empty string if not available.
    java_method(class, method[, arg1[, arg2 ..]])Calls a method with reflection.
    monotonically_increasing_id()Returns monotonically increasing 64-bit integers. The generated ID is guaranteed to be monotonically increasing and unique, but not consecutive. The current implementation puts the partition ID in the upper 31 bits, and the lower 33 bits represent the record number within each partition. The assumption is that the data frame has less than 1 billion partitions, and each partition has less than 8 billion records. The function is non-deterministic because its result depends on partition IDs.
    reflect(class, method[, arg1[, arg2 ..]])Calls a method with reflection.
    spark_partition_id()Returns the current partition id.
    typeof(expr)Return DDL-formatted type string for the data type of the input.
    user()user name of current execution context.
    uuid()Returns an universally unique identifier (UUID) string. The value is returned as a canonical UUID 36-character string.
    version()Returns the Spark version. The string contains 2 fields, the first being a release version and the second being a git revision.

    Examples

    -- aes_decrypt
    SELECT aes_decrypt(unhex('83F16B2AA704794132802D248E6BFD4E380078182D1544813898AC97E709B28A94'), '0000111122223333');
    +----------------------------------------------------------------------------------------------------------------------+
    |aes_decrypt(unhex(83F16B2AA704794132802D248E6BFD4E380078182D1544813898AC97E709B28A94), 0000111122223333, GCM, DEFAULT)|
    +----------------------------------------------------------------------------------------------------------------------+
    |                                                                                                      [53 70 61 72 6B]|
    +----------------------------------------------------------------------------------------------------------------------+
    
    SELECT aes_decrypt(unhex('6E7CA17BBB468D3084B5744BCA729FB7B2B7BCB8E4472847D02670489D95FA97DBBA7D3210'), '0000111122223333', 'GCM');
    +------------------------------------------------------------------------------------------------------------------------------+
    |aes_decrypt(unhex(6E7CA17BBB468D3084B5744BCA729FB7B2B7BCB8E4472847D02670489D95FA97DBBA7D3210), 0000111122223333, GCM, DEFAULT)|
    +------------------------------------------------------------------------------------------------------------------------------+
    |                                                                                                          [53 70 61 72 6B 2...|
    +------------------------------------------------------------------------------------------------------------------------------+
    
    SELECT aes_decrypt(unbase64('3lmwu+Mw0H3fi5NDvcu9lg=='), '1234567890abcdef', 'ECB', 'PKCS');
    +----------------------------------------------------------------------------+
    |aes_decrypt(unbase64(3lmwu+Mw0H3fi5NDvcu9lg==), 1234567890abcdef, ECB, PKCS)|
    +----------------------------------------------------------------------------+
    |                                                        [53 70 61 72 6B 2...|
    +----------------------------------------------------------------------------+
    
    -- aes_encrypt
    SELECT hex(aes_encrypt('Spark', '0000111122223333'));
    +-------------------------------------------------------+
    |hex(aes_encrypt(Spark, 0000111122223333, GCM, DEFAULT))|
    +-------------------------------------------------------+
    |                                   43D03E9172CD78517...|
    +-------------------------------------------------------+
    
    SELECT hex(aes_encrypt('Spark SQL', '0000111122223333', 'GCM'));
    +-----------------------------------------------------------+
    |hex(aes_encrypt(Spark SQL, 0000111122223333, GCM, DEFAULT))|
    +-----------------------------------------------------------+
    |                                       D1D460D10FB95C4D6...|
    +-----------------------------------------------------------+
    
    SELECT base64(aes_encrypt('Spark SQL', '1234567890abcdef', 'ECB', 'PKCS'));
    +-----------------------------------------------------------+
    |base64(aes_encrypt(Spark SQL, 1234567890abcdef, ECB, PKCS))|
    +-----------------------------------------------------------+
    |                                       3lmwu+Mw0H3fi5NDv...|
    +-----------------------------------------------------------+
    
    -- assert_true
    SELECT assert_true(0 < 1);
    +--------------------------------------------+
    |assert_true((0 < 1), '(0 < 1)' is not true!)|
    +--------------------------------------------+
    |                                        null|
    +--------------------------------------------+
    
    -- current_catalog
    SELECT current_catalog();
    +-----------------+
    |current_catalog()|
    +-----------------+
    |    spark_catalog|
    +-----------------+
    
    -- current_database
    SELECT current_database();
    +------------------+
    |current_database()|
    +------------------+
    |           default|
    +------------------+
    
    -- current_schema
    SELECT current_schema();
    +------------------+
    |current_database()|
    +------------------+
    |           default|
    +------------------+
    
    -- current_user
    SELECT current_user();
    +--------------+
    |current_user()|
    +--------------+
    |      spark-rm|
    +--------------+
    
    -- equal_null
    SELECT equal_null(3, 3);
    +----------------+
    |equal_null(3, 3)|
    +----------------+
    |            true|
    +----------------+
    
    SELECT equal_null(1, '11');
    +-----------------+
    |equal_null(1, 11)|
    +-----------------+
    |            false|
    +-----------------+
    
    SELECT equal_null(true, NULL);
    +----------------------+
    |equal_null(true, NULL)|
    +----------------------+
    |                 false|
    +----------------------+
    
    SELECT equal_null(NULL, 'abc');
    +---------------------+
    |equal_null(NULL, abc)|
    +---------------------+
    |                false|
    +---------------------+
    
    SELECT equal_null(NULL, NULL);
    +----------------------+
    |equal_null(NULL, NULL)|
    +----------------------+
    |                  true|
    +----------------------+
    
    -- input_file_block_length
    SELECT input_file_block_length();
    +-------------------------+
    |input_file_block_length()|
    +-------------------------+
    |                       -1|
    +-------------------------+
    
    -- input_file_block_start
    SELECT input_file_block_start();
    +------------------------+
    |input_file_block_start()|
    +------------------------+
    |                      -1|
    +------------------------+
    
    -- input_file_name
    SELECT input_file_name();
    +-----------------+
    |input_file_name()|
    +-----------------+
    |                 |
    +-----------------+
    
    -- java_method
    SELECT java_method('java.util.UUID', 'randomUUID');
    +---------------------------------------+
    |java_method(java.util.UUID, randomUUID)|
    +---------------------------------------+
    |                   c08fe6f2-5c7d-427...|
    +---------------------------------------+
    
    SELECT java_method('java.util.UUID', 'fromString', 'a5cf6c42-0c85-418f-af6c-3e4e5b1328f2');
    +-----------------------------------------------------------------------------+
    |java_method(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2)|
    +-----------------------------------------------------------------------------+
    |                                                         a5cf6c42-0c85-418...|
    +-----------------------------------------------------------------------------+
    
    -- monotonically_increasing_id
    SELECT monotonically_increasing_id();
    +-----------------------------+
    |monotonically_increasing_id()|
    +-----------------------------+
    |                            0|
    +-----------------------------+
    
    -- reflect
    SELECT reflect('java.util.UUID', 'randomUUID');
    +-----------------------------------+
    |reflect(java.util.UUID, randomUUID)|
    +-----------------------------------+
    |               73ac8e16-2ec2-48b...|
    +-----------------------------------+
    
    SELECT reflect('java.util.UUID', 'fromString', 'a5cf6c42-0c85-418f-af6c-3e4e5b1328f2');
    +-------------------------------------------------------------------------+
    |reflect(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2)|
    +-------------------------------------------------------------------------+
    |                                                     a5cf6c42-0c85-418...|
    +-------------------------------------------------------------------------+
    
    -- spark_partition_id
    SELECT spark_partition_id();
    +--------------------+
    |SPARK_PARTITION_ID()|
    +--------------------+
    |                   0|
    +--------------------+
    
    -- typeof
    SELECT typeof(1);
    +---------+
    |typeof(1)|
    +---------+
    |      int|
    +---------+
    
    SELECT typeof(array(1));
    +----------------+
    |typeof(array(1))|
    +----------------+
    |      array<int>|
    +----------------+
    
    -- user
    SELECT user();
    +--------------+
    |current_user()|
    +--------------+
    |      spark-rm|
    +--------------+
    
    -- uuid
    SELECT uuid();
    +--------------------+
    |              uuid()|
    +--------------------+
    |ef166921-f2b5-412...|
    +--------------------+
    
    -- version
    SELECT version();
    +--------------------+
    |           version()|
    +--------------------+
    |3.4.0 87a5442f7ed...|
    +--------------------+
    

    Generator Functions

    FunctionDescription
    explode(expr)Separates the elements of array expr into multiple rows, or the elements of map expr into multiple rows and columns. Unless specified otherwise, uses the default column name col for elements of the array or key and value for the elements of the map.
    explode_outer(expr)Separates the elements of array expr into multiple rows, or the elements of map expr into multiple rows and columns. Unless specified otherwise, uses the default column name col for elements of the array or key and value for the elements of the map.
    inline(expr)Explodes an array of structs into a table. Uses column names col1, col2, etc. by default unless specified otherwise.
    inline_outer(expr)Explodes an array of structs into a table. Uses column names col1, col2, etc. by default unless specified otherwise.
    posexplode(expr)Separates the elements of array expr into multiple rows with positions, or the elements of map expr into multiple rows and columns with positions. Unless specified otherwise, uses the column name pos for position, col for elements of the array or key and value for elements of the map.
    posexplode_outer(expr)Separates the elements of array expr into multiple rows with positions, or the elements of map expr into multiple rows and columns with positions. Unless specified otherwise, uses the column name pos for position, col for elements of the array or key and value for elements of the map.
    stack(n, expr1, …, exprk)Separates expr1, …, exprk into n rows. Uses column names col0, col1, etc. by default unless specified otherwise.

    Examples

    -- explode
    SELECT explode(array(10, 20));
    +---+
    |col|
    +---+
    | 10|
    | 20|
    +---+
    
    -- explode_outer
    SELECT explode_outer(array(10, 20));
    +---+
    |col|
    +---+
    | 10|
    | 20|
    +---+
    
    -- inline
    SELECT inline(array(struct(1, 'a'), struct(2, 'b')));
    +----+----+
    |col1|col2|
    +----+----+
    |   1|   a|
    |   2|   b|
    +----+----+
    
    -- inline_outer
    SELECT inline_outer(array(struct(1, 'a'), struct(2, 'b')));
    +----+----+
    |col1|col2|
    +----+----+
    |   1|   a|
    |   2|   b|
    +----+----+
    
    -- posexplode
    SELECT posexplode(array(10,20));
    +---+---+
    |pos|col|
    +---+---+
    |  0| 10|
    |  1| 20|
    +---+---+
    
    -- posexplode_outer
    SELECT posexplode_outer(array(10,20));
    +---+---+
    |pos|col|
    +---+---+
    |  0| 10|
    |  1| 20|
    +---+---+
    
    -- stack
    SELECT stack(2, 1, 2, 3);
    +----+----+
    |col0|col1|
    +----+----+
    |   1|   2|
    |   3|null|
    +----+----+