Built-in Functions

Aggregate Functions

FunctionDescription
any(expr)Returns true if at least one value of expr is true.
any_value(expr[, isIgnoreNull])Returns some value of expr for a group of rows. If isIgnoreNull is true, returns only non-null values.
approx_count_distinct(expr[, relativeSD])Returns the estimated cardinality by HyperLogLog++. relativeSD defines the maximum relative standard deviation allowed.
approx_percentile(col, percentage [, accuracy])Returns the approximate percentile of the numeric or ansi interval column col which is the smallest value in the ordered col values (sorted from least to greatest) such that no more than percentage of col values is less than the value or equal to that value. The value of percentage must be between 0.0 and 1.0. The accuracy parameter (default: 10000) is a positive numeric literal which controls approximation accuracy at the cost of memory. Higher value of accuracy yields better accuracy, 1.0/accuracy is the relative error of the approximation. When percentage is an array, each value of the percentage array must be between 0.0 and 1.0. In this case, returns the approximate percentile array of column col at the given percentage array.
array_agg(expr)Collects and returns a list of non-unique elements.
avg(expr)Returns the mean calculated from values of a group.
bit_and(expr)Returns the bitwise AND of all non-null input values, or null if none.
bit_or(expr)Returns the bitwise OR of all non-null input values, or null if none.
bit_xor(expr)Returns the bitwise XOR of all non-null input values, or null if none.
bool_and(expr)Returns true if all values of expr are true.
bool_or(expr)Returns true if at least one value of expr is true.
collect_list(expr)Collects and returns a list of non-unique elements.
collect_set(expr)Collects and returns a set of unique elements.
corr(expr1, expr2)Returns Pearson coefficient of correlation between a set of number pairs.
count()Returns the total number of retrieved rows, including rows containing null.
count(expr[, expr…])Returns the number of rows for which the supplied expression(s) are all non-null.
count(DISTINCT expr[, expr…])Returns the number of rows for which the supplied expression(s) are unique and non-null.
count_if(expr)Returns the number of TRUE values for the expression.
count_min_sketch(col, eps, confidence, seed)Returns a count-min sketch of a column with the given esp, confidence and seed. The result is an array of bytes, which can be deserialized to a CountMinSketch before usage. Count-min sketch is a probabilistic data structure used for cardinality estimation using sub-linear space.
covar_pop(expr1, expr2)Returns the population covariance of a set of number pairs.
covar_samp(expr1, expr2)Returns the sample covariance of a set of number pairs.
every(expr)Returns true if all values of expr are true.
first(expr[, isIgnoreNull])Returns the first value of expr for a group of rows. If isIgnoreNull is true, returns only non-null values.
first_value(expr[, isIgnoreNull])Returns the first value of expr for a group of rows. If isIgnoreNull is true, returns only non-null values.
grouping(col)indicates whether a specified column in a GROUP BY is aggregated or not, returns 1 for aggregated or 0 for not aggregated in the result set.”,
grouping_id([col1[, col2 ..]])returns the level of grouping, equals to (grouping(c1) << (n-1)) + (grouping(c2) << (n-2)) + ... + grouping(cn)
histogram_numeric(expr, nb)Computes a histogram on numeric ‘expr’ using nb bins. The return value is an array of (x,y) pairs representing the centers of the histogram’s bins. As the value of ‘nb’ is increased, the histogram approximation gets finer-grained, but may yield artifacts around outliers. In practice, 20-40 histogram bins appear to work well, with more bins being required for skewed or smaller datasets. Note that this function creates a histogram with non-uniform bin widths. It offers no guarantees in terms of the mean-squared-error of the histogram, but in practice is comparable to the histograms produced by the R/S-Plus statistical computing packages. Note: the output type of the ‘x’ field in the return value is propagated from the input value consumed in the aggregate function.
kurtosis(expr)Returns the kurtosis value calculated from values of a group.
last(expr[, isIgnoreNull])Returns the last value of expr for a group of rows. If isIgnoreNull is true, returns only non-null values
last_value(expr[, isIgnoreNull])Returns the last value of expr for a group of rows. If isIgnoreNull is true, returns only non-null values
max(expr)Returns the maximum value of expr.
max_by(x, y)Returns the value of x associated with the maximum value of y.
mean(expr)Returns the mean calculated from values of a group.
median(col)Returns the median of numeric or ANSI interval column col.
min(expr)Returns the minimum value of expr.
min_by(x, y)Returns the value of x associated with the minimum value of y.
mode(col)Returns the most frequent value for the values within col. NULL values are ignored. If all the values are NULL, or there are 0 rows, returns NULL.
percentile(col, percentage [, frequency])Returns the exact percentile value of numeric or ANSI interval column col at the given percentage. The value of percentage must be between 0.0 and 1.0. The value of frequency should be positive integral
percentile(col, array(percentage1 [, percentage2]…) [, frequency])Returns the exact percentile value array of numeric column col at the given percentage(s). Each value of the percentage array must be between 0.0 and 1.0. The value of frequency should be positive integral
percentile_approx(col, percentage [, accuracy])Returns the approximate percentile of the numeric or ansi interval column col which is the smallest value in the ordered col values (sorted from least to greatest) such that no more than percentage of col values is less than the value or equal to that value. The value of percentage must be between 0.0 and 1.0. The accuracy parameter (default: 10000) is a positive numeric literal which controls approximation accuracy at the cost of memory. Higher value of accuracy yields better accuracy, 1.0/accuracy is the relative error of the approximation. When percentage is an array, each value of the percentage array must be between 0.0 and 1.0. In this case, returns the approximate percentile array of column col at the given percentage array.
regr_avgx(y, x)Returns the average of the independent variable for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_avgy(y, x)Returns the average of the dependent variable for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_count(y, x)Returns the number of non-null number pairs in a group, where y is the dependent variable and x is the independent variable.
regr_intercept(y, x)Returns the intercept of the univariate linear regression line for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_r2(y, x)Returns the coefficient of determination for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_slope(y, x)Returns the slope of the linear regression line for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_sxx(y, x)Returns REGR_COUNT(y, x) VAR_POP(x) for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_sxy(y, x)Returns REGR_COUNT(y, x) COVAR_POP(y, x) for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
regr_syy(y, x)Returns REGR_COUNT(y, x) VAR_POP(y) for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
skewness(expr)Returns the skewness value calculated from values of a group.
some(expr)Returns true if at least one value of expr is true.
std(expr)Returns the sample standard deviation calculated from values of a group.
stddev(expr)Returns the sample standard deviation calculated from values of a group.
stddev_pop(expr)Returns the population standard deviation calculated from values of a group.
stddev_samp(expr)Returns the sample standard deviation calculated from values of a group.
sum(expr)Returns the sum calculated from values of a group.
try_avg(expr)Returns the mean calculated from values of a group and the result is null on overflow.
try_sum(expr)Returns the sum calculated from values of a group and the result is null on overflow.
var_pop(expr)Returns the population variance calculated from values of a group.
var_samp(expr)Returns the sample variance calculated from values of a group.
variance(expr)Returns the sample variance calculated from values of a group.

