-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
Merged
Merged
ESQL: INLINESTATS #109583
Changes from 31 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
e2d9da7
ESQL: INLINESTATS
nik9000 14017d6
Explain
nik9000 355905a
More nocommit
nik9000 a634f90
More nocommit
nik9000 8f04e1b
Spotless
nik9000 3a3939b
Merge branch 'main' into inlinestats
nik9000 5483426
Works again
nik9000 7695f67
Closer
nik9000 64f858b
Merge branch 'main' into inlinestats
nik9000 141a63f
More test
nik9000 d0dc736
Share
nik9000 6947e1c
Merge branch 'main' into inlinestats
nik9000 cc44421
More
nik9000 8466a4e
ungrouped
nik9000 cc20b73
WIt P
nik9000 2f9b8af
Merge branch 'main' into inlinestats
nik9000 c4f1d87
Remove
nik9000 1408824
Remove unused
nik9000 2a38bc8
Merge branch 'main' into inlinestats
nik9000 b786c1c
More test
nik9000 547e5a5
Merge
nik9000 0eb2958
More nocommit
nik9000 32a03b2
explain
nik9000 966c860
Merge branch 'main' into inlinestats
nik9000 83252cf
WIP
nik9000 40d3fe9
Passes now?
nik9000 50fc6e2
one more exampl
nik9000 d33a445
Merge branch 'main' into inlinestats
nik9000 a868e7e
Javadoc
nik9000 0bbdef4
MOAR JAVADOC
nik9000 e19c769
Update docs/changelog/109583.yaml
nik9000 5d019f4
Changelog
nik9000 434bd9b
WIP
nik9000 0d5d0da
Merge branch 'main' into inlinestats
nik9000 e7eb532
Ready?
nik9000 908dfc9
Update docs
nik9000 2386fa8
Raname to line up with other stuff
nik9000 887b9ce
More
nik9000 2e028ce
Merge branch 'main' into inlinestats
nik9000 1dfe527
Merge branch 'main' into inlinestats
nik9000 ab350c4
Apply suggestions from code review
nik9000 2d90569
Merge remote-tracking branch 'nik9000/inlinestats' into inlinestats
nik9000 c937834
Update docs
nik9000 0a9332c
Updates
nik9000 c30230c
More explain and a couple renames
nik9000 9d12a29
Format
nik9000 6f690b7
Merge branch 'main' into inlinestats
nik9000 18d30f0
Some progress
nik9000 9ff0024
percentile
nik9000 c142d61
Better way?
nik9000 76998b7
Merge branch 'main' into inlinestats
nik9000 40b13df
techpreview
nik9000 48253a4
Merge branch 'main' into inlinestats
nik9000 674d93d
WIP
nik9000 4489b27
Update
nik9000 8074a95
Merge branch 'main' into inlinestats
nik9000 10b1fcd
Link
nik9000 fdb43d8
Check
nik9000 cbb1e60
Feature flag it
nik9000 8fbe301
Merge branch 'main' into inlinestats
nik9000 09d226a
WI{
nik9000 0e454f7
Merge branch 'main' into inlinestats
nik9000 a6ec9be
more skips
nik9000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| pr: 109583 | ||
| summary: "ESQL: INLINESTATS" | ||
| area: ES|QL | ||
| type: feature | ||
| issues: | ||
| - 107589 | ||
| highlight: | ||
| title: "ESQL: INLINESTATS" | ||
| body: |- | ||
| This implements `INLINESTATS`. Most of the heavy lifting is done by | ||
| `LOOKUP`, with this change mostly adding a new abstraction to logical | ||
| plans, an interface I'm calling `Phased`. Implementing this interface | ||
| allows a logical plan node to cut the query into phases. `INLINESTATS` | ||
| implements it by asking for a "first phase" that's the same query, up to | ||
| `INLINESTATS`, but with `INLINESTATS` replaced with `STATS`. The next | ||
| phase replaces the `INLINESTATS` with a hash join on the results of the | ||
| first phase. | ||
|
|
||
| So, this query: | ||
|
|
||
| ``` | ||
| FROM foo | ||
| | EVAL bar = a * b | ||
| | INLINESTATS m = MAX(bar) BY b | ||
| | WHERE m = bar | ||
| | LIMIT 1 | ||
| ``` | ||
|
|
||
| gets split into | ||
|
|
||
| ``` | ||
| FROM foo | ||
| | EVAL bar = a * b | ||
| | STATS m = MAX(bar) BY b | ||
| ``` | ||
|
|
||
| followed by | ||
|
|
||
| ``` | ||
| FROM foo | ||
| | EVAL bar = a * b | ||
| | LOOKUP (results of m = MAX(bar) BY b) ON b | ||
| | WHERE m = bar | ||
| | LIMIT 1 | ||
| ``` | ||
|
|
||
| Here's an example of the syntax: | ||
|
|
||
| ``` | ||
| $ curl -k -XDELETE -u'elastic:password' 'http://localhost:9200/test' | ||
| $ for a in {0..99}; do | ||
| echo -n $a | ||
| rm -f /tmp/bulk | ||
| for b in {0..999}; do | ||
| echo '{"index": {}}' >> /tmp/bulk | ||
| echo '{"a": '$a', "b": '$b'}' >> /tmp/bulk | ||
| done | ||
| curl -s -k -XPOST -u'elastic:password' -HContent-Type:application/json 'http://localhost:9200/test/_bulk?pretty' --data-binary @/tmp/bulk | grep errors | ||
| done | ||
| $ curl -s -k -XPOST -u'elastic:password' -HContent-Type:application/json 'http://localhost:9200/test/_forcemerge?max_num_segments=1&pretty' | ||
| $ curl -s -k -XPOST -u'elastic:password' -HContent-Type:application/json 'http://localhost:9200/test/_refresh?pretty' | ||
| $ curl -k -XPOST -u'elastic:password' -HContent-Type:application/json http://localhost:9200/_query?pretty -d'{ | ||
| "query": "FROM test | INLINESTATS m=MAX(a * b) BY b | WHERE m == a * b | SORT a DESC, b DESC | LIMIT 1", | ||
| "profile": true | ||
| }' | ||
| { | ||
| "columns" : [ | ||
| { | ||
| "name" : "a", | ||
| "type" : "long" | ||
| }, | ||
| { | ||
| "name" : "b", | ||
| "type" : "long" | ||
| }, | ||
| { | ||
| "name" : "m", | ||
| "type" : "long" | ||
| } | ||
| ], | ||
| "values" : [ | ||
| [ | ||
| 99, | ||
| 999, | ||
| 98901 | ||
| ] | ||
| ``` | ||
|
|
||
| Closes #107589 | ||
| notable: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
docs/reference/esql/processing-commands/inlinestats.asciidoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| [discrete] | ||
| [[esql-inlinestats-by]] | ||
| === `INLINESTATS ... BY` | ||
|
|
||
| The `INLINESTATS` command calculates an aggregate result and merges that result | ||
| back into the stream of input data. | ||
|
nik9000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| **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* | ||
|
|
||
| Add the maximum : | ||
|
|
||
| [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 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>>: | ||
|
|
||
| [source.merge.styled,esql] | ||
| ---- | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=mv-group] | ||
| ---- | ||
| [%header.monospaced.styled,format=dsv,separator=|] | ||
| |=== | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=mv-group-result] | ||
| |=== | ||
|
|
||
| To treat each group key as its own row use <<esql-mv_expand>> before `INLINESTATS`: | ||
|
|
||
| [source.merge.styled,esql] | ||
| ---- | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand] | ||
| ---- | ||
| [%header.monospaced.styled,format=dsv,separator=|] | ||
| |=== | ||
| include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand-result] | ||
| |=== | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.