Skip to content

Commit 2d90ddc

Browse files
[8.12] [DOCS] More ES|QL backtick examples (elastic#103995) (elastic#104049)
* Merge conflict * Fix borked merge conflict resolution
1 parent 851235a commit 2d90ddc

File tree

6 files changed

+273
-106
lines changed

6 files changed

+273
-106
lines changed

docs/reference/esql/esql-get-started.asciidoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,31 @@ calculate the median duration per client IP:
205205
include::{esql-specs}/stats.csv-spec[tag=gs-stats-by]
206206
----
207207

208+
[discrete]
209+
[[esql-getting-started-access-columns]]
210+
=== Access columns
211+
212+
You can access columns by their name. If a name contains special characters,
213+
<<esql-identifiers,it needs to be quoted>> with backticks (+{backtick}+).
214+
215+
Assigning an explicit name to a column created by `EVAL` or `STATS` is optional.
216+
If you don't provide a name, the new column name is equal to the function
217+
expression. For example:
218+
219+
[source,esql]
220+
----
221+
include::{esql-specs}/eval.csv-spec[tag=gs-eval-no-column-name]
222+
----
223+
224+
In this query, `EVAL` adds a new column named `event_duration/1000000.0`.
225+
Because its name contains special characters, to access this column, quote it
226+
with backticks:
227+
228+
[source,esql]
229+
----
230+
include::{esql-specs}/eval.csv-spec[tag=gs-eval-stats-backticks]
231+
----
232+
208233
[discrete]
209234
[[esql-getting-started-histogram]]
210235
=== Create a histogram

docs/reference/esql/processing-commands/eval.asciidoc

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[source,esql]
88
----
9-
EVAL column1 = value1[, ..., columnN = valueN]
9+
EVAL [column1 =] value1[, ..., [columnN =] valueN]
1010
----
1111

1212
*Parameters*
@@ -28,21 +28,46 @@ values. `EVAL` supports various functions for calculating values. Refer to
2828

2929
[source.merge.styled,esql]
3030
----
31-
include::{esql-specs}/docs.csv-spec[tag=eval]
31+
include::{esql-specs}/eval.csv-spec[tag=eval]
3232
----
3333
[%header.monospaced.styled,format=dsv,separator=|]
3434
|===
35-
include::{esql-specs}/docs.csv-spec[tag=eval-result]
35+
include::{esql-specs}/eval.csv-spec[tag=eval-result]
3636
|===
3737

3838
If the specified column already exists, the existing column will be dropped, and
3939
the new column will be appended to the table:
4040

4141
[source.merge.styled,esql]
4242
----
43-
include::{esql-specs}/docs.csv-spec[tag=evalReplace]
43+
include::{esql-specs}/eval.csv-spec[tag=evalReplace]
4444
----
4545
[%header.monospaced.styled,format=dsv,separator=|]
4646
|===
47-
include::{esql-specs}/docs.csv-spec[tag=evalReplace-result]
47+
include::{esql-specs}/eval.csv-spec[tag=evalReplace-result]
48+
|===
49+
50+
Specifying the output column name is optional. If not specified, the new column
51+
name is equal to the expression. The following query adds a column named
52+
`height*3.281`:
53+
54+
[source.merge.styled,esql]
55+
----
56+
include::{esql-specs}/eval.csv-spec[tag=evalUnnamedColumn]
57+
----
58+
[%header.monospaced.styled,format=dsv,separator=|]
59+
|===
60+
include::{esql-specs}/eval.csv-spec[tag=evalUnnamedColumn-result]
61+
|===
62+
63+
Because this name contains special characters, <<esql-identifiers,it needs to be
64+
quoted>> with backticks (+{backtick}+) when using it in subsequent commands:
65+
66+
[source.merge.styled,esql]
67+
----
68+
include::{esql-specs}/eval.csv-spec[tag=evalUnnamedColumnStats]
69+
----
70+
[%header.monospaced.styled,format=dsv,separator=|]
71+
|===
72+
include::{esql-specs}/eval.csv-spec[tag=evalUnnamedColumnStats-result]
4873
|===

docs/reference/esql/processing-commands/stats.asciidoc

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,61 @@ Calculating a statistic and grouping by the values of another column:
4747

4848
[source.merge.styled,esql]
4949
----
50-
include::{esql-specs}/docs.csv-spec[tag=stats]
50+
include::{esql-specs}/stats.csv-spec[tag=stats]
5151
----
5252
[%header.monospaced.styled,format=dsv,separator=|]
5353
|===
54-
include::{esql-specs}/docs.csv-spec[tag=stats-result]
54+
include::{esql-specs}/stats.csv-spec[tag=stats-result]
5555
|===
5656

5757
Omitting `BY` returns one row with the aggregations applied over the entire
5858
dataset:
5959

6060
[source.merge.styled,esql]
6161
----
62-
include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy]
62+
include::{esql-specs}/stats.csv-spec[tag=statsWithoutBy]
6363
----
6464
[%header.monospaced.styled,format=dsv,separator=|]
6565
|===
66-
include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy-result]
66+
include::{esql-specs}/stats.csv-spec[tag=statsWithoutBy-result]
6767
|===
6868

