Skip to content

Commit 2233349

Browse files
authored
ESQL: top_list aggregation (#109386)
Added `top_list(<field>, <limit>, <order>)` aggregation, that collect top N values per bucket. Works with the same types as MAX/MIN. - Added the aggregation function - Added a template to generate the aggregators - Added a template to generate the `<Type>BucketedSort` implementations per-type - This structure is based on the `BucketedSort` structure used on the original aggregations. It was modified to better fit the ESQL ecosystem (Blocks based, no docs...) Also added a guide to create aggregations. Fixes #109213
1 parent 0145a41 commit 2233349

File tree

41 files changed

+4364
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4364
-19
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG.asciidoc merge=union
44
# Windows
55
build-tools-internal/src/test/resources/org/elasticsearch/gradle/internal/release/*.asciidoc text eol=lf
66

7+
x-pack/plugin/esql/compute/src/main/generated/** linguist-generated=true
8+
x-pack/plugin/esql/compute/src/main/generated-src/** linguist-generated=true
79
x-pack/plugin/esql/src/main/antlr/*.tokens linguist-generated=true
810
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/*.interp linguist-generated=true
911
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseLexer*.java linguist-generated=true

docs/changelog/109386.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 109386
2+
summary: "ESQL: `top_list` aggregation"
3+
area: ES|QL
4+
type: feature
5+
issues:
6+
- 109213

x-pack/plugin/esql/compute/ann/src/main/java/org/elasticsearch/compute/ann/GroupingAggregator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import java.lang.annotation.RetentionPolicy;
1313
import java.lang.annotation.Target;
1414

15+
/**
16+
* Annotates a class that implements an aggregation function with grouping.
17+
* See {@link Aggregator} for more information.
18+
*/
1519
@Target(ElementType.TYPE)
1620
@Retention(RetentionPolicy.SOURCE)
1721
public @interface GroupingAggregator {

x-pack/plugin/esql/compute/build.gradle

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ spotless {
3636
}
3737
}
3838

39-
def prop(Type, type, TYPE, BYTES, Array, Hash) {
39+
def prop(Type, type, Wrapper, TYPE, BYTES, Array, Hash) {
4040
return [
4141
"Type" : Type,
4242
"type" : type,
43+
"Wrapper": Wrapper,
4344
"TYPE" : TYPE,
4445
"BYTES" : BYTES,
4546
"Array" : Array,
@@ -55,12 +56,13 @@ def prop(Type, type, TYPE, BYTES, Array, Hash) {
5556
}
5657

5758
tasks.named('stringTemplates').configure {
58-
var intProperties = prop("Int", "int", "INT", "Integer.BYTES", "IntArray", "LongHash")
59-
var floatProperties = prop("Float", "float", "FLOAT", "Float.BYTES", "FloatArray", "LongHash")
60-
var longProperties = prop("Long", "long", "LONG", "Long.BYTES", "LongArray", "LongHash")
61-
var doubleProperties = prop("Double", "double", "DOUBLE", "Double.BYTES", "DoubleArray", "LongHash")
62-
var bytesRefProperties = prop("BytesRef", "BytesRef", "BYTES_REF", "org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF", "", "BytesRefHash")
63-
var booleanProperties = prop("Boolean", "boolean", "BOOLEAN", "Byte.BYTES", "BitArray", "")
59+
var intProperties = prop("Int", "int", "Integer", "INT", "Integer.BYTES", "IntArray", "LongHash")
60+
var floatProperties = prop("Float", "float", "Float", "FLOAT", "Float.BYTES", "FloatArray", "LongHash")
61+
var longProperties = prop("Long", "long", "Long", "LONG", "Long.BYTES", "LongArray", "LongHash")
62+
var doubleProperties = prop("Double", "double", "Double", "DOUBLE", "Double.BYTES", "DoubleArray", "LongHash")
63+
var bytesRefProperties = prop("BytesRef", "BytesRef", "", "BYTES_REF", "org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF", "", "BytesRefHash")
64+
var booleanProperties = prop("Boolean", "boolean", "Boolean", "BOOLEAN", "Byte.BYTES", "BitArray", "")
65+
6466
// primitive vectors
6567
File vectorInputFile = new File("${projectDir}/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st")
6668
template {
@@ -500,6 +502,24 @@ tasks.named('stringTemplates').configure {
500502
it.outputFile = "org/elasticsearch/compute/aggregation/RateDoubleAggregator.java"
501503
}
502504

505+
506+
File topListAggregatorInputFile = new File("${projectDir}/src/main/java/org/elasticsearch/compute/aggregation/X-TopListAggregator.java.st")
507+
template {
508+
it.properties = intProperties
509+
it.inputFile = topListAggregatorInputFile
510+
it.outputFile = "org/elasticsearch/compute/aggregation/TopListIntAggregator.java"
511+
}
512+
template {
513+
it.properties = longProperties
514+
it.inputFile = topListAggregatorInputFile
515+
it.outputFile = "org/elasticsearch/compute/aggregation/TopListLongAggregator.java"
516+
}
517+
template {
518+
it.properties = doubleProperties
519+
it.inputFile = topListAggregatorInputFile
520+
it.outputFile = "org/elasticsearch/compute/aggregation/TopListDoubleAggregator.java"
521+
}
522+
503523
File multivalueDedupeInputFile = file("src/main/java/org/elasticsearch/compute/operator/mvdedupe/X-MultivalueDedupe.java.st")
504524
template {
505525
it.properties = intProperties
@@ -635,4 +655,21 @@ tasks.named('stringTemplates').configure {
635655
it.inputFile = resultBuilderInputFile
636656
it.outputFile = "org/elasticsearch/compute/operator/topn/ResultBuilderForFloat.java"
637657
}
658+
659+
File bucketedSortInputFile = new File("${projectDir}/src/main/java/org/elasticsearch/compute/data/sort/X-BucketedSort.java.st")
660+
template {
661+
it.properties = intProperties
662+
it.inputFile = bucketedSortInputFile
663+
it.outputFile = "org/elasticsearch/compute/data/sort/IntBucketedSort.java"
664+
}
665+
template {
666+
it.properties = longProperties
667+
it.inputFile = bucketedSortInputFile
668+
it.outputFile = "org/elasticsearch/compute/data/sort/LongBucketedSort.java"
669+
}
670+
template {
671+
it.properties = doubleProperties
672+
it.inputFile = bucketedSortInputFile
673+
it.outputFile = "org/elasticsearch/compute/data/sort/DoubleBucketedSort.java"
674+
}
638675
}

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/TopListDoubleAggregator.java

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/TopListIntAggregator.java

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)