-
Notifications
You must be signed in to change notification settings - Fork 25.4k
ESQL: Improve local folding of aggregates #103670
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,5 @@ | ||
pr: 103670 | ||
summary: "ESQL: Improve local folding of aggregates" | ||
area: ES|QL | ||
type: bug | ||
issues: [] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.compute.data.Block; | ||
import org.elasticsearch.compute.data.BlockFactory; | ||
import org.elasticsearch.compute.data.BlockUtils; | ||
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.esql.evaluator.predicate.operator.comparison.Equals; | ||
|
@@ -24,7 +25,6 @@ | |
import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject; | ||
import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation; | ||
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier; | ||
import org.elasticsearch.xpack.esql.planner.AbstractPhysicalOperationProviders; | ||
import org.elasticsearch.xpack.esql.planner.PlannerUtils; | ||
import org.elasticsearch.xpack.esql.type.EsqlDataTypes; | ||
import org.elasticsearch.xpack.ql.expression.Alias; | ||
|
@@ -62,7 +62,6 @@ | |
import org.elasticsearch.xpack.ql.rule.ParameterizedRuleExecutor; | ||
import org.elasticsearch.xpack.ql.rule.Rule; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
import org.elasticsearch.xpack.ql.type.DataType; | ||
import org.elasticsearch.xpack.ql.type.DataTypes; | ||
import org.elasticsearch.xpack.ql.util.CollectionUtils; | ||
import org.elasticsearch.xpack.ql.util.Holder; | ||
|
@@ -100,17 +99,8 @@ protected List<Batch<LogicalPlan>> batches() { | |
return rules(); | ||
} | ||
|
||
protected static List<Batch<LogicalPlan>> rules() { | ||
var substitutions = new Batch<>( | ||
"Substitutions", | ||
Limiter.ONCE, | ||
new SubstituteSurrogates(), | ||
new ReplaceRegexMatch(), | ||
new ReplaceAliasingEvalWithProject() | ||
// new NormalizeAggregate(), - waits on https://github.com/elastic/elasticsearch/issues/100634 | ||
); | ||
|
||
var operators = new Batch<>( | ||
protected static Batch<LogicalPlan> operators() { | ||
return new Batch<>( | ||
"Operator Optimization", | ||
new CombineProjections(), | ||
new CombineEvals(), | ||
|
@@ -145,19 +135,33 @@ protected static List<Batch<LogicalPlan>> rules() { | |
new PruneOrderByBeforeStats(), | ||
new PruneRedundantSortClauses() | ||
); | ||
} | ||
|
||
var skip = new Batch<>("Skip Compute", new SkipQueryOnLimitZero()); | ||
var cleanup = new Batch<>( | ||
protected static Batch<LogicalPlan> cleanup() { | ||
return new Batch<>( | ||
"Clean Up", | ||
new ReplaceDuplicateAggWithEval(), | ||
// pushing down limits again, because ReplaceDuplicateAggWithEval could create new Project nodes that can still be optimized | ||
new PushDownAndCombineLimits(), | ||
new ReplaceLimitAndSortAsTopN() | ||
); | ||
} | ||
|
||
protected static List<Batch<LogicalPlan>> rules() { | ||
var substitutions = new Batch<>( | ||
"Substitutions", | ||
Limiter.ONCE, | ||
new SubstituteSurrogates(), | ||
new ReplaceRegexMatch(), | ||
new ReplaceAliasingEvalWithProject() | ||
// new NormalizeAggregate(), - waits on https://github.com/elastic/elasticsearch/issues/100634 | ||
); | ||
|
||
var skip = new Batch<>("Skip Compute", new SkipQueryOnLimitZero()); | ||
var defaultTopN = new Batch<>("Add default TopN", new AddDefaultTopN()); | ||
var label = new Batch<>("Set as Optimized", Limiter.ONCE, new SetAsOptimized()); | ||
|
||
return asList(substitutions, operators, skip, cleanup, defaultTopN, label); | ||
return asList(substitutions, operators(), skip, cleanup(), defaultTopN, label); | ||
} | ||
|
||
// TODO: currently this rule only works for aggregate functions (AVG) | ||
|
@@ -632,6 +636,7 @@ protected LogicalPlan rule(UnaryPlan plan) { | |
} | ||
} | ||
|
||
@SuppressWarnings("removal") | ||
static class PropagateEmptyRelation extends OptimizerRules.OptimizerRule<UnaryPlan> { | ||
|
||
@Override | ||
|
@@ -649,39 +654,31 @@ protected LogicalPlan rule(UnaryPlan plan) { | |
return p; | ||
} | ||
|
||
private static List<Block> aggsFromEmpty(List<? extends NamedExpression> aggs) { | ||
// TODO: Should we introduce skip operator that just never queries the source | ||
private List<Block> aggsFromEmpty(List<? extends NamedExpression> aggs) { | ||
List<Block> blocks = new ArrayList<>(); | ||
var blockFactory = PlannerUtils.NON_BREAKING_BLOCK_FACTORY; | ||
int i = 0; | ||
for (var agg : aggs) { | ||
// there needs to be an alias | ||
if (agg instanceof Alias a && a.child() instanceof AggregateFunction aggFunc) { | ||
List<Attribute> output = AbstractPhysicalOperationProviders.intermediateAttributes(List.of(agg), List.of()); | ||
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. This section is now moved in the local rule. |
||
for (Attribute o : output) { | ||
DataType dataType = o.dataType(); | ||
// fill the boolean block later in LocalExecutionPlanner | ||
if (dataType != DataTypes.BOOLEAN) { | ||
// look for count(literal) with literal != null | ||
var wrapper = BlockUtils.wrapperFor( | ||
PlannerUtils.NON_BREAKING_BLOCK_FACTORY, | ||
PlannerUtils.toElementType(dataType), | ||
1 | ||
); | ||
if (aggFunc instanceof Count count && (count.foldable() == false || count.fold() != null)) { | ||
wrapper.accept(0L); | ||
} else { | ||
wrapper.accept(null); | ||
} | ||
blocks.add(wrapper.builder().build()); | ||
} | ||
} | ||
aggOutput(agg, aggFunc, blockFactory, blocks); | ||
} else { | ||
throw new EsqlIllegalArgumentException("Did not expect a non-aliased aggregation {}", agg); | ||
} | ||
} | ||
return blocks; | ||
} | ||
|
||
/** | ||
* The folded aggregation output - this variant is for the coordinator/final. | ||
*/ | ||
protected void aggOutput(NamedExpression agg, AggregateFunction aggFunc, BlockFactory blockFactory, List<Block> blocks) { | ||
// look for count(literal) with literal != null | ||
Object value = aggFunc instanceof Count count && (count.foldable() == false || count.fold() != null) ? 0L : null; | ||
var wrapper = BlockUtils.wrapperFor(blockFactory, PlannerUtils.toElementType(aggFunc.dataType()), 1); | ||
wrapper.accept(value); | ||
blocks.add(wrapper.builder().build()); | ||
} | ||
} | ||
|
||
private static LogicalPlan skipPlan(UnaryPlan plan) { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gist of this PR - override the global rule with a specific local rule that takes into account the intermediate aggregation and also takes care of handling the boolean types (seen).
This removes the weird logic downstream in the LocalExecutionPlanner as its much more contained.