6969
It's possible to calculate multiple values:
7070

7171
[source,esql]
7272
----
73-
include::{esql-specs}/docs.csv-spec[tag=statsCalcMultipleValues]
73+
include::{esql-specs}/stats.csv-spec[tag=statsCalcMultipleValues]
7474
----
7575

7676
It's also possible to group by multiple values (only supported for long and
7777
keyword family fields):
7878

7979
[source,esql]
8080
----
81-
include::{esql-specs}/docs.csv-spec[tag=statsGroupByMultipleValues]
81+
include::{esql-specs}/stats.csv-spec[tag=statsGroupByMultipleValues]
8282
----
83+
84+
Specifying the output column name is optional. If not specified, the new column
85+
name is equal to the expression. The following query returns a column named
86+
`AVG(salary)`:
87+
88+
[source.merge.styled,esql]
89+
----
90+
include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumn]
91+
----
92+
[%header.monospaced.styled,format=dsv,separator=|]
93+
|===
94+
include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumn-result]
95+
|===
96+
97+
Because this name contains special characters, <<esql-identifiers,it needs to be
98+
quoted>> with backticks (+{backtick}+) when using it in subsequent commands:
99+
100+
[source.merge.styled,esql]
101+
----
102+
include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumnEval]
103+
----
104+
[%header.monospaced.styled,format=dsv,separator=|]
105+
|===
106+
include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumnEval-result]
107+
|===

x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,6 @@ FROM employees
1818
avg_worked_seconds:long | birth_date:date | emp_no:integer | first_name:keyword | gender:keyword | hire_date:date | is_rehired:boolean | job_positions:keyword | languages:integer | languages.byte:integer | languages.long:long | languages.short:integer | last_name:keyword | salary:integer | salary_change:double | salary_change.int:integer |salary_change.keyword:keyword |salary_change.long:long | still_hired:boolean
1919
;
2020

21-
docsEval
22-
// tag::eval[]
23-
FROM employees
24-
| SORT emp_no
25-
| KEEP first_name, last_name, height
26-
| EVAL height_feet = height * 3.281, height_cm = height * 100
27-
// end::eval[]
28-
| WHERE first_name == "Georgi"
29-
| LIMIT 1;
30-
31-
// tag::eval-result[]
32-
first_name:keyword | last_name:keyword | height:double | height_feet:double | height_cm:double
33-
Georgi |Facello | 2.03 | 6.66043 | 202.99999999999997
34-
// end::eval-result[]
35-
;
36-
37-
docsEvalReplace
38-
// tag::evalReplace[]
39-
FROM employees
40-
| SORT emp_no
41-
| KEEP first_name, last_name, height
42-
| EVAL height = height * 3.281
43-
// end::evalReplace[]
44-
| WHERE first_name == "Georgi"
45-
| LIMIT 1;
46-
47-
// tag::evalReplace-result[]
48-
first_name:keyword | last_name:keyword | height:double
49-
Georgi | Facello | 6.66043
50-
// end::evalReplace-result[]
51-
;
52-
5321
docsLimit
5422
// tag::limit[]
5523
FROM employees
@@ -187,67 +155,6 @@ null |Lortz |1.53
187155
null |Brender |1.55
188156
;
189157

190-
docsStats
191-
// tag::stats[]
192-
FROM employees
193-
| STATS count = COUNT(emp_no) BY languages
194-
| SORT languages
195-
// end::stats[]
196-
;
197-
198-
// tag::stats-result[]
199-
count:long | languages:integer
200-
15 |1
201-
19 |2
202-
17 |3
203-
18 |4
204-
21 |5
205-
10 |null
206-
// end::stats-result[]
207-
;
208-
209-
docsStatsWithoutBy
210-
// tag::statsWithoutBy[]
211-
FROM employees
212-
| STATS avg_lang = AVG(languages)
213-
// end::statsWithoutBy[]
214-
;
215-
216-
// tag::statsWithoutBy-result[]
217-
avg_lang:double
218-
3.1222222222222222
219-
// end::statsWithoutBy-result[]
220-
;
221-
222-
docsStatsMultiple
223-
// tag::statsCalcMultipleValues[]
224-
FROM employees
225-
| STATS avg_lang = AVG(languages), max_lang = MAX(languages)
226-
// end::statsCalcMultipleValues[]
227-
;
228-
229-
avg_lang:double | max_lang:integer
230-
3.1222222222222222|5
231-
;
232-
233-
docsStatsGroupByMultipleValues
234-
// tag::statsGroupByMultipleValues[]
235-
FROM employees
236-
| EVAL hired = DATE_FORMAT("YYYY", hire_date)
237-
| STATS avg_salary = AVG(salary) BY hired, languages.long
238-
| EVAL avg_salary = ROUND(avg_salary)
239-
| SORT hired, languages.long
240-
// end::statsGroupByMultipleValues[]
241-
| LIMIT 4
242-
;
243-
244-
hired:keyword |languages.long:long | avg_salary:double
245-
1985 |1 |54668.0
246-
1985 |3 |47723.0
247-
1985 |4 |44817.0
248-
1985 |5 |47720.0
249-
;
250-
251158
docsWhere
252159
// tag::where[]
253160
FROM employees

x-pack/plugin/esql/qa/testFixtures/src/main/resources/eval.csv-spec

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Anneke |Preusig |Anneke Preusig
243243
docsGettingStartedEval
244244
// tag::gs-eval[]
245245
FROM sample_data
246-
| EVAL duration_ms = event_duration / 1000000.0
246+
| EVAL duration_ms = event_duration/1000000.0
247247
// end::gs-eval[]
248248
| LIMIT 0
249249
;
@@ -254,10 +254,98 @@ FROM sample_data
254254
docsGettingStartedRound
255255
// tag::gs-round[]
256256
FROM sample_data
257-
| EVAL duration_ms = ROUND(event_duration / 1000000.0, 1)
257+
| EVAL duration_ms = ROUND(event_duration/1000000.0, 1)
258258
// end::gs-round[]
259259
| LIMIT 0
260260
;
261261

262262
@timestamp:date | client_ip:ip | event_duration:long | message:keyword | duration_ms:double
263263
;
264+
265+
docsGettingStartedEvalNoColumnName
266+
// tag::gs-eval-no-column-name[]
267+
FROM sample_data
268+
| EVAL event_duration/1000000.0
269+
// end::gs-eval-no-column-name[]
270+
| LIMIT 0
271+
;
272+
273+
@timestamp:date | client_ip:ip | event_duration:long | message:keyword | event_duration/1000000.0:double
274+
;
275+
276+
docsGettingStartedEvalStatsBackticks
277+
// tag::gs-eval-stats-backticks[]
278+
FROM sample_data
279+
| EVAL event_duration/1000000.0
280+
| STATS MEDIAN(`event_duration/1000000.0`)
281+
// end::gs-eval-stats-backticks[]
282+
;
283+
284+
MEDIAN(`event_duration/1000000.0`):double
285+
2.764889
286+
;
287+
288+
docsEval
289+
// tag::eval[]
290+
FROM employees
291+
| SORT emp_no
292+
| KEEP first_name, last_name, height
293+
| EVAL height_feet = height * 3.281, height_cm = height * 100
294+
// end::eval[]
295+
| LIMIT 3;
296+
297+
// tag::eval-result[]
298+
first_name:keyword | last_name:keyword | height:double | height_feet:double | height_cm:double
299+
Georgi |Facello |2.03 |6.66043 |202.99999999999997
300+
Bezalel |Simmel |2.08 |6.82448 |208.0
301+
Parto |Bamford |1.83 |6.004230000000001 |183.0
302+
// end::eval-result[]
303+
;
304+
305+
docsEvalReplace
306+
// tag::evalReplace[]
307+
FROM employees
308+
| SORT emp_no
309+
| KEEP first_name, last_name, height
310+
| EVAL height = height * 3.281
311+
// end::evalReplace[]
312+
| LIMIT 3;
313+
314+
// tag::evalReplace-result[]
315+
first_name:keyword | last_name:keyword | height:double
316+
Georgi |Facello |6.66043
317+
Bezalel |Simmel |6.82448
318+
Parto |Bamford |6.004230000000001
319+
// end::evalReplace-result[]
320+
;
321+
322+
docsEvalUnnamedColumn
323+
// tag::evalUnnamedColumn[]
324+
FROM employees
325+
| SORT emp_no
326+
| KEEP first_name, last_name, height
327+
| EVAL height * 3.281
328+
// end::evalUnnamedColumn[]
329+
| LIMIT 3;
330+
331+
// tag::evalUnnamedColumn-result[]
332+
first_name:keyword | last_name:keyword | height:double | height*3.281:double
333+
Georgi |Facello |2.03 |6.66043
334+
Bezalel |Simmel |2.08 |6.82448
335+
Parto |Bamford |1.83 |6.004230000000001
336+
// end::evalUnnamedColumn-result[]
337+
;
338+
339+
docsEvalUnnamedColumnStats
340+
// tag::evalUnnamedColumnStats[]
341+
FROM employees
342+
| EVAL height * 3.281
343+
| STATS avg_height_feet = AVG(`height*3.281`)
344+
// end::evalUnnamedColumnStats[]
345+
;
346+
347+
// tag::evalUnnamedColumnStats-result[]
348+
avg_height_feet:double
349+
5.801464200000001
350+
// end::evalUnnamedColumnStats-result[]
351+
;

0 commit comments

Comments
 (0)