esql-functions.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. [[esql-functions]]
  2. == Functions
  3. <<esql-row,`ROW`>>, <<esql-eval,`EVAL`>> and <<esql-where,`WHERE`>> support
  4. these functions:
  5. * <<esql-abs>>
  6. * <<esql-case>>
  7. * <<esql-cidr_match>>
  8. * <<esql-concat>>
  9. * <<esql-date_format>>
  10. * <<esql-date_trunc>>
  11. * <<esql-is_finite>>
  12. * <<esql-is_infinite>>
  13. * <<esql-is_nan>>
  14. * <<esql-is_null>>
  15. * <<esql-length>>
  16. * <<esql-mv_avg>>
  17. * <<esql-mv_max>>
  18. * <<esql-mv_min>>
  19. * <<esql-mv_sum>>
  20. * <<esql-pow>>
  21. * <<esql-round>>
  22. * <<esql-split>>
  23. * <<esql-starts_with>>
  24. * <<esql-substring>>
  25. [[esql-abs]]
  26. === `ABS`
  27. Returns the absolute value.
  28. [source,esql]
  29. ----
  30. FROM employees
  31. | PROJECT first_name, last_name, height
  32. | EVAL abs_height = ABS(0.0 - height)
  33. ----
  34. [[esql-case]]
  35. === `CASE`
  36. Accepts pairs of conditions and values. The function returns the value that
  37. belongs to the first condition that evaluates to `true`. If the number of
  38. arguments is odd, the last argument is the default value which is returned when
  39. no condition matches.
  40. [source,esql]
  41. ----
  42. FROM employees
  43. | EVAL type = CASE(
  44. languages <= 1, "monolingual",
  45. languages <= 2, "bilingual",
  46. "polyglot")
  47. | PROJECT first_name, last_name, type
  48. ----
  49. [[esql-cidr_match]]
  50. === `CIDR_MATCH`
  51. Returns `true` if the provided IP is contained in one of the provided CIDR
  52. blocks.
  53. `CIDR_MATCH` accepts two or more arguments. The first argument is the IP
  54. address of type `ip` (both IPv4 and IPv6 are supported). Subsequent arguments
  55. are the CIDR blocks to test the IP against.
  56. [source,esql]
  57. ----
  58. FROM hosts
  59. | WHERE CIDR_MATCH(ip, "127.0.0.2/32", "127.0.0.3/32")
  60. ----
  61. [[esql-concat]]
  62. === `CONCAT`
  63. Concatenates two or more strings.
  64. [source,esql]
  65. ----
  66. FROM employees
  67. | PROJECT first_name, last_name, height
  68. | EVAL fullname = CONCAT(first_name, " ", last_name)
  69. ----
  70. [[esql-date_format]]
  71. === `DATE_FORMAT`
  72. Returns a string representation of a date in the provided format. If no format
  73. is specified, the `yyyy-MM-dd'T'HH:mm:ss.SSSZ` format is used.
  74. [source,esql]
  75. ----
  76. FROM employees
  77. | PROJECT first_name, last_name, hire_date
  78. | EVAL hired = DATE_FORMAT(hire_date, "YYYY-MM-dd")
  79. ----
  80. [[esql-date_trunc]]
  81. === `DATE_TRUNC`
  82. Rounds down a date to the closest interval. Intervals can be expressed using the
  83. <<esql-timespan-literals,timespan literal syntax>>.
  84. [source,esql]
  85. ----
  86. FROM employees
  87. | EVAL year_hired = DATE_TRUNC(hire_date, 1 year)
  88. | STATS count(emp_no) BY year_hired
  89. | SORT year_hired
  90. ----
  91. [[esql-is_finite]]
  92. === `IS_FINITE`
  93. Returns a boolean that indicates whether its input is a finite number.
  94. [source,esql]
  95. ----
  96. ROW d = 1.0
  97. | EVAL s = IS_FINITE(d/0)
  98. ----
  99. [[esql-is_infinite]]
  100. === `IS_INFINITE`
  101. Returns a boolean that indicates whether its input is infinite.
  102. [source,esql]
  103. ----
  104. ROW d = 1.0
  105. | EVAL s = IS_INFINITE(d/0)
  106. ----
  107. [[esql-is_nan]]
  108. === `IS_NAN`
  109. Returns a boolean that indicates whether its input is not a number.
  110. [source,esql]
  111. ----
  112. ROW d = 1.0
  113. | EVAL s = IS_NAN(d)
  114. ----
  115. [[esql-is_null]]
  116. === `IS_NULL`
  117. Returns a boolean than indicates whether its input is `null`.
  118. [source,esql]
  119. ----
  120. FROM employees
  121. | WHERE IS_NULL(first_name)
  122. ----
  123. Combine this function with `NOT` to filter out any `null` data:
  124. [source,esql]
  125. ----
  126. FROM employees
  127. | WHERE NOT IS_NULL(first_name)
  128. ----
  129. [[esql-length]]
  130. === `LENGTH`
  131. Returns the character length of a string.
  132. [source,esql]
  133. ----
  134. FROM employees
  135. | PROJECT first_name, last_name, height
  136. | EVAL fn_length = LENGTH(first_name)
  137. ----
  138. include::functions/mv_avg.asciidoc[]
  139. include::functions/mv_max.asciidoc[]
  140. include::functions/mv_min.asciidoc[]
  141. include::functions/mv_sum.asciidoc[]
  142. [[esql-pow]]
  143. === `POW`
  144. Returns the the value of a base (first argument) raised to a power (second
  145. argument).
  146. [source,esql]
  147. ----
  148. ROW base = 2.0, exponent = 2.0
  149. | EVAL s = POW(base, exponent)
  150. ----
  151. [[esql-round]]
  152. === `ROUND`
  153. Rounds a number to the closest number with the specified number of digits.
  154. Defaults to 0 digits if no number of digits is provided. If the specified number
  155. of digits is negative, rounds to the number of digits left of the decimal point.
  156. [source,esql]
  157. ----
  158. FROM employees
  159. | PROJECT first_name, last_name, height
  160. | EVAL height = ROUND(height * 3.281, 1)
  161. ----
  162. include::functions/split.asciidoc[]
  163. [[esql-starts_with]]
  164. === `STARTS_WITH`
  165. Returns a boolean that indicates whether a keyword string starts with another
  166. string:
  167. [source,esql]
  168. ----
  169. FROM employees
  170. | PROJECT first_name, last_name, height
  171. | EVAL ln_S = STARTS_WITH(last_name, "S")
  172. ----
  173. [[esql-substring]]
  174. === `SUBSTRING`
  175. Returns a substring of a string, specified by a start position and an optional
  176. length. This example returns the first three characters of every last name:
  177. [source,esql]
  178. ----
  179. FROM employees
  180. | PROJECT last_name
  181. | EVAL ln_sub = SUBSTRING(last_name, 1, 3)
  182. ----
  183. A negative start position is interpreted as being relative to the end of the
  184. string. This example returns the last three characters of of every last name:
  185. [source,esql]
  186. ----
  187. FROM employees
  188. | PROJECT last_name
  189. | EVAL ln_sub = SUBSTRING(last_name, -3, 3)
  190. ----
  191. If length is omitted, substring returns the remainder of the string. This
  192. example returns all characters except for the first:
  193. [source,esql]
  194. ----
  195. FROM employees
  196. | PROJECT last_name
  197. | EVAL ln_sub = SUBSTRING(last_name, 2)
  198. ----