-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ESQL: Add asynchronous pre-optimization step for logical plan #131440
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
afoucret
merged 9 commits into
elastic:main
from
afoucret:esql-logical-plan-pre-optimizer
Jul 21, 2025
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0568a4d
ESQL: Add asynchronous pre-optimization step for logical plan
afoucret 5da14bb
[CI] Auto commit changes from spotless
38aa860
Fix copy//paste
afoucret a4d27ac
Revert uselss change in QueryPlanningBenchmark
afoucret d4e857d
Lint
afoucret b061fc7
Lint again
afoucret 26d815e
Fix CsvTests
afoucret d2355b8
Merge branch 'main' into esql-logical-plan-pre-optimizer
afoucret 4b0cca8
Merge branch 'main' into esql-logical-plan-pre-optimizer
afoucret 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
50 changes: 50 additions & 0 deletions
50
...in/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanPreOptimizer.java
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,50 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
|
|
||
| /** | ||
| * The class is responsible for invoking any steps that need to be applied to the logical plan, | ||
| * before this is being optimized. | ||
| * <p> | ||
| * This is useful, especially if you need to execute some async tasks before the plan is optimized. | ||
| * </p> | ||
| */ | ||
| public class LogicalPlanPreOptimizer { | ||
|
|
||
| private final LogicalPreOptimizerContext preOptimizerContext; | ||
|
|
||
| public LogicalPlanPreOptimizer(LogicalPreOptimizerContext preOptimizerContext) { | ||
| this.preOptimizerContext = preOptimizerContext; | ||
| } | ||
|
|
||
| /** | ||
| * Pre-optimize a logical plan. | ||
| * | ||
| * @param plan the analyzed logical plan to pre-optimize | ||
| * @param listener the listener returning the pre-optimized plan when pre-optimization is complete | ||
| */ | ||
| public void preOptimize(LogicalPlan plan, ActionListener<LogicalPlan> listener) { | ||
| if (plan.analyzed() == false) { | ||
| listener.onFailure(new IllegalStateException("Expected analyzed plan")); | ||
| return; | ||
| } | ||
|
|
||
| doPreOptimize(plan, listener.delegateFailureAndWrap((l, preOptimized) -> { | ||
| preOptimized.setPreOptimized(); | ||
| listener.onResponse(preOptimized); | ||
| })); | ||
| } | ||
|
|
||
| private void doPreOptimize(LogicalPlan plan, ActionListener<LogicalPlan> listener) { | ||
| // this is where we will be executing async tasks | ||
| listener.onResponse(plan); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPreOptimizerContext.java
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,43 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer; | ||
|
|
||
| import org.elasticsearch.xpack.esql.core.expression.FoldContext; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class LogicalPreOptimizerContext { | ||
|
|
||
| private final FoldContext foldCtx; | ||
|
|
||
| public LogicalPreOptimizerContext(FoldContext foldCtx) { | ||
| this.foldCtx = foldCtx; | ||
| } | ||
|
|
||
| public FoldContext foldCtx() { | ||
| return foldCtx; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) return true; | ||
| if (obj == null || obj.getClass() != this.getClass()) return false; | ||
| var that = (LogicalPreOptimizerContext) obj; | ||
| return this.foldCtx.equals(that.foldCtx); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(foldCtx); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LogicalPreOptimizerContext[foldCtx=" + foldCtx + ']'; | ||
| } | ||
| } |
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
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
110 changes: 110 additions & 0 deletions
110
...ql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanPreOptimizerTests.java
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,110 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer; | ||
|
|
||
| import org.apache.lucene.util.SetOnce; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.esql.EsqlTestUtils; | ||
| import org.elasticsearch.xpack.esql.core.expression.Alias; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.FoldContext; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.expression.function.scalar.string.Concat; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Add; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Eval; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Filter; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Limit; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Project; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.xpack.esql.EsqlTestUtils.as; | ||
| import static org.elasticsearch.xpack.esql.EsqlTestUtils.fieldAttribute; | ||
| import static org.elasticsearch.xpack.esql.EsqlTestUtils.of; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| public class LogicalPlanPreOptimizerTests extends ESTestCase { | ||
|
|
||
| public void testPlanIsMarkedAsPreOptimized() throws Exception { | ||
| for (int round = 0; round < 100; round++) { | ||
| // We want to make sure that the pre-optimizer woks for a wide range of plans | ||
| preOptimizedPlan(randomPlan()); | ||
| } | ||
| } | ||
|
|
||
| public void testPreOptimizeFailsIfPlanIsNotAnalyzed() throws Exception { | ||
| LogicalPlan plan = EsqlTestUtils.relation(); | ||
| SetOnce<Exception> exceptionHolder = new SetOnce<>(); | ||
|
|
||
| preOptimizer().preOptimize(plan, ActionListener.wrap(r -> fail("Should have failed"), exceptionHolder::set)); | ||
| assertBusy(() -> { | ||
| assertThat(exceptionHolder.get(), notNullValue()); | ||
| IllegalStateException e = as(exceptionHolder.get(), IllegalStateException.class); | ||
| assertThat(e.getMessage(), equalTo("Expected analyzed plan")); | ||
| }); | ||
| } | ||
|
|
||
| public LogicalPlan preOptimizedPlan(LogicalPlan plan) throws Exception { | ||
| // set plan as analyzed | ||
| plan.setPreOptimized(); | ||
|
|
||
| SetOnce<LogicalPlan> resultHolder = new SetOnce<>(); | ||
| SetOnce<Exception> exceptionHolder = new SetOnce<>(); | ||
|
|
||
| preOptimizer().preOptimize(plan, ActionListener.wrap(resultHolder::set, exceptionHolder::set)); | ||
|
|
||
| if (exceptionHolder.get() != null) { | ||
| throw exceptionHolder.get(); | ||
| } | ||
|
|
||
| assertThat(resultHolder.get(), notNullValue()); | ||
| assertThat(resultHolder.get().preOptimized(), equalTo(true)); | ||
|
|
||
| return resultHolder.get(); | ||
| } | ||
|
|
||
| private LogicalPlanPreOptimizer preOptimizer() { | ||
| LogicalPreOptimizerContext preOptimizerContext = new LogicalPreOptimizerContext(FoldContext.small()); | ||
| return new LogicalPlanPreOptimizer(preOptimizerContext); | ||
| } | ||
|
|
||
| private LogicalPlan randomPlan() { | ||
| LogicalPlan plan = EsqlTestUtils.relation(); | ||
| int numCommands = between(0, 100); | ||
|
|
||
| for (int i = 0; i < numCommands; i++) { | ||
| plan = switch (randomInt(3)) { | ||
| case 0 -> new Eval(Source.EMPTY, plan, List.of(new Alias(Source.EMPTY, randomIdentifier(), randomExpression()))); | ||
| case 1 -> new Limit(Source.EMPTY, of(randomInt()), plan); | ||
| case 2 -> new Filter(Source.EMPTY, plan, randomCondition()); | ||
| default -> new Project(Source.EMPTY, plan, List.of(new Alias(Source.EMPTY, randomIdentifier(), fieldAttribute()))); | ||
| }; | ||
| } | ||
| return plan; | ||
| } | ||
|
|
||
| private Expression randomExpression() { | ||
| return switch (randomInt(3)) { | ||
| case 0 -> of(randomInt()); | ||
| case 1 -> of(randomIdentifier()); | ||
| case 2 -> new Add(Source.EMPTY, of(randomInt()), of(randomDouble())); | ||
| default -> new Concat(Source.EMPTY, of(randomIdentifier()), randomList(1, 10, () -> of(randomIdentifier()))); | ||
| }; | ||
| } | ||
|
|
||
| private Expression randomCondition() { | ||
| if (randomBoolean()) { | ||
| return EsqlTestUtils.equalsOf(randomExpression(), randomExpression()); | ||
| } | ||
|
|
||
| return EsqlTestUtils.greaterThanOf(randomExpression(), randomExpression()); | ||
| } | ||
| } | ||
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.