index.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions]]
  4. == Functions and Operators
  5. {es-sql} provides a number of built-in operators and functions.
  6. === Comparison Operators
  7. {es-sql} supports the following comparison operators:
  8. * Equality (`=`)
  9. ["source","sql",subs="attributes,callouts,macros"]
  10. --------------------------------------------------
  11. include-tagged::{sql-specs}/filter.sql-spec[whereFieldEquality]
  12. --------------------------------------------------
  13. * Inequality (`<>` or `!=` or `<=>`)
  14. ["source","sql",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{sql-specs}/filter.sql-spec[whereFieldNonEquality]
  17. --------------------------------------------------
  18. * Comparison (`<`, `<=`, `>`, `>=`)
  19. ["source","sql",subs="attributes,callouts,macros"]
  20. --------------------------------------------------
  21. include-tagged::{sql-specs}/filter.sql-spec[whereFieldLessThan]
  22. --------------------------------------------------
  23. * `BETWEEN`
  24. ["source","sql",subs="attributes,callouts,macros"]
  25. --------------------------------------------------
  26. include-tagged::{sql-specs}/filter.sql-spec[whereBetween]
  27. --------------------------------------------------
  28. * `IS NULL`/`IS NOT NULL`
  29. ["source","sql",subs="attributes,callouts,macros"]
  30. --------------------------------------------------
  31. include-tagged::{sql-specs}/filter.sql-spec[whereIsNotNullAndIsNull]
  32. --------------------------------------------------
  33. === Logical Operators
  34. {es-sql} supports the following logical operators:
  35. * `AND`
  36. ["source","sql",subs="attributes,callouts,macros"]
  37. --------------------------------------------------
  38. include-tagged::{sql-specs}/filter.sql-spec[whereFieldAndComparison]
  39. --------------------------------------------------
  40. * `OR`
  41. ["source","sql",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{sql-specs}/filter.sql-spec[whereFieldOrComparison]
  44. --------------------------------------------------
  45. * `NOT`
  46. ["source","sql",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{sql-specs}/filter.sql-spec[whereFieldEqualityNot]
  49. --------------------------------------------------
  50. === Math Operators
  51. {es-sql} supports the following math operators:
  52. * Add (`+`)
  53. ["source","sql",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{sql-specs}/arithmetic.sql-spec[plus]
  56. --------------------------------------------------
  57. * Subtract (infix `-`)
  58. ["source","sql",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{sql-specs}/arithmetic.sql-spec[minus]
  61. --------------------------------------------------
  62. * Negate (unary `-`)
  63. ["source","sql",subs="attributes,callouts,macros"]
  64. --------------------------------------------------
  65. include-tagged::{sql-specs}/arithmetic.sql-spec[unaryMinus]
  66. --------------------------------------------------
  67. * Multiply (`*`)
  68. ["source","sql",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{sql-specs}/arithmetic.sql-spec[multiply]
  71. --------------------------------------------------
  72. * Divide (`/`)
  73. ["source","sql",subs="attributes,callouts,macros"]
  74. --------------------------------------------------
  75. include-tagged::{sql-specs}/arithmetic.sql-spec[divide]
  76. --------------------------------------------------
  77. * https://en.wikipedia.org/wiki/Modulo_operation[Modulo] or Reminder(`%`)
  78. ["source","sql",subs="attributes,callouts,macros"]
  79. --------------------------------------------------
  80. include-tagged::{sql-specs}/arithmetic.sql-spec[mod]
  81. --------------------------------------------------
  82. === Math Functions
  83. All math and trigonometric functions require their input (where applicable)
  84. to be numeric.
  85. ==== Generic
  86. * `ABS`
  87. https://en.wikipedia.org/wiki/Absolute_value[Absolute value], returns \[same type as input]
  88. ["source","sql",subs="attributes,callouts,macros"]
  89. --------------------------------------------------
  90. include-tagged::{sql-specs}/math.sql-spec[abs]
  91. --------------------------------------------------
  92. * `CBRT`
  93. https://en.wikipedia.org/wiki/Cube_root[Cube root], returns `double`
  94. // TODO make the example in the tests presentable
  95. * `CEIL`
  96. https://en.wikipedia.org/wiki/Floor_and_ceiling_functions[Ceiling], returns `double`
  97. * `CEILING`
  98. Same as `CEIL`
  99. // TODO make the example in the tests presentable
  100. * `E`
  101. https://en.wikipedia.org/wiki/E_%28mathematical_constant%29[Euler's number], returns `2.7182818284590452354`
  102. * https://en.wikipedia.org/wiki/Rounding#Round_half_up[Round] (`ROUND`)
  103. // TODO make the example in the tests presentable
  104. NOTE: This rounds "half up" meaning that `ROUND(-1.5)` results in `-1`.
  105. * https://en.wikipedia.org/wiki/Floor_and_ceiling_functions[Floor] (`FLOOR`)
  106. // TODO make the example in the tests presentable
  107. * https://en.wikipedia.org/wiki/Natural_logarithm[Natural logarithm] (`LOG`)
  108. ["source","sql",subs="attributes,callouts,macros"]
  109. --------------------------------------------------
  110. include-tagged::{sql-specs}/math.sql-spec[log]
  111. --------------------------------------------------
  112. * https://en.wikipedia.org/wiki/Logarithm[Logarithm] base 10 (`LOG10`)
  113. ["source","sql",subs="attributes,callouts,macros"]
  114. --------------------------------------------------
  115. include-tagged::{sql-specs}/math.sql-spec[log10]
  116. --------------------------------------------------
  117. * https://en.wikipedia.org/wiki/Square_root[Square root] (`SQRT`)
  118. ["source","sql",subs="attributes,callouts,macros"]
  119. --------------------------------------------------
  120. include-tagged::{sql-specs}/math.sql-spec[sqrt]
  121. --------------------------------------------------
  122. * https://en.wikipedia.org/wiki/Exponential_function[e^x^] (`EXP`)
  123. ["source","sql",subs="attributes,callouts,macros"]
  124. --------------------------------------------------
  125. include-tagged::{sql-specs}/math.sql-spec[exp]
  126. --------------------------------------------------
  127. * https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#expm1-double-[e^x^ - 1] (`EXPM1`)
  128. ["source","sql",subs="attributes,callouts,macros"]
  129. --------------------------------------------------
  130. include-tagged::{sql-specs}/math.sql-spec[expm1]
  131. --------------------------------------------------
  132. ==== Trigonometric
  133. * Convert from https://en.wikipedia.org/wiki/Radian[radians]
  134. to https://en.wikipedia.org/wiki/Degree_(angle)[degrees] (`DEGREES`)
  135. ["source","sql",subs="attributes,callouts,macros"]
  136. --------------------------------------------------
  137. include-tagged::{sql-specs}/math.sql-spec[degrees]
  138. --------------------------------------------------
  139. * Convert from https://en.wikipedia.org/wiki/Degree_(angle)[degrees]
  140. to https://en.wikipedia.org/wiki/Radian[radians] (`RADIANS`)
  141. ["source","sql",subs="attributes,callouts,macros"]
  142. --------------------------------------------------
  143. include-tagged::{sql-specs}/math.sql-spec[degrees]
  144. --------------------------------------------------
  145. * https://en.wikipedia.org/wiki/Trigonometric_functions#sine[Sine] (`SIN`)
  146. ["source","sql",subs="attributes,callouts,macros"]
  147. --------------------------------------------------
  148. include-tagged::{sql-specs}/math.sql-spec[sin]
  149. --------------------------------------------------
  150. * https://en.wikipedia.org/wiki/Trigonometric_functions#cosine[Cosine] (`COS`)
  151. ["source","sql",subs="attributes,callouts,macros"]
  152. --------------------------------------------------
  153. include-tagged::{sql-specs}/math.sql-spec[cos]
  154. --------------------------------------------------
  155. * https://en.wikipedia.org/wiki/Trigonometric_functions#tangent[Tangent] (`TAN`)
  156. ["source","sql",subs="attributes,callouts,macros"]
  157. --------------------------------------------------
  158. include-tagged::{sql-specs}/math.sql-spec[tan]
  159. --------------------------------------------------
  160. * https://en.wikipedia.org/wiki/Inverse_trigonometric_functions[Arc sine] (`ASIN`)
  161. ["source","sql",subs="attributes,callouts,macros"]
  162. --------------------------------------------------
  163. include-tagged::{sql-specs}/math.sql-spec[asin]
  164. --------------------------------------------------
  165. * https://en.wikipedia.org/wiki/Inverse_trigonometric_functions[Arc cosine] (`ACOS`)
  166. ["source","sql",subs="attributes,callouts,macros"]
  167. --------------------------------------------------
  168. include-tagged::{sql-specs}/math.sql-spec[acos]
  169. --------------------------------------------------
  170. * https://en.wikipedia.org/wiki/Inverse_trigonometric_functions[Arc tangent] (`ATAN`)
  171. ["source","sql",subs="attributes,callouts,macros"]
  172. --------------------------------------------------
  173. include-tagged::{sql-specs}/math.sql-spec[atan]
  174. --------------------------------------------------
  175. * https://en.wikipedia.org/wiki/Hyperbolic_function[Hyperbolic sine] (`SINH`)
  176. ["source","sql",subs="attributes,callouts,macros"]
  177. --------------------------------------------------
  178. include-tagged::{sql-specs}/math.sql-spec[sinh]
  179. --------------------------------------------------
  180. * https://en.wikipedia.org/wiki/Hyperbolic_function[Hyperbolic cosine] (`COSH`)
  181. ["source","sql",subs="attributes,callouts,macros"]
  182. --------------------------------------------------
  183. include-tagged::{sql-specs}/math.sql-spec[cosh]
  184. --------------------------------------------------
  185. [[sql-functions-datetime]]
  186. === Date and Time Functions
  187. * Extract the year from a date (`YEAR`)
  188. ["source","sql",subs="attributes,callouts,macros"]
  189. --------------------------------------------------
  190. include-tagged::{sql-specs}/datetime.csv-spec[year]
  191. --------------------------------------------------
  192. * Extract the month of the year from a date (`MONTH_OF_YEAR` or `MONTH`)
  193. ["source","sql",subs="attributes,callouts,macros"]
  194. --------------------------------------------------
  195. include-tagged::{sql-specs}/datetime.csv-spec[monthOfYear]
  196. --------------------------------------------------
  197. * Extract the week of the year from a date (`WEEK_OF_YEAR` or `WEEK`)
  198. ["source","sql",subs="attributes,callouts,macros"]
  199. --------------------------------------------------
  200. include-tagged::{sql-specs}/datetime.csv-spec[weekOfYear]
  201. --------------------------------------------------
  202. * Extract the day of the year from a date (`DAY_OF_YEAR` or `DOY`)
  203. ["source","sql",subs="attributes,callouts,macros"]
  204. --------------------------------------------------
  205. include-tagged::{sql-specs}/datetime.csv-spec[dayOfYear]
  206. --------------------------------------------------
  207. * Extract the day of the month from a date (`DAY_OF_MONTH`, `DOM`, or `DAY`)
  208. ["source","sql",subs="attributes,callouts,macros"]
  209. --------------------------------------------------
  210. include-tagged::{sql-specs}/datetime.csv-spec[dayOfMonth]
  211. --------------------------------------------------
  212. * Extract the day of the week from a date (`DAY_OF_WEEK` or `DOW`).
  213. Monday is `1`, Tuesday is `2`, etc.
  214. ["source","sql",subs="attributes,callouts,macros"]
  215. --------------------------------------------------
  216. include-tagged::{sql-specs}/datetime.csv-spec[dayOfWeek]
  217. --------------------------------------------------
  218. * Extract the hour of the day from a date (`HOUR_OF_DAY` or `HOUR`).
  219. Monday is `1`, Tuesday is `2`, etc.
  220. ["source","sql",subs="attributes,callouts,macros"]
  221. --------------------------------------------------
  222. include-tagged::{sql-specs}/datetime.csv-spec[hourOfDay]
  223. --------------------------------------------------
  224. * Extract the minute of the day from a date (`MINUTE_OF_DAY`).
  225. ["source","sql",subs="attributes,callouts,macros"]
  226. --------------------------------------------------
  227. include-tagged::{sql-specs}/datetime.csv-spec[minuteOfDay]
  228. --------------------------------------------------
  229. * Extract the minute of the hour from a date (`MINUTE_OF_HOUR`, `MINUTE`).
  230. ["source","sql",subs="attributes,callouts,macros"]
  231. --------------------------------------------------
  232. include-tagged::{sql-specs}/datetime.csv-spec[minuteOfHour]
  233. --------------------------------------------------
  234. * Extract the second of the minute from a date (`SECOND_OF_MINUTE`, `SECOND`).
  235. ["source","sql",subs="attributes,callouts,macros"]
  236. --------------------------------------------------
  237. include-tagged::{sql-specs}/datetime.csv-spec[secondOfMinute]
  238. --------------------------------------------------
  239. * Extract
  240. As an alternative, one can support `EXTRACT` to extract fields from datetimes.
  241. You can run any <<sql-functions-datetime,datetime function>>
  242. with `EXTRACT(<datetime_function> FROM <expression>)`. So
  243. ["source","sql",subs="attributes,callouts,macros"]
  244. --------------------------------------------------
  245. include-tagged::{sql-specs}/datetime.csv-spec[extractDayOfYear]
  246. --------------------------------------------------
  247. is the equivalent to
  248. ["source","sql",subs="attributes,callouts,macros"]
  249. --------------------------------------------------
  250. include-tagged::{sql-specs}/datetime.csv-spec[dayOfYear]
  251. --------------------------------------------------
  252. [[sql-functions-aggregate]]
  253. === Aggregate Functions
  254. ==== Basic
  255. * https://en.wikipedia.org/wiki/Arithmetic_mean[Average] (`AVG`)
  256. ["source","sql",subs="attributes,callouts,macros"]
  257. --------------------------------------------------
  258. include-tagged::{sql-specs}/agg.sql-spec[avg]
  259. --------------------------------------------------
  260. * Count the number of matching fields (`COUNT`)
  261. ["source","sql",subs="attributes,callouts,macros"]
  262. --------------------------------------------------
  263. include-tagged::{sql-specs}/agg.sql-spec[countStar]
  264. --------------------------------------------------
  265. * Count the number of distinct values in matching documents (`COUNT(DISTINCT`)
  266. ["source","sql",subs="attributes,callouts,macros"]
  267. --------------------------------------------------
  268. include-tagged::{sql-specs}/agg.sql-spec[countDistinct]
  269. --------------------------------------------------
  270. * Find the maximum value in matching documents (`MAX`)
  271. ["source","sql",subs="attributes,callouts,macros"]
  272. --------------------------------------------------
  273. include-tagged::{sql-specs}/agg.sql-spec[max]
  274. --------------------------------------------------
  275. * Find the minimum value in matching documents (`MIN`)
  276. ["source","sql",subs="attributes,callouts,macros"]
  277. --------------------------------------------------
  278. include-tagged::{sql-specs}/agg.sql-spec[min]
  279. --------------------------------------------------
  280. * https://en.wikipedia.org/wiki/Kahan_summation_algorithm[Sum]
  281. all values of matching documents (`SUM`).
  282. ["source","sql",subs="attributes,callouts,macros"]
  283. --------------------------------------------------
  284. include-tagged::{sql-specs}/agg.csv-spec[sum]
  285. --------------------------------------------------