Examples

  1. -- any
  2. SELECT any(col) FROM VALUES (true), (false), (false) AS tab(col);
  3. +--------+
  4. |any(col)|
  5. +--------+
  6. | true|
  7. +--------+
  8. SELECT any(col) FROM VALUES (NULL), (true), (false) AS tab(col);
  9. +--------+
  10. |any(col)|
  11. +--------+
  12. | true|
  13. +--------+
  14. SELECT any(col) FROM VALUES (false), (false), (NULL) AS tab(col);
  15. +--------+
  16. |any(col)|
  17. +--------+
  18. | false|
  19. +--------+
  20. -- any_value
  21. SELECT any_value(col) FROM VALUES (10), (5), (20) AS tab(col);
  22. +--------------+
  23. |any_value(col)|
  24. +--------------+
  25. | 10|
  26. +--------------+
  27. SELECT any_value(col) FROM VALUES (NULL), (5), (20) AS tab(col);
  28. +--------------+
  29. |any_value(col)|
  30. +--------------+
  31. | null|
  32. +--------------+
  33. SELECT any_value(col, true) FROM VALUES (NULL), (5), (20) AS tab(col);
  34. +--------------+
  35. |any_value(col)|
  36. +--------------+
  37. | 5|
  38. +--------------+
  39. -- approx_count_distinct
  40. SELECT approx_count_distinct(col1) FROM VALUES (1), (1), (2), (2), (3) tab(col1);
  41. +---------------------------+
  42. |approx_count_distinct(col1)|
  43. +---------------------------+
  44. | 3|
  45. +---------------------------+
  46. -- approx_percentile
  47. SELECT approx_percentile(col, array(0.5, 0.4, 0.1), 100) FROM VALUES (0), (1), (2), (10) AS tab(col);
  48. +-------------------------------------------------+
  49. |approx_percentile(col, array(0.5, 0.4, 0.1), 100)|
  50. +-------------------------------------------------+
  51. | [1, 1, 0]|
  52. +-------------------------------------------------+
  53. SELECT approx_percentile(col, 0.5, 100) FROM VALUES (0), (6), (7), (9), (10) AS tab(col);
  54. +--------------------------------+
  55. |approx_percentile(col, 0.5, 100)|
  56. +--------------------------------+
  57. | 7|
  58. +--------------------------------+
  59. SELECT approx_percentile(col, 0.5, 100) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '1' MONTH), (INTERVAL '2' MONTH), (INTERVAL '10' MONTH) AS tab(col);
  60. +--------------------------------+
  61. |approx_percentile(col, 0.5, 100)|
  62. +--------------------------------+
  63. | INTERVAL '1' MONTH|
  64. +--------------------------------+
  65. SELECT approx_percentile(col, array(0.5, 0.7), 100) FROM VALUES (INTERVAL '0' SECOND), (INTERVAL '1' SECOND), (INTERVAL '2' SECOND), (INTERVAL '10' SECOND) AS tab(col);
  66. +--------------------------------------------+
  67. |approx_percentile(col, array(0.5, 0.7), 100)|
  68. +--------------------------------------------+
  69. | [INTERVAL '01' SE...|
  70. +--------------------------------------------+
  71. -- array_agg
  72. SELECT array_agg(col) FROM VALUES (1), (2), (1) AS tab(col);
  73. +-----------------+
  74. |collect_list(col)|
  75. +-----------------+
  76. | [1, 2, 1]|
  77. +-----------------+
  78. -- avg
  79. SELECT avg(col) FROM VALUES (1), (2), (3) AS tab(col);
  80. +--------+
  81. |avg(col)|
  82. +--------+
  83. | 2.0|
  84. +--------+
  85. SELECT avg(col) FROM VALUES (1), (2), (NULL) AS tab(col);
  86. +--------+
  87. |avg(col)|
  88. +--------+
  89. | 1.5|
  90. +--------+
  91. -- bit_and
  92. SELECT bit_and(col) FROM VALUES (3), (5) AS tab(col);
  93. +------------+
  94. |bit_and(col)|
  95. +------------+
  96. | 1|
  97. +------------+
  98. -- bit_or
  99. SELECT bit_or(col) FROM VALUES (3), (5) AS tab(col);
  100. +-----------+
  101. |bit_or(col)|
  102. +-----------+
  103. | 7|
  104. +-----------+
  105. -- bit_xor
  106. SELECT bit_xor(col) FROM VALUES (3), (5) AS tab(col);
  107. +------------+
  108. |bit_xor(col)|
  109. +------------+
  110. | 6|
  111. +------------+
  112. -- bool_and
  113. SELECT bool_and(col) FROM VALUES (true), (true), (true) AS tab(col);
  114. +-------------+
  115. |bool_and(col)|
  116. +-------------+
  117. | true|
  118. +-------------+
  119. SELECT bool_and(col) FROM VALUES (NULL), (true), (true) AS tab(col);
  120. +-------------+
  121. |bool_and(col)|
  122. +-------------+
  123. | true|
  124. +-------------+
  125. SELECT bool_and(col) FROM VALUES (true), (false), (true) AS tab(col);
  126. +-------------+
  127. |bool_and(col)|
  128. +-------------+
  129. | false|
  130. +-------------+
  131. -- bool_or
  132. SELECT bool_or(col) FROM VALUES (true), (false), (false) AS tab(col);
  133. +------------+
  134. |bool_or(col)|
  135. +------------+
  136. | true|
  137. +------------+
  138. SELECT bool_or(col) FROM VALUES (NULL), (true), (false) AS tab(col);
  139. +------------+
  140. |bool_or(col)|
  141. +------------+
  142. | true|
  143. +------------+
  144. SELECT bool_or(col) FROM VALUES (false), (false), (NULL) AS tab(col);
  145. +------------+
  146. |bool_or(col)|
  147. +------------+
  148. | false|
  149. +------------+
  150. -- collect_list
  151. SELECT collect_list(col) FROM VALUES (1), (2), (1) AS tab(col);
  152. +-----------------+
  153. |collect_list(col)|
  154. +-----------------+
  155. | [1, 2, 1]|
  156. +-----------------+
  157. -- collect_set
  158. SELECT collect_set(col) FROM VALUES (1), (2), (1) AS tab(col);
  159. +----------------+
  160. |collect_set(col)|
  161. +----------------+
  162. | [1, 2]|
  163. +----------------+
  164. -- corr
  165. SELECT corr(c1, c2) FROM VALUES (3, 2), (3, 3), (6, 4) as tab(c1, c2);
  166. +------------------+
  167. | corr(c1, c2)|
  168. +------------------+
  169. |0.8660254037844387|
  170. +------------------+
  171. -- count
  172. SELECT count(*) FROM VALUES (NULL), (5), (5), (20) AS tab(col);
  173. +--------+
  174. |count(1)|
  175. +--------+
  176. | 4|
  177. +--------+
  178. SELECT count(col) FROM VALUES (NULL), (5), (5), (20) AS tab(col);
  179. +----------+
  180. |count(col)|
  181. +----------+
  182. | 3|
  183. +----------+
  184. SELECT count(DISTINCT col) FROM VALUES (NULL), (5), (5), (10) AS tab(col);
  185. +-------------------+
  186. |count(DISTINCT col)|
  187. +-------------------+
  188. | 2|
  189. +-------------------+
  190. -- count_if
  191. SELECT count_if(col % 2 = 0) FROM VALUES (NULL), (0), (1), (2), (3) AS tab(col);
  192. +-------------------------+
  193. |count_if(((col % 2) = 0))|
  194. +-------------------------+
  195. | 2|
  196. +-------------------------+
  197. SELECT count_if(col IS NULL) FROM VALUES (NULL), (0), (1), (2), (3) AS tab(col);
  198. +-----------------------+
  199. |count_if((col IS NULL))|
  200. +-----------------------+
  201. | 1|
  202. +-----------------------+
  203. -- count_min_sketch
  204. SELECT hex(count_min_sketch(col, 0.5d, 0.5d, 1)) FROM VALUES (1), (2), (1) AS tab(col);
  205. +---------------------------------------+
  206. |hex(count_min_sketch(col, 0.5, 0.5, 1))|
  207. +---------------------------------------+
  208. | 00000001000000000...|
  209. +---------------------------------------+
  210. -- covar_pop
  211. SELECT covar_pop(c1, c2) FROM VALUES (1,1), (2,2), (3,3) AS tab(c1, c2);
  212. +------------------+
  213. | covar_pop(c1, c2)|
  214. +------------------+
  215. |0.6666666666666666|
  216. +------------------+
  217. -- covar_samp
  218. SELECT covar_samp(c1, c2) FROM VALUES (1,1), (2,2), (3,3) AS tab(c1, c2);
  219. +------------------+
  220. |covar_samp(c1, c2)|
  221. +------------------+
  222. | 1.0|
  223. +------------------+
  224. -- every
  225. SELECT every(col) FROM VALUES (true), (true), (true) AS tab(col);
  226. +----------+
  227. |every(col)|
  228. +----------+
  229. | true|
  230. +----------+
  231. SELECT every(col) FROM VALUES (NULL), (true), (true) AS tab(col);
  232. +----------+
  233. |every(col)|
  234. +----------+
  235. | true|
  236. +----------+
  237. SELECT every(col) FROM VALUES (true), (false), (true) AS tab(col);
  238. +----------+
  239. |every(col)|
  240. +----------+
  241. | false|
  242. +----------+
  243. -- first
  244. SELECT first(col) FROM VALUES (10), (5), (20) AS tab(col);
  245. +----------+
  246. |first(col)|
  247. +----------+
  248. | 10|
  249. +----------+
  250. SELECT first(col) FROM VALUES (NULL), (5), (20) AS tab(col);
  251. +----------+
  252. |first(col)|
  253. +----------+
  254. | null|
  255. +----------+
  256. SELECT first(col, true) FROM VALUES (NULL), (5), (20) AS tab(col);
  257. +----------+
  258. |first(col)|
  259. +----------+
  260. | 5|
  261. +----------+
  262. -- first_value
  263. SELECT first_value(col) FROM VALUES (10), (5), (20) AS tab(col);
  264. +----------------+
  265. |first_value(col)|
  266. +----------------+
  267. | 10|
  268. +----------------+
  269. SELECT first_value(col) FROM VALUES (NULL), (5), (20) AS tab(col);
  270. +----------------+
  271. |first_value(col)|
  272. +----------------+
  273. | null|
  274. +----------------+
  275. SELECT first_value(col, true) FROM VALUES (NULL), (5), (20) AS tab(col);
  276. +----------------+
  277. |first_value(col)|
  278. +----------------+
  279. | 5|
  280. +----------------+
  281. -- grouping
  282. SELECT name, grouping(name), sum(age) FROM VALUES (2, 'Alice'), (5, 'Bob') people(age, name) GROUP BY cube(name);
  283. +-----+--------------+--------+
  284. | name|grouping(name)|sum(age)|
  285. +-----+--------------+--------+
  286. | null| 1| 7|
  287. |Alice| 0| 2|
  288. | Bob| 0| 5|
  289. +-----+--------------+--------+
  290. -- grouping_id
  291. SELECT name, grouping_id(), sum(age), avg(height) FROM VALUES (2, 'Alice', 165), (5, 'Bob', 180) people(age, name, height) GROUP BY cube(name, height);
  292. +-----+-------------+--------+-----------+
  293. | name|grouping_id()|sum(age)|avg(height)|
  294. +-----+-------------+--------+-----------+
  295. | null| 2| 2| 165.0|
  296. |Alice| 0| 2| 165.0|
  297. |Alice| 1| 2| 165.0|
  298. | null| 3| 7| 172.5|
  299. | Bob| 1| 5| 180.0|
  300. | Bob| 0| 5| 180.0|
  301. | null| 2| 5| 180.0|
  302. +-----+-------------+--------+-----------+
  303. -- histogram_numeric
  304. SELECT histogram_numeric(col, 5) FROM VALUES (0), (1), (2), (10) AS tab(col);
  305. +-------------------------+
  306. |histogram_numeric(col, 5)|
  307. +-------------------------+
  308. | [{0, 1.0}, {1, 1....|
  309. +-------------------------+
  310. -- kurtosis
  311. SELECT kurtosis(col) FROM VALUES (-10), (-20), (100), (1000) AS tab(col);
  312. +-------------------+
  313. | kurtosis(col)|
  314. +-------------------+
  315. |-0.7014368047529618|
  316. +-------------------+
  317. SELECT kurtosis(col) FROM VALUES (1), (10), (100), (10), (1) as tab(col);
  318. +-------------------+
  319. | kurtosis(col)|
  320. +-------------------+
  321. |0.19432323191698986|
  322. +-------------------+
  323. -- last
  324. SELECT last(col) FROM VALUES (10), (5), (20) AS tab(col);
  325. +---------+
  326. |last(col)|
  327. +---------+
  328. | 20|
  329. +---------+
  330. SELECT last(col) FROM VALUES (10), (5), (NULL) AS tab(col);
  331. +---------+
  332. |last(col)|
  333. +---------+
  334. | null|
  335. +---------+
  336. SELECT last(col, true) FROM VALUES (10), (5), (NULL) AS tab(col);
  337. +---------+
  338. |last(col)|
  339. +---------+
  340. | 5|
  341. +---------+
  342. -- last_value
  343. SELECT last_value(col) FROM VALUES (10), (5), (20) AS tab(col);
  344. +---------------+
  345. |last_value(col)|
  346. +---------------+
  347. | 20|
  348. +---------------+
  349. SELECT last_value(col) FROM VALUES (10), (5), (NULL) AS tab(col);
  350. +---------------+
  351. |last_value(col)|
  352. +---------------+
  353. | null|
  354. +---------------+
  355. SELECT last_value(col, true) FROM VALUES (10), (5), (NULL) AS tab(col);
  356. +---------------+
  357. |last_value(col)|
  358. +---------------+
  359. | 5|
  360. +---------------+
  361. -- max
  362. SELECT max(col) FROM VALUES (10), (50), (20) AS tab(col);
  363. +--------+
  364. |max(col)|
  365. +--------+
  366. | 50|
  367. +--------+
  368. -- max_by
  369. SELECT max_by(x, y) FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y);
  370. +------------+
  371. |max_by(x, y)|
  372. +------------+
  373. | b|
  374. +------------+
  375. -- mean
  376. SELECT mean(col) FROM VALUES (1), (2), (3) AS tab(col);
  377. +---------+
  378. |mean(col)|
  379. +---------+
  380. | 2.0|
  381. +---------+
  382. SELECT mean(col) FROM VALUES (1), (2), (NULL) AS tab(col);
  383. +---------+
  384. |mean(col)|
  385. +---------+
  386. | 1.5|
  387. +---------+
  388. -- median
  389. SELECT median(col) FROM VALUES (0), (10) AS tab(col);
  390. +-----------+
  391. |median(col)|
  392. +-----------+
  393. | 5.0|
  394. +-----------+
  395. SELECT median(col) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '10' MONTH) AS tab(col);
  396. +--------------------+
  397. | median(col)|
  398. +--------------------+
  399. |INTERVAL '0-5' YE...|
  400. +--------------------+
  401. -- min
  402. SELECT min(col) FROM VALUES (10), (-1), (20) AS tab(col);
  403. +--------+
  404. |min(col)|
  405. +--------+
  406. | -1|
  407. +--------+
  408. -- min_by
  409. SELECT min_by(x, y) FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y);
  410. +------------+
  411. |min_by(x, y)|
  412. +------------+
  413. | a|
  414. +------------+
  415. -- mode
  416. SELECT mode(col) FROM VALUES (0), (10), (10) AS tab(col);
  417. +---------+
  418. |mode(col)|
  419. +---------+
  420. | 10|
  421. +---------+
  422. SELECT mode(col) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '10' MONTH), (INTERVAL '10' MONTH) AS tab(col);
  423. +-------------------+
  424. | mode(col)|
  425. +-------------------+
  426. |INTERVAL '10' MONTH|
  427. +-------------------+
  428. SELECT mode(col) FROM VALUES (0), (10), (10), (null), (null), (null) AS tab(col);
  429. +---------+
  430. |mode(col)|
  431. +---------+
  432. | 10|
  433. +---------+
  434. -- percentile
  435. SELECT percentile(col, 0.3) FROM VALUES (0), (10) AS tab(col);
  436. +-----------------------+
  437. |percentile(col, 0.3, 1)|
  438. +-----------------------+
  439. | 3.0|
  440. +-----------------------+
  441. SELECT percentile(col, array(0.25, 0.75)) FROM VALUES (0), (10) AS tab(col);
  442. +-------------------------------------+
  443. |percentile(col, array(0.25, 0.75), 1)|
  444. +-------------------------------------+
  445. | [2.5, 7.5]|
  446. +-------------------------------------+
  447. SELECT percentile(col, 0.5) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '10' MONTH) AS tab(col);
  448. +-----------------------+
  449. |percentile(col, 0.5, 1)|
  450. +-----------------------+
  451. | INTERVAL '0-5' YE...|
  452. +-----------------------+
  453. SELECT percentile(col, array(0.2, 0.5)) FROM VALUES (INTERVAL '0' SECOND), (INTERVAL '10' SECOND) AS tab(col);
  454. +-----------------------------------+
  455. |percentile(col, array(0.2, 0.5), 1)|
  456. +-----------------------------------+
  457. | [INTERVAL '0 00:0...|
  458. +-----------------------------------+
  459. -- percentile_approx
  460. SELECT percentile_approx(col, array(0.5, 0.4, 0.1), 100) FROM VALUES (0), (1), (2), (10) AS tab(col);
  461. +-------------------------------------------------+
  462. |percentile_approx(col, array(0.5, 0.4, 0.1), 100)|
  463. +-------------------------------------------------+
  464. | [1, 1, 0]|
  465. +-------------------------------------------------+
  466. SELECT percentile_approx(col, 0.5, 100) FROM VALUES (0), (6), (7), (9), (10) AS tab(col);
  467. +--------------------------------+
  468. |percentile_approx(col, 0.5, 100)|
  469. +--------------------------------+
  470. | 7|
  471. +--------------------------------+
  472. SELECT percentile_approx(col, 0.5, 100) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '1' MONTH), (INTERVAL '2' MONTH), (INTERVAL '10' MONTH) AS tab(col);
  473. +--------------------------------+
  474. |percentile_approx(col, 0.5, 100)|
  475. +--------------------------------+
  476. | INTERVAL '1' MONTH|
  477. +--------------------------------+
  478. SELECT percentile_approx(col, array(0.5, 0.7), 100) FROM VALUES (INTERVAL '0' SECOND), (INTERVAL '1' SECOND), (INTERVAL '2' SECOND), (INTERVAL '10' SECOND) AS tab(col);
  479. +--------------------------------------------+
  480. |percentile_approx(col, array(0.5, 0.7), 100)|
  481. +--------------------------------------------+
  482. | [INTERVAL '01' SE...|
  483. +--------------------------------------------+
  484. -- regr_avgx
  485. SELECT regr_avgx(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  486. +---------------+
  487. |regr_avgx(y, x)|
  488. +---------------+
  489. | 2.75|
  490. +---------------+
  491. SELECT regr_avgx(y, x) FROM VALUES (1, null) AS tab(y, x);
  492. +---------------+
  493. |regr_avgx(y, x)|
  494. +---------------+
  495. | null|
  496. +---------------+
  497. SELECT regr_avgx(y, x) FROM VALUES (null, 1) AS tab(y, x);
  498. +---------------+
  499. |regr_avgx(y, x)|
  500. +---------------+
  501. | null|
  502. +---------------+
  503. SELECT regr_avgx(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  504. +---------------+
  505. |regr_avgx(y, x)|
  506. +---------------+
  507. | 3.0|
  508. +---------------+
  509. SELECT regr_avgx(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  510. +---------------+
  511. |regr_avgx(y, x)|
  512. +---------------+
  513. | 3.0|
  514. +---------------+
  515. -- regr_avgy
  516. SELECT regr_avgy(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  517. +---------------+
  518. |regr_avgy(y, x)|
  519. +---------------+
  520. | 1.75|
  521. +---------------+
  522. SELECT regr_avgy(y, x) FROM VALUES (1, null) AS tab(y, x);
  523. +---------------+
  524. |regr_avgy(y, x)|
  525. +---------------+
  526. | null|
  527. +---------------+
  528. SELECT regr_avgy(y, x) FROM VALUES (null, 1) AS tab(y, x);
  529. +---------------+
  530. |regr_avgy(y, x)|
  531. +---------------+
  532. | null|
  533. +---------------+
  534. SELECT regr_avgy(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  535. +------------------+
  536. | regr_avgy(y, x)|
  537. +------------------+
  538. |1.6666666666666667|
  539. +------------------+
  540. SELECT regr_avgy(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  541. +---------------+
  542. |regr_avgy(y, x)|
  543. +---------------+
  544. | 1.5|
  545. +---------------+
  546. -- regr_count
  547. SELECT regr_count(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  548. +----------------+
  549. |regr_count(y, x)|
  550. +----------------+
  551. | 4|
  552. +----------------+
  553. SELECT regr_count(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  554. +----------------+
  555. |regr_count(y, x)|
  556. +----------------+
  557. | 3|
  558. +----------------+
  559. SELECT regr_count(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  560. +----------------+
  561. |regr_count(y, x)|
  562. +----------------+
  563. | 2|
  564. +----------------+
  565. -- regr_intercept
  566. SELECT regr_intercept(y, x) FROM VALUES (1,1), (2,2), (3,3) AS tab(y, x);
  567. +--------------------+
  568. |regr_intercept(y, x)|
  569. +--------------------+
  570. | 0.0|
  571. +--------------------+
  572. SELECT regr_intercept(y, x) FROM VALUES (1, null) AS tab(y, x);
  573. +--------------------+
  574. |regr_intercept(y, x)|
  575. +--------------------+
  576. | null|
  577. +--------------------+
  578. SELECT regr_intercept(y, x) FROM VALUES (null, 1) AS tab(y, x);
  579. +--------------------+
  580. |regr_intercept(y, x)|
  581. +--------------------+
  582. | null|
  583. +--------------------+
  584. -- regr_r2
  585. SELECT regr_r2(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  586. +------------------+
  587. | regr_r2(y, x)|
  588. +------------------+
  589. |0.2727272727272726|
  590. +------------------+
  591. SELECT regr_r2(y, x) FROM VALUES (1, null) AS tab(y, x);
  592. +-------------+
  593. |regr_r2(y, x)|
  594. +-------------+
  595. | null|
  596. +-------------+
  597. SELECT regr_r2(y, x) FROM VALUES (null, 1) AS tab(y, x);
  598. +-------------+
  599. |regr_r2(y, x)|
  600. +-------------+
  601. | null|
  602. +-------------+
  603. SELECT regr_r2(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  604. +------------------+
  605. | regr_r2(y, x)|
  606. +------------------+
  607. |0.7500000000000001|
  608. +------------------+
  609. SELECT regr_r2(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  610. +-------------+
  611. |regr_r2(y, x)|
  612. +-------------+
  613. | 1.0|
  614. +-------------+
  615. -- regr_slope
  616. SELECT regr_slope(y, x) FROM VALUES (1,1), (2,2), (3,3) AS tab(y, x);
  617. +----------------+
  618. |regr_slope(y, x)|
  619. +----------------+
  620. | 1.0|
  621. +----------------+
  622. SELECT regr_slope(y, x) FROM VALUES (1, null) AS tab(y, x);
  623. +----------------+
  624. |regr_slope(y, x)|
  625. +----------------+
  626. | null|
  627. +----------------+
  628. SELECT regr_slope(y, x) FROM VALUES (null, 1) AS tab(y, x);
  629. +----------------+
  630. |regr_slope(y, x)|
  631. +----------------+
  632. | null|
  633. +----------------+
  634. -- regr_sxx
  635. SELECT regr_sxx(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  636. +------------------+
  637. | regr_sxx(y, x)|
  638. +------------------+
  639. |2.7499999999999996|
  640. +------------------+
  641. SELECT regr_sxx(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  642. +--------------+
  643. |regr_sxx(y, x)|
  644. +--------------+
  645. | 2.0|
  646. +--------------+
  647. SELECT regr_sxx(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  648. +--------------+
  649. |regr_sxx(y, x)|
  650. +--------------+
  651. | 2.0|
  652. +--------------+
  653. -- regr_sxy
  654. SELECT regr_sxy(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  655. +------------------+
  656. | regr_sxy(y, x)|
  657. +------------------+
  658. |0.7499999999999998|
  659. +------------------+
  660. SELECT regr_sxy(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  661. +--------------+
  662. |regr_sxy(y, x)|
  663. +--------------+
  664. | 1.0|
  665. +--------------+
  666. SELECT regr_sxy(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  667. +--------------+
  668. |regr_sxy(y, x)|
  669. +--------------+
  670. | 1.0|
  671. +--------------+
  672. -- regr_syy
  673. SELECT regr_syy(y, x) FROM VALUES (1, 2), (2, 2), (2, 3), (2, 4) AS tab(y, x);
  674. +------------------+
  675. | regr_syy(y, x)|
  676. +------------------+
  677. |0.7499999999999999|
  678. +------------------+
  679. SELECT regr_syy(y, x) FROM VALUES (1, 2), (2, null), (2, 3), (2, 4) AS tab(y, x);
  680. +------------------+
  681. | regr_syy(y, x)|
  682. +------------------+
  683. |0.6666666666666666|
  684. +------------------+
  685. SELECT regr_syy(y, x) FROM VALUES (1, 2), (2, null), (null, 3), (2, 4) AS tab(y, x);
  686. +--------------+
  687. |regr_syy(y, x)|
  688. +--------------+
  689. | 0.5|
  690. +--------------+
  691. -- skewness
  692. SELECT skewness(col) FROM VALUES (-10), (-20), (100), (1000) AS tab(col);
  693. +------------------+
  694. | skewness(col)|
  695. +------------------+
  696. |1.1135657469022013|
  697. +------------------+
  698. SELECT skewness(col) FROM VALUES (-1000), (-100), (10), (20) AS tab(col);
  699. +-------------------+
  700. | skewness(col)|
  701. +-------------------+
  702. |-1.1135657469022011|
  703. +-------------------+
  704. -- some
  705. SELECT some(col) FROM VALUES (true), (false), (false) AS tab(col);
  706. +---------+
  707. |some(col)|
  708. +---------+
  709. | true|
  710. +---------+
  711. SELECT some(col) FROM VALUES (NULL), (true), (false) AS tab(col);
  712. +---------+
  713. |some(col)|
  714. +---------+
  715. | true|
  716. +---------+
  717. SELECT some(col) FROM VALUES (false), (false), (NULL) AS tab(col);
  718. +---------+
  719. |some(col)|
  720. +---------+
  721. | false|
  722. +---------+
  723. -- std
  724. SELECT std(col) FROM VALUES (1), (2), (3) AS tab(col);
  725. +--------+
  726. |std(col)|
  727. +--------+
  728. | 1.0|
  729. +--------+
  730. -- stddev
  731. SELECT stddev(col) FROM VALUES (1), (2), (3) AS tab(col);
  732. +-----------+
  733. |stddev(col)|
  734. +-----------+
  735. | 1.0|
  736. +-----------+
  737. -- stddev_pop
  738. SELECT stddev_pop(col) FROM VALUES (1), (2), (3) AS tab(col);
  739. +-----------------+
  740. | stddev_pop(col)|
  741. +-----------------+
  742. |0.816496580927726|
  743. +-----------------+
  744. -- stddev_samp
  745. SELECT stddev_samp(col) FROM VALUES (1), (2), (3) AS tab(col);
  746. +----------------+
  747. |stddev_samp(col)|
  748. +----------------+
  749. | 1.0|
  750. +----------------+
  751. -- sum
  752. SELECT sum(col) FROM VALUES (5), (10), (15) AS tab(col);
  753. +--------+
  754. |sum(col)|
  755. +--------+
  756. | 30|
  757. +--------+
  758. SELECT sum(col) FROM VALUES (NULL), (10), (15) AS tab(col);
  759. +--------+
  760. |sum(col)|
  761. +--------+
  762. | 25|
  763. +--------+
  764. SELECT sum(col) FROM VALUES (NULL), (NULL) AS tab(col);
  765. +--------+
  766. |sum(col)|
  767. +--------+
  768. | null|
  769. +--------+
  770. -- try_avg
  771. SELECT try_avg(col) FROM VALUES (1), (2), (3) AS tab(col);
  772. +------------+
  773. |try_avg(col)|
  774. +------------+
  775. | 2.0|
  776. +------------+
  777. SELECT try_avg(col) FROM VALUES (1), (2), (NULL) AS tab(col);
  778. +------------+
  779. |try_avg(col)|
  780. +------------+
  781. | 1.5|
  782. +------------+
  783. SELECT try_avg(col) FROM VALUES (interval '2147483647 months'), (interval '1 months') AS tab(col);
  784. +------------+
  785. |try_avg(col)|
  786. +------------+
  787. | null|
  788. +------------+
  789. -- try_sum
  790. SELECT try_sum(col) FROM VALUES (5), (10), (15) AS tab(col);
  791. +------------+
  792. |try_sum(col)|
  793. +------------+
  794. | 30|
  795. +------------+
  796. SELECT try_sum(col) FROM VALUES (NULL), (10), (15) AS tab(col);
  797. +------------+
  798. |try_sum(col)|
  799. +------------+
  800. | 25|
  801. +------------+
  802. SELECT try_sum(col) FROM VALUES (NULL), (NULL) AS tab(col);
  803. +------------+
  804. |try_sum(col)|
  805. +------------+
  806. | null|
  807. +------------+
  808. SELECT try_sum(col) FROM VALUES (9223372036854775807L), (1L) AS tab(col);
  809. +------------+
  810. |try_sum(col)|
  811. +------------+
  812. | null|
  813. +------------+
  814. -- var_pop
  815. SELECT var_pop(col) FROM VALUES (1), (2), (3) AS tab(col);
  816. +------------------+
  817. | var_pop(col)|
  818. +------------------+
  819. |0.6666666666666666|
  820. +------------------+
  821. -- var_samp
  822. SELECT var_samp(col) FROM VALUES (1), (2), (3) AS tab(col);
  823. +-------------+
  824. |var_samp(col)|
  825. +-------------+
  826. | 1.0|
  827. +-------------+
  828. -- variance
  829. SELECT variance(col) FROM VALUES (1), (2), (3) AS tab(col);
  830. +-------------+
  831. |variance(col)|
  832. +-------------+
  833. | 1.0|
  834. +-------------+

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 ‘Types of time windows’ 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 ‘Window Operations on Event Time’ 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. | 2008-12-25 15:30:...|
  599. +----------------------------------+
  600. -- timestamp_millis
  601. SELECT timestamp_millis(1230219000123);
  602. +-------------------------------+
  603. |timestamp_millis(1230219000123)|
  604. +-------------------------------+
  605. | 2008-12-25 15:30:...|
  606. +-------------------------------+
  607. -- timestamp_seconds
  608. SELECT timestamp_seconds(1230219000);
  609. +-----------------------------+
  610. |timestamp_seconds(1230219000)|
  611. +-----------------------------+
  612. | 2008-12-25 15:30:00|
  613. +-----------------------------+
  614. SELECT timestamp_seconds(1230219000.123);
  615. +---------------------------------+
  616. |timestamp_seconds(1230219000.123)|
  617. +---------------------------------+
  618. | 2008-12-25 15:30:...|
  619. +---------------------------------+
  620. -- to_date
  621. SELECT to_date('2009-07-30 04:17:52');
  622. +----------------------------+
  623. |to_date(2009-07-30 04:17:52)|
  624. +----------------------------+
  625. | 2009-07-30|
  626. +----------------------------+
  627. SELECT to_date('2016-12-31', 'yyyy-MM-dd');
  628. +-------------------------------+
  629. |to_date(2016-12-31, yyyy-MM-dd)|
  630. +-------------------------------+
  631. | 2016-12-31|
  632. +-------------------------------+
  633. -- to_timestamp
  634. SELECT to_timestamp('2016-12-31 00:12:00');
  635. +---------------------------------+
  636. |to_timestamp(2016-12-31 00:12:00)|
  637. +---------------------------------+
  638. | 2016-12-31 00:12:00|
  639. +---------------------------------+
  640. SELECT to_timestamp('2016-12-31', 'yyyy-MM-dd');
  641. +------------------------------------+
  642. |to_timestamp(2016-12-31, yyyy-MM-dd)|
  643. +------------------------------------+
  644. | 2016-12-31 00:00:00|
  645. +------------------------------------+
  646. -- to_timestamp_ltz
  647. SELECT to_timestamp_ltz('2016-12-31 00:12:00');
  648. +-------------------------------------+
  649. |to_timestamp_ltz(2016-12-31 00:12:00)|
  650. +-------------------------------------+
  651. | 2016-12-31 00:12:00|
  652. +-------------------------------------+
  653. SELECT to_timestamp_ltz('2016-12-31', 'yyyy-MM-dd');
  654. +----------------------------------------+
  655. |to_timestamp_ltz(2016-12-31, yyyy-MM-dd)|
  656. +----------------------------------------+
  657. | 2016-12-31 00:00:00|
  658. +----------------------------------------+
  659. -- to_timestamp_ntz
  660. SELECT to_timestamp_ntz('2016-12-31 00:12:00');
  661. +-------------------------------------+
  662. |to_timestamp_ntz(2016-12-31 00:12:00)|
  663. +-------------------------------------+
  664. | 2016-12-31 00:12:00|
  665. +-------------------------------------+
  666. SELECT to_timestamp_ntz('2016-12-31', 'yyyy-MM-dd');
  667. +----------------------------------------+
  668. |to_timestamp_ntz(2016-12-31, yyyy-MM-dd)|
  669. +----------------------------------------+
  670. | 2016-12-31 00:00:00|
  671. +----------------------------------------+
  672. -- to_unix_timestamp
  673. SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');
  674. +-----------------------------------------+
  675. |to_unix_timestamp(2016-04-08, yyyy-MM-dd)|
  676. +-----------------------------------------+
  677. | 1460073600|
  678. +-----------------------------------------+
  679. -- to_utc_timestamp
  680. SELECT to_utc_timestamp('2016-08-31', 'Asia/Seoul');
  681. +----------------------------------------+
  682. |to_utc_timestamp(2016-08-31, Asia/Seoul)|
  683. +----------------------------------------+
  684. | 2016-08-30 15:00:00|
  685. +----------------------------------------+
  686. -- trunc
  687. SELECT trunc('2019-08-04', 'week');
  688. +-----------------------+
  689. |trunc(2019-08-04, week)|
  690. +-----------------------+
  691. | 2019-07-29|
  692. +-----------------------+
  693. SELECT trunc('2019-08-04', 'quarter');
  694. +--------------------------+
  695. |trunc(2019-08-04, quarter)|
  696. +--------------------------+
  697. | 2019-07-01|
  698. +--------------------------+
  699. SELECT trunc('2009-02-12', 'MM');
  700. +---------------------+
  701. |trunc(2009-02-12, MM)|
  702. +---------------------+
  703. | 2009-02-01|
  704. +---------------------+
  705. SELECT trunc('2015-10-27', 'YEAR');
  706. +-----------------------+
  707. |trunc(2015-10-27, YEAR)|
  708. +-----------------------+
  709. | 2015-01-01|
  710. +-----------------------+
  711. -- try_to_timestamp
  712. SELECT try_to_timestamp('2016-12-31 00:12:00');
  713. +-------------------------------------+
  714. |try_to_timestamp(2016-12-31 00:12:00)|
  715. +-------------------------------------+
  716. | 2016-12-31 00:12:00|
  717. +-------------------------------------+
  718. SELECT try_to_timestamp('2016-12-31', 'yyyy-MM-dd');
  719. +----------------------------------------+
  720. |try_to_timestamp(2016-12-31, yyyy-MM-dd)|
  721. +----------------------------------------+
  722. | 2016-12-31 00:00:00|
  723. +----------------------------------------+
  724. SELECT try_to_timestamp('foo', 'yyyy-MM-dd');
  725. +---------------------------------+
  726. |try_to_timestamp(foo, yyyy-MM-dd)|
  727. +---------------------------------+
  728. | null|
  729. +---------------------------------+
  730. -- unix_date
  731. SELECT unix_date(DATE("1970-01-02"));
  732. +---------------------+
  733. |unix_date(1970-01-02)|
  734. +---------------------+
  735. | 1|
  736. +---------------------+
  737. -- unix_micros
  738. SELECT unix_micros(TIMESTAMP('1970-01-01 00:00:01Z'));
  739. +---------------------------------+
  740. |unix_micros(1970-01-01 00:00:01Z)|
  741. +---------------------------------+
  742. | 1000000|
  743. +---------------------------------+
  744. -- unix_millis
  745. SELECT unix_millis(TIMESTAMP('1970-01-01 00:00:01Z'));
  746. +---------------------------------+
  747. |unix_millis(1970-01-01 00:00:01Z)|
  748. +---------------------------------+
  749. | 1000|
  750. +---------------------------------+
  751. -- unix_seconds
  752. SELECT unix_seconds(TIMESTAMP('1970-01-01 00:00:01Z'));
  753. +----------------------------------+
  754. |unix_seconds(1970-01-01 00:00:01Z)|
  755. +----------------------------------+
  756. | 1|
  757. +----------------------------------+
  758. -- unix_timestamp
  759. SELECT unix_timestamp();
  760. +--------------------------------------------------------+
  761. |unix_timestamp(current_timestamp(), yyyy-MM-dd HH:mm:ss)|
  762. +--------------------------------------------------------+
  763. | 1681497755|
  764. +--------------------------------------------------------+
  765. SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');
  766. +--------------------------------------+
  767. |unix_timestamp(2016-04-08, yyyy-MM-dd)|
  768. +--------------------------------------+
  769. | 1460073600|
  770. +--------------------------------------+
  771. -- weekday
  772. SELECT weekday('2009-07-30');
  773. +-------------------+
  774. |weekday(2009-07-30)|
  775. +-------------------+
  776. | 3|
  777. +-------------------+
  778. -- weekofyear
  779. SELECT weekofyear('2008-02-20');
  780. +----------------------+
  781. |weekofyear(2008-02-20)|
  782. +----------------------+
  783. | 8|
  784. +----------------------+
  785. -- window
  786. 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;
  787. +---+-------------------+-------------------+---+
  788. | a| start| end|cnt|
  789. +---+-------------------+-------------------+---+
  790. | A1|2021-01-01 00:00:00|2021-01-01 00:05:00| 2|
  791. | A1|2021-01-01 00:05:00|2021-01-01 00:10:00| 1|
  792. | A2|2021-01-01 00:00:00|2021-01-01 00:05:00| 1|
  793. +---+-------------------+-------------------+---+
  794. 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;
  795. +---+-------------------+-------------------+---+
  796. | a| start| end|cnt|
  797. +---+-------------------+-------------------+---+
  798. | A1|2020-12-31 23:55:00|2021-01-01 00:05:00| 2|
  799. | A1|2021-01-01 00:00:00|2021-01-01 00:10:00| 3|
  800. | A1|2021-01-01 00:05:00|2021-01-01 00:15:00| 1|
  801. | A2|2020-12-31 23:55:00|2021-01-01 00:05:00| 1|
  802. | A2|2021-01-01 00:00:00|2021-01-01 00:10:00| 1|
  803. +---+-------------------+-------------------+---+
  804. -- window_time
  805. 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);
  806. +---+-------------------+-------------------+--------------------+---+
  807. | a| start| end| window_time(window)|cnt|
  808. +---+-------------------+-------------------+--------------------+---+
  809. | A1|2021-01-01 00:00:00|2021-01-01 00:05:00|2021-01-01 00:04:...| 2|
  810. | A1|2021-01-01 00:05:00|2021-01-01 00:10:00|2021-01-01 00:09:...| 1|
  811. | A2|2021-01-01 00:00:00|2021-01-01 00:05:00|2021-01-01 00:04:...| 1|
  812. +---+-------------------+-------------------+--------------------+---+
  813. -- year
  814. SELECT year('2016-07-30');
  815. +----------------+
  816. |year(2016-07-30)|
  817. +----------------+
  818. | 2016|
  819. +----------------+

JSON Functions

FunctionDescription
from_json(jsonStr, schema[, options])Returns a struct value with the given jsonStr and schema.
get_json_object(json_txt, path)Extracts a json object from path.
json_array_length(jsonArray)Returns the number of elements in the outermost JSON array.
json_object_keys(json_object)Returns all the keys of the outermost JSON object as an array.
json_tuple(jsonStr, p1, p2, …, pn)Returns a tuple like the function get_json_object, but it takes multiple names. All the input parameters and output column types are string.
schema_of_json(json[, options])Returns schema in the DDL format of JSON string.
to_json(expr[, options])Returns a JSON string with a given struct value

Examples

  1. -- from_json
  2. SELECT from_json('{"a":1, "b":0.8}', 'a INT, b DOUBLE');
  3. +---------------------------+
  4. |from_json({"a":1, "b":0.8})|
  5. +---------------------------+
  6. | {1, 0.8}|
  7. +---------------------------+
  8. SELECT from_json('{"time":"26/08/2015"}', 'time Timestamp', map('timestampFormat', 'dd/MM/yyyy'));
  9. +--------------------------------+
  10. |from_json({"time":"26/08/2015"})|
  11. +--------------------------------+
  12. | {2015-08-26 00:00...|
  13. +--------------------------------+
  14. SELECT from_json('{"teacher": "Alice", "student": [{"name": "Bob", "rank": 1}, {"name": "Charlie", "rank": 2}]}', 'STRUCT<teacher: STRING, student: ARRAY<STRUCT<name: STRING, rank: INT>>>');
  15. +--------------------------------------------------------------------------------------------------------+
  16. |from_json({"teacher": "Alice", "student": [{"name": "Bob", "rank": 1}, {"name": "Charlie", "rank": 2}]})|
  17. +--------------------------------------------------------------------------------------------------------+
  18. | {Alice, [{Bob, 1}...|
  19. +--------------------------------------------------------------------------------------------------------+
  20. -- get_json_object
  21. SELECT get_json_object('{"a":"b"}', '$.a');
  22. +-------------------------------+
  23. |get_json_object({"a":"b"}, $.a)|
  24. +-------------------------------+
  25. | b|
  26. +-------------------------------+
  27. -- json_array_length
  28. SELECT json_array_length('[1,2,3,4]');
  29. +----------------------------+
  30. |json_array_length([1,2,3,4])|
  31. +----------------------------+
  32. | 4|
  33. +----------------------------+
  34. SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  35. +------------------------------------------------+
  36. |json_array_length([1,2,3,{"f1":1,"f2":[5,6]},4])|
  37. +------------------------------------------------+
  38. | 5|
  39. +------------------------------------------------+
  40. SELECT json_array_length('[1,2');
  41. +-----------------------+
  42. |json_array_length([1,2)|
  43. +-----------------------+
  44. | null|
  45. +-----------------------+
  46. -- json_object_keys
  47. SELECT json_object_keys('{}');
  48. +--------------------+
  49. |json_object_keys({})|
  50. +--------------------+
  51. | []|
  52. +--------------------+
  53. SELECT json_object_keys('{"key": "value"}');
  54. +----------------------------------+
  55. |json_object_keys({"key": "value"})|
  56. +----------------------------------+
  57. | [key]|
  58. +----------------------------------+
  59. SELECT json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}');
  60. +--------------------------------------------------------+
  61. |json_object_keys({"f1":"abc","f2":{"f3":"a", "f4":"b"}})|
  62. +--------------------------------------------------------+
  63. | [f1, f2]|
  64. +--------------------------------------------------------+
  65. -- json_tuple
  66. SELECT json_tuple('{"a":1, "b":2}', 'a', 'b');
  67. +---+---+
  68. | c0| c1|
  69. +---+---+
  70. | 1| 2|
  71. +---+---+
  72. -- schema_of_json
  73. SELECT schema_of_json('[{"col":0}]');
  74. +---------------------------+
  75. |schema_of_json([{"col":0}])|
  76. +---------------------------+
  77. | ARRAY<STRUCT<col:...|
  78. +---------------------------+
  79. SELECT schema_of_json('[{"col":01}]', map('allowNumericLeadingZeros', 'true'));
  80. +----------------------------+
  81. |schema_of_json([{"col":01}])|
  82. +----------------------------+
  83. | ARRAY<STRUCT<col:...|
  84. +----------------------------+
  85. -- to_json
  86. SELECT to_json(named_struct('a', 1, 'b', 2));
  87. +---------------------------------+
  88. |to_json(named_struct(a, 1, b, 2))|
  89. +---------------------------------+
  90. | {"a":1,"b":2}|
  91. +---------------------------------+
  92. SELECT to_json(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy'));
  93. +-----------------------------------------------------------------+
  94. |to_json(named_struct(time, to_timestamp(2015-08-26, yyyy-MM-dd)))|
  95. +-----------------------------------------------------------------+
  96. | {"time":"26/08/20...|
  97. +-----------------------------------------------------------------+
  98. SELECT to_json(array(named_struct('a', 1, 'b', 2)));
  99. +----------------------------------------+
  100. |to_json(array(named_struct(a, 1, b, 2)))|
  101. +----------------------------------------+
  102. | [{"a":1,"b":2}]|
  103. +----------------------------------------+
  104. SELECT to_json(map('a', named_struct('b', 1)));
  105. +-----------------------------------+
  106. |to_json(map(a, named_struct(b, 1)))|
  107. +-----------------------------------+
  108. | {"a":{"b":1}}|
  109. +-----------------------------------+
  110. SELECT to_json(map(named_struct('a', 1),named_struct('b', 2)));
  111. +----------------------------------------------------+
  112. |to_json(map(named_struct(a, 1), named_struct(b, 2)))|
  113. +----------------------------------------------------+
  114. | {"[1]":{"b":2}}|
  115. +----------------------------------------------------+
  116. SELECT to_json(map('a', 1));
  117. +------------------+
  118. |to_json(map(a, 1))|
  119. +------------------+
  120. | {"a":1}|
  121. +------------------+
  122. SELECT to_json(array((map('a', 1))));
  123. +-------------------------+
  124. |to_json(array(map(a, 1)))|
  125. +-------------------------+
  126. | [{"a":1}]|
  127. +-------------------------+

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

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|
+---+

Conversion Functions

FunctionDescription
bigint(expr)Casts the value expr to the target data type bigint.
binary(expr)Casts the value expr to the target data type binary.
boolean(expr)Casts the value expr to the target data type boolean.
cast(expr AS type)Casts the value expr to the target data type type.
date(expr)Casts the value expr to the target data type date.
decimal(expr)Casts the value expr to the target data type decimal.
double(expr)Casts the value expr to the target data type double.
float(expr)Casts the value expr to the target data type float.
int(expr)Casts the value expr to the target data type int.
smallint(expr)Casts the value expr to the target data type smallint.
string(expr)Casts the value expr to the target data type string.
timestamp(expr)Casts the value expr to the target data type timestamp.
tinyint(expr)Casts the value expr to the target data type tinyint.

Examples

-- cast
SELECT cast('10' as int);
+---------------+
|CAST(10 AS INT)|
+---------------+
|             10|
+---------------+

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|
+----+----+