Skip to content

[ESQL] Plug TopN agg groupings filtering into the language#130111

Draft
ivancea wants to merge 95 commits intoelastic:mainfrom
ivancea:esql-top-n-agg-ql
Draft

[ESQL] Plug TopN agg groupings filtering into the language#130111
ivancea wants to merge 95 commits intoelastic:mainfrom
ivancea:esql-top-n-agg-ql

Conversation

@ivancea
Copy link
Copy Markdown
Contributor

@ivancea ivancea commented Jun 26, 2025

Continuation of #127148

Depends on #129633 (Bug fix)

The first PR added a new TopNBlockHash that skips non-topN groups for specific cases (TopN after Aggregate, sharing the same attributes for groups/order).

This PR plugs it into the language:

  • LogicalPlanOptimizer: Convert TopN + Aggregate to TopNAggregate. This is done at the very end, so other logical rules remain unchanged
  • Planner: Convert TopNAggregate to TopNAggregateExec
  • LocalLogicalPlanOptimizer: Just like TopN is converted to Limit and Sort at the beginning, here we do the same converting TopNAggregate to TopN and Aggregate
  • LocalPhysicalPlanOptimizer: TopNAggregateExec is a child class of AggregateExec, so most rules still apply. Some other were updated

Also, a new pragma was added (max_top_n_aggs_limit) to limit the maximum TopN limit after which the TopNBlockHash will be used. This is temporary until we use better index statistics.

Draft

  • Currently, TopNAggregate can't be serialized to older nodes. This must be fixed before merging.
    • A potential solution could be applying the pragma at logical plan optimization level, and skip the rule there instead of at physical plan to operator conversion. However, we don't have (currently) the pragmas accesible within the rules
    • Use TransportVersionAware (uses minTransportVersion), added in ESQL: SUM long overflow fix and TransportVersionAware interface #141272

Future changes

  • Update OrdinalsGroupingOperator to use the pragma for its BlockHash, if it makes sense

ivancea and others added 30 commits April 16, 2025 14:29
# Conflicts:
#	x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/aggregation/blockhash/BlockHashTests.java
aggregatorFactories,
context.pageSize(ts.estimatedRowSize())
context.pageSize(ts.estimatedRowSize()),
context.queryPragmas().maxTopNAggsLimit()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we inject the pragma

# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/mapper/Mapper.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LocalPhysicalPlanOptimizerTests.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java
# Conflicts:
#	x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/OrdinalsGroupingOperator.java
#	x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/OperatorTests.java
#	x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/HashAggregationOperatorTests.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/AbstractPhysicalOperationProviders.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/PhysicalPlanOptimizerTests.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.5.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants