-
Notifications
You must be signed in to change notification settings - Fork 25.9k
ESQL: INLINESTATS #109583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESQL: INLINESTATS #109583
Changes from 17 commits
e2d9da7
14017d6
355905a
a634f90
8f04e1b
3a3939b
5483426
7695f67
64f858b
141a63f
d0dc736
6947e1c
cc44421
8466a4e
cc20b73
2f9b8af
c4f1d87
1408824
2a38bc8
b786c1c
547e5a5
0eb2958
32a03b2
966c860
83252cf
40d3fe9
50fc6e2
d33a445
a868e7e
0bbdef4
e19c769
5d019f4
434bd9b
0d5d0da
e7eb532
908dfc9
2386fa8
887b9ce
2e028ce
1dfe527
ab350c4
2d90569
c937834
0a9332c
c30230c
9d12a29
6f690b7
18d30f0
9ff0024
c142d61
76998b7
40b13df
48253a4
674d93d
4489b27
8074a95
10b1fcd
fdb43d8
cbb1e60
8fbe301
09d226a
0e454f7
a6ec9be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| [discrete] | ||
| [[esql-inlinestats-by]] | ||
| === `INLINESTATS ... BY` | ||
|
|
||
| **Syntax** | ||
|
|
||
| [source,esql] | ||
| ---- | ||
| INLINESTATS [column1 =] expression1[, ..., [columnN =] expressionN] | ||
| [BY grouping_expression1[, ..., grouping_expressionN]] | ||
| ---- | ||
|
|
||
| *Parameters* | ||
|
|
||
| `columnX`:: | ||
| The name by which the aggregated value is returned. If omitted, the name is | ||
| equal to the corresponding expression (`expressionX`). | ||
|
nik9000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| `expressionX`:: | ||
| An expression that computes an aggregated value. | ||
|
|
||
| `grouping_expressionX`:: | ||
| An expression that outputs the values to group by. | ||
|
nik9000 marked this conversation as resolved.
|
||
|
|
||
| NOTE: Individual `null` values are skipped when computing aggregations. | ||
|
|
||
| *Description* | ||
|
|
||
| The `INLINESTATS` command calculates an aggregate result and merges that result | ||
| back into the stream of input data. Without the optional `BY` clause this will | ||
| produce a single result which is merged into the input. With a `BY` clause this | ||
|
nik9000 marked this conversation as resolved.
Outdated
|
||
| will produce one result per grouping merge the result into the stream based on | ||
| matching group keys. | ||
|
nik9000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| All of the <<esql-agg-functions,aggregation functions>> are supported. | ||
|
|
||
| *Examples* | ||
|
|
||
| Find the employees that speak the most languages (it's a tie!): | ||
|
|
||
| [source.merge.styled,esql] | ||
| ---- | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=max-languages] | ||
| ---- | ||
| [%header.monospaced.styled,format=dsv,separator=|] | ||
| |=== | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=max-languages-result] | ||
| |=== | ||
|
|
||
| Find the longest tenured employee who's last name starts with each letter of the alphabet: | ||
|
|
||
| [source.merge.styled,esql] | ||
| ---- | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first] | ||
| ---- | ||
| [%header.monospaced.styled,format=dsv,separator=|] | ||
| |=== | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first-result] | ||
| |=== | ||
|
|
||
| Find the northern and southern most airports: | ||
|
|
||
| [source.merge.styled,esql] | ||
| ---- | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports] | ||
| ---- | ||
| [%header.monospaced.styled,format=dsv,separator=|] | ||
| |=== | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports-result] | ||
| |=== | ||
|
|
||
| NOTE: Our test data doesn't have many "small" airports. | ||
|
|
||
| If a `BY` field is multivalued then `INLINESTATS` will put the row in *each* | ||
| bucket like <<esql-stats-by>>. | ||
|
nik9000 marked this conversation as resolved.
Outdated
|
||
|
nik9000 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| maxOfInt | ||
|
nik9000 marked this conversation as resolved.
|
||
| required_capability: inlinestats | ||
|
|
||
| // tag::max-languages[] | ||
| FROM employees | ||
| | KEEP emp_no, languages | ||
| | INLINESTATS max_lang = MAX(languages) | ||
| | WHERE max_lang == languages | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice examples ❤️ . It would be lovely if we could push down the filter in the second phase to Lucene instead of using HashJoin, so that we can avoid scanning the entire dataset. Let's address this later.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah! I'm pretty sure we can push these down. That's on the followup list! |
||
| | SORT emp_no ASC | ||
| | LIMIT 5 | ||
| // end::max-languages[] | ||
| ; | ||
|
|
||
| // tag::max-languages-result[] | ||
| emp_no:integer | languages:integer | max_lang:integer | ||
| 10002 | 5 | 5 | ||
| 10004 | 5 | 5 | ||
| 10011 | 5 | 5 | ||
| 10012 | 5 | 5 | ||
| 10014 | 5 | 5 | ||
| // end::max-languages-result[] | ||
| ; | ||
|
|
||
| maxOfIntByKeyword | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, languages, gender | ||
| | INLINESTATS max_lang = MAX(languages) BY gender | ||
| | WHERE max_lang == languages | ||
| | SORT emp_no ASC | ||
| | LIMIT 5; | ||
|
|
||
| emp_no:integer | languages:integer | gender:keyword | max_lang:integer | ||
| 10002 | 5 | F | 5 | ||
| 10004 | 5 | M | 5 | ||
| 10011 | 5 | null | 5 | ||
| 10012 | 5 | null | 5 | ||
| 10014 | 5 | null | 5 | ||
| ; | ||
|
|
||
| maxOfLongByKeyword | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, avg_worked_seconds, gender | ||
| | INLINESTATS max_avg_worked_seconds = MAX(avg_worked_seconds) BY gender | ||
| | WHERE max_avg_worked_seconds == avg_worked_seconds | ||
| | SORT emp_no ASC; | ||
|
|
||
| emp_no:integer | avg_worked_seconds:long | gender:keyword | max_avg_worked_seconds:long | ||
| 10007 | 393084805 | F | 393084805 | ||
| 10015 | 390266432 | null | 390266432 | ||
| 10030 | 394597613 | M | 394597613 | ||
| ; | ||
|
|
||
| maxOfLong | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, avg_worked_seconds, gender | ||
| | INLINESTATS max_avg_worked_seconds = MAX(avg_worked_seconds) | ||
| | WHERE max_avg_worked_seconds == avg_worked_seconds | ||
| | SORT emp_no ASC; | ||
|
|
||
| emp_no:integer | avg_worked_seconds:long | gender:keyword | max_avg_worked_seconds:long | ||
| 10030 | 394597613 | M | 394597613 | ||
| ; | ||
|
|
||
| // TODO allow inline calculation like BY l = SUBSTRING( | ||
| maxOfLongByCalculatedKeyword | ||
| required_capability: inlinestats | ||
|
|
||
| // tag::longest-tenured-by-first[] | ||
| FROM employees | ||
| | EVAL l = SUBSTRING(last_name, 0, 1) | ||
| | KEEP emp_no, avg_worked_seconds, l | ||
| | INLINESTATS max_avg_worked_seconds = MAX(avg_worked_seconds) BY l | ||
| | WHERE max_avg_worked_seconds == avg_worked_seconds | ||
| | SORT l ASC | ||
| | LIMIT 5 | ||
| // end::longest-tenured-by-first[] | ||
| ; | ||
|
|
||
| // tag::longest-tenured-by-first-result[] | ||
| emp_no:integer | avg_worked_seconds:long | l:keyword | max_avg_worked_seconds:long | ||
| 10065 | 372660279 | A | 372660279 | ||
| 10074 | 382397583 | B | 382397583 | ||
| 10044 | 387408356 | C | 387408356 | ||
| 10030 | 394597613 | D | 394597613 | ||
| 10087 | 305782871 | E | 305782871 | ||
| // end::longest-tenured-by-first-result[] | ||
| ; | ||
|
|
||
| maxOfLongByInt | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, avg_worked_seconds, languages | ||
| | INLINESTATS max_avg_worked_seconds = MAX(avg_worked_seconds) BY languages | ||
| | WHERE max_avg_worked_seconds == avg_worked_seconds | ||
| | SORT languages ASC; | ||
|
|
||
| emp_no:integer | avg_worked_seconds:long | languages:integer | max_avg_worked_seconds:long | ||
| 10044 | 387408356 | 1 | 387408356 | ||
| 10099 | 377713748 | 2 | 377713748 | ||
| 10030 | 394597613 | 3 | 394597613 | ||
| 10007 | 393084805 | 4 | 393084805 | ||
| 10015 | 390266432 | 5 | 390266432 | ||
| 10027 | 374037782 | null | 374037782 | ||
| ; | ||
|
|
||
| maxOfLongByIntDouble | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, avg_worked_seconds, languages, height | ||
| | EVAL height=ROUND(height, 1) | ||
| | INLINESTATS max_avg_worked_seconds = MAX(avg_worked_seconds) BY languages, height | ||
| | WHERE max_avg_worked_seconds == avg_worked_seconds | ||
| | SORT languages, height ASC | ||
| | LIMIT 4; | ||
|
|
||
| emp_no:integer | avg_worked_seconds:long | languages:integer | height:double | max_avg_worked_seconds:long | ||
| 10083 | 331236443 | 1 | 1.4 | 331236443 | ||
| 10084 | 359067056 | 1 | 1.5 | 359067056 | ||
| 10033 | 208374744 | 1 | 1.6 | 208374744 | ||
| 10086 | 328580163 | 1 | 1.7 | 328580163 | ||
| ; | ||
|
|
||
|
|
||
| two | ||
| required_capability: inlinestats | ||
|
|
||
| FROM employees | ||
| | KEEP emp_no, languages, avg_worked_seconds, gender | ||
| | INLINESTATS avg_avg_worked_seconds = AVG(avg_worked_seconds) BY languages | ||
| | WHERE avg_worked_seconds > avg_avg_worked_seconds | ||
| | INLINESTATS max_languages = MAX(languages) BY gender | ||
| | SORT emp_no ASC | ||
| | LIMIT 3; | ||
|
|
||
| emp_no:integer | languages:integer | avg_worked_seconds:long | gender:keyword | avg_avg_worked_seconds:double | max_languages:integer | ||
| 10002 | 5 | 328922887 | F | 3.133013149047619E8 | 5 | ||
| 10006 | 3 | 372957040 | F | 2.978159518235294E8 | 5 | ||
| 10007 | 4 | 393084805 | F | 2.863684210555556E8 | 5 | ||
| ; | ||
|
|
||
| byMultivaluedSimple | ||
| required_capability: inlinestats | ||
|
|
||
| // tag:mv-simple | ||
| FROM airports | ||
| | INLINESTATS min_scalerank=MIN(scalerank) BY type | ||
| | EVAL type=MV_SORT(type), min_scalerank=MV_SORT(min_scalerank) | ||
| | KEEP abbrev, type, scalerank, min_scalerank | ||
| | WHERE abbrev == "GWL" | ||
| // tag:mv-simple | ||
| ; | ||
|
|
||
| // tag:mv-simple-result | ||
| abbrev:keyword | type:keyword | scalerank:integer | min_scalerank:integer | ||
| GWL | [mid, military] | 9 | [2, 4] | ||
| // end:mv-simple-result | ||
| ; | ||
|
|
||
| byMultivaluedMvExpand | ||
| required_capability: inlinestats | ||
|
|
||
| FROM airports | ||
| | KEEP abbrev, type, scalerank | ||
| | MV_EXPAND type | ||
| | INLINESTATS min_scalerank=MIN(scalerank) BY type | ||
| | SORT min_scalerank ASC | ||
| | WHERE abbrev == "GWL"; | ||
|
|
||
| abbrev:keyword | type:keyword | scalerank:integer | min_scalerank:integer | ||
| GWL | mid | 9 | 2 | ||
| GWL | military | 9 | 4 | ||
| ; | ||
|
|
||
| byMvExpand | ||
| required_capability: inlinestats | ||
|
|
||
| // tag::extreme-airports[] | ||
| FROM airports | ||
| | MV_EXPAND type | ||
| | EVAL lat = ST_Y(location) | ||
| | INLINESTATS most_northern=MAX(lat), most_southern=MIN(lat) BY type | ||
| | WHERE lat == most_northern OR lat == most_southern | ||
| | SORT lat DESC | ||
| | KEEP type, name, location | ||
| // end::extreme-airports[] | ||
| ; | ||
|
|
||
| // tag::extreme-airports-result[] | ||
| type:keyword | name:text | location:geo_point | ||
| mid | Svalbard Longyear | POINT (15.495229 78.246717) | ||
| major | Tromsø Langnes | POINT (18.9072624292132 69.6796790473478) | ||
| military | Severomorsk-3 (Murmansk N.E.) | POINT (33.2903527616285 69.0168711826804) | ||
| spaceport | Baikonur Cosmodrome | POINT (63.307354423875 45.9635739403124) | ||
| small | Dhamial | POINT (73.0320498392002 33.5614146278861) | ||
| small | Sahnewal | POINT (75.9570722403652 30.8503598561702) | ||
| spaceport | Centre Spatial Guyanais | POINT (-52.7684296893452 5.23941001258035) | ||
| military | Santos Air Force Base | POINT (-46.3052704931003 -23.9237590410637) | ||
| major | Christchurch Int'l | POINT (172.538675565223 -43.4885486784104) | ||
| mid | Hermes Quijada Int'l | POINT (-67.7530268462675 -53.7814746058316) | ||
| // end::extreme-airports-result[] | ||
| ; | ||
|
|
||
| brokenwhy-Ignore | ||
| required_capability: inlinestats | ||
|
nik9000 marked this conversation as resolved.
|
||
|
|
||
| FROM airports | ||
| | INLINESTATS min_scalerank=MIN(scalerank) BY type | ||
| | MV_EXPAND type | ||
| | WHERE scalerank == MV_MIN(scalerank); | ||
|
|
||
| abbrev:keyword | type:keyword | scalerank:integer | min_scalerank:integer | ||
| GWL | [mid, military] | 9 | [2, 4] | ||
| ; | ||
Uh oh!
There was an error while loading. Please reload this page.