esql-processing-commands.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. [[esql-processing-commands]]
  2. == ESQL processing commands
  3. Processing commands change an input table by adding, removing, or changing rows
  4. and columns.
  5. image::images/esql/processing-command.svg[A processing command changes an input table,align="center"]
  6. ESQL supports these processing commands:
  7. * <<esql-dissect>>
  8. * <<esql-drop>>
  9. * <<esql-eval>>
  10. * <<esql-grok>>
  11. * <<esql-limit>>
  12. * <<esql-project>>
  13. * <<esql-rename>>
  14. * <<esql-sort>>
  15. * <<esql-stats-by>>
  16. * <<esql-where>>
  17. [[esql-dissect]]
  18. === `DISSECT`
  19. TODO
  20. [[esql-drop]]
  21. === `DROP`
  22. TODO
  23. [[esql-eval]]
  24. === `EVAL`
  25. `EVAL` enables you to add new columns to the end of the table:
  26. [source,esql]
  27. ----
  28. FROM employees
  29. | PROJECT first_name, last_name, height
  30. | EVAL height_feet = height * 3.281, height_cm = height * 100
  31. ----
  32. If the specified column already exists, the existing column will be dropped, and
  33. the new column will be appended to the table:
  34. [source,esql]
  35. ----
  36. FROM employees
  37. | PROJECT first_name, last_name, height
  38. | EVAL height = height * 3.281
  39. ----
  40. [discrete]
  41. ==== Functions
  42. `EVAL` supports various functions for calculating values. Refer to
  43. <<esql-functions,Functions>> for more information.
  44. [[esql-grok]]
  45. === `GROK`
  46. TODO
  47. [[esql-limit]]
  48. === `LIMIT`
  49. The `LIMIT` processing command enables you to limit the number of rows:
  50. [source,esql]
  51. ----
  52. FROM employees
  53. | LIMIT 5
  54. ----
  55. [[esql-project]]
  56. === `PROJECT`
  57. The `PROJECT` command enables you to change:
  58. * the columns that are returned,
  59. * the order in which they are returned,
  60. * and the name with which they are returned.
  61. To limit the columns that are returned, use a comma-separated list of column
  62. names. The columns are returned in the specified order:
  63. [source,esql]
  64. ----
  65. FROM employees
  66. | PROJECT first_name, last_name, height
  67. ----
  68. Rather than specify each column by name, you can use wildcards to return all
  69. columns with a name that matches a pattern:
  70. [source,esql]
  71. ----
  72. FROM employees
  73. | PROJECT h*
  74. ----
  75. The asterisk wildcard (`*`) by itself translates to all columns that do not
  76. match the other arguments. This query will first return all columns with a name
  77. that starts with an h, followed by all other columns:
  78. [source,esql]
  79. ----
  80. FROM employees
  81. | PROJECT h*, *
  82. ----
  83. Use a dash to specify columns you do not want returned:
  84. [source,esql]
  85. ----
  86. FROM employees
  87. | PROJECT -h*
  88. ----
  89. Use `=` to rename columns:
  90. [source,esql]
  91. ----
  92. FROM employees
  93. | PROJECT current_employee = still_hired, *
  94. ----
  95. [[esql-rename]]
  96. === `RENAME`
  97. TODO
  98. [[esql-sort]]
  99. === `SORT`
  100. Use the `SORT` command to sort rows on one or more fields:
  101. [source,esql]
  102. ----
  103. FROM employees
  104. | PROJECT first_name, last_name, height
  105. | SORT height
  106. ----
  107. The default sort order is ascending. Set an explicit sort order using `ASC` or
  108. `DESC`:
  109. [source,esql]
  110. ----
  111. FROM employees
  112. | PROJECT first_name, last_name, height
  113. | SORT height desc
  114. ----
  115. If two rows have the same sort key, the original order will be preserved. You
  116. can provide additional sort expressions to act as tie breakers:
  117. [source,esql]
  118. ----
  119. FROM employees
  120. | PROJECT first_name, last_name, height
  121. | SORT height desc, first_name ASC
  122. ----
  123. [discrete]
  124. ==== `null` values
  125. By default, `null` values are treated as being larger than any other value. With
  126. an ascending sort order, `null` values are sorted last, and with a descending
  127. sort order, `null` values are sorted first. You can change that by providing
  128. `NULLS FIRST` or `NULLS LAST`:
  129. [source,esql]
  130. ----
  131. FROM employees
  132. | PROJECT first_name, last_name, height
  133. | SORT first_name ASC NULLS FIRST
  134. ----
  135. [[esql-stats-by]]
  136. === `STATS ... BY`
  137. Use `STATS ... BY` to group rows according to a common value and calculate one
  138. or more aggregated values over the grouped rows.
  139. [source,esql]
  140. ----
  141. FROM employees
  142. | STATS count = COUNT(languages) BY languages
  143. ----
  144. If `BY` is omitted, the output table contains exactly one row with the
  145. aggregations applied over the entire dataset:
  146. [source,esql]
  147. ----
  148. FROM employees
  149. | STATS avg_lang = AVG(languages)
  150. ----
  151. It's possible to calculate multiple values:
  152. [source,esql]
  153. ----
  154. FROM employees
  155. | STATS avg_lang = AVG(languages), max_lang = MAX(languages)
  156. ----
  157. It's also possible to group by multiple values (only supported for long and
  158. keyword family fields):
  159. [source,esql]
  160. ----
  161. FROM employees
  162. | EVAL hired = DATE_FORMAT(hire_date, "YYYY")
  163. | STATS avg_salary = AVG(salary) BY hired, languages.long
  164. | EVAL avg_salary = ROUND(avg_salary)
  165. | SORT hired, languages.long
  166. ----
  167. The following aggregation functions are supported:
  168. * `AVG`
  169. * `COUNT`
  170. * `MAX`
  171. * `MEDIAN`
  172. * `MEDIAN_ABSOLUTE_DEVIATION`
  173. * `MIN`
  174. * `SUM`
  175. [[esql-where]]
  176. === `WHERE`
  177. Use `WHERE` to produce a table that contains all the rows from the input table
  178. for which the provided condition evaluates to `true`:
  179. [source,esql]
  180. ----
  181. FROM employees
  182. | PROJECT first_name, last_name, still_hired
  183. | WHERE still_hired == true
  184. ----
  185. Which, because `still_hired` is a boolean field, can be simplified to:
  186. [source,esql]
  187. ----
  188. FROM employees
  189. | PROJECT first_name, last_name, still_hired
  190. | WHERE still_hired
  191. ----
  192. [discrete]
  193. ==== Operators
  194. These comparison operators are supported:
  195. * equality: `==`
  196. * inequality: `!=`
  197. * comparison:
  198. ** less than: `<`
  199. ** less than or equal: `<=`
  200. ** larger than: `>`
  201. ** larger than or equal: `>=`
  202. You can use the following boolean operators:
  203. * `AND`
  204. * `OR`
  205. * `NOT`
  206. [source,esql]
  207. ----
  208. FROM employees
  209. | PROJECT first_name, last_name, height, still_hired
  210. | WHERE height > 2 AND NOT still_hired
  211. ----
  212. [discrete]
  213. ==== Functions
  214. `WHERE` supports various functions for calculating values. Refer to
  215. <<esql-functions,Functions>> for more information.
  216. [source,esql]
  217. ----
  218. FROM employees
  219. | PROJECT first_name, last_name, height
  220. | WHERE length(first_name) < 4
  221. ----