Skip to content

Conversation

ivancea
Copy link
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

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
Copy link
Contributor Author

@ivancea ivancea Jun 27, 2025

Choose a reason for hiding this comment

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

This is an unrelated fix, as this class wasn't overriding those methods. I will move this to another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR opened with this: #130218

aggregatorFactories,
context.pageSize(ts.estimatedRowSize())
context.pageSize(ts.estimatedRowSize()),
context.queryPragmas().maxTopNAggsLimit()
Copy link
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.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants