esql-functions.asciidoc 4.4 KB

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