esql-functions.asciidoc 4.3 KB

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