-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add checks that optimizers do not modify the layout #130855
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
julian-elastic
merged 14 commits into
elastic:main
from
julian-elastic:outputChecker_v2
Jul 18, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4d69b79
Verify the layout is not changed during optimizations
julian-elastic 6cefde2
Update docs/changelog/130855.yaml
julian-elastic f022d87
Update docs/changelog/130855.yaml
julian-elastic fcbe4bc
Address code review feedback part 1
julian-elastic 0270314
Address code review feedback part 2
julian-elastic 0404ab0
Merge remote-tracking branch 'origin/outputChecker_v2' into outputChe…
julian-elastic ce2ce1c
[CI] Auto commit changes from spotless
6f29114
Merge branch 'main' into outputChecker_v2
julian-elastic da96ebd
Fix more cases and address code review
julian-elastic 425a733
Merge branch 'main' into outputChecker_v2
julian-elastic b29fb22
Integrate with latest from main and fix merge errors
julian-elastic 52dba47
Fix a bug due to static counter
julian-elastic 9164afe
Address more code review comments
julian-elastic 0f82149
Fix merge error
julian-elastic 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,6 @@ | ||
| pr: 130855 | ||
| summary: Add checks that optimizers do not modify the layout | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: | ||
| - 125576 |
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
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
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
82 changes: 82 additions & 0 deletions
82
...c/main/java/org/elasticsearch/xpack/esql/optimizer/PostOptimizationPhasePlanVerifier.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,82 @@ | ||
| /* | ||
| * 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.common.Failures; | ||
| import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
| import org.elasticsearch.xpack.esql.optimizer.rules.physical.ProjectAwayColumns; | ||
| import org.elasticsearch.xpack.esql.plan.QueryPlan; | ||
| import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.index.IndexMode.LOOKUP; | ||
| import static org.elasticsearch.xpack.esql.common.Failure.fail; | ||
| import static org.elasticsearch.xpack.esql.core.expression.Attribute.dataTypeEquals; | ||
|
|
||
| /** | ||
| * Verifies the plan after optimization. | ||
| * This is invoked immediately after a Plan Optimizer completes its work. | ||
| * Currently, it is called after LogicalPlanOptimizer, PhysicalPlanOptimizer, | ||
| * LocalLogicalPlanOptimizer, and LocalPhysicalPlanOptimizer. | ||
| * Note: Logical and Physical optimizers may override methods in this class to perform different checks. | ||
| */ | ||
| public abstract class PostOptimizationPhasePlanVerifier<P extends QueryPlan<P>> { | ||
|
|
||
| /** Verifies the optimized plan */ | ||
| public Failures verify(P optimizedPlan, boolean skipRemoteEnrichVerification, List<Attribute> expectedOutputAttributes) { | ||
| Failures failures = new Failures(); | ||
| Failures depFailures = new Failures(); | ||
| if (skipVerification(optimizedPlan, skipRemoteEnrichVerification)) { | ||
| return failures; | ||
| } | ||
|
|
||
| checkPlanConsistency(optimizedPlan, failures, depFailures); | ||
|
|
||
| verifyOutputNotChanged(optimizedPlan, expectedOutputAttributes, failures); | ||
|
|
||
| if (depFailures.hasFailures()) { | ||
| throw new IllegalStateException(depFailures.toString()); | ||
| } | ||
|
|
||
| return failures; | ||
| } | ||
|
|
||
| abstract boolean skipVerification(P optimizedPlan, boolean skipRemoteEnrichVerification); | ||
|
|
||
| abstract void checkPlanConsistency(P optimizedPlan, Failures failures, Failures depFailures); | ||
|
|
||
| private static void verifyOutputNotChanged(QueryPlan<?> optimizedPlan, List<Attribute> expectedOutputAttributes, Failures failures) { | ||
| if (dataTypeEquals(expectedOutputAttributes, optimizedPlan.output()) == false) { | ||
| // If the output level is empty we add a column called ProjectAwayColumns.ALL_FIELDS_PROJECTED | ||
| // We will ignore such cases for output verification | ||
| // TODO: this special casing is required due to https://github.com/elastic/elasticsearch/issues/121741, remove when fixed. | ||
| boolean hasProjectAwayColumns = optimizedPlan.output() | ||
| .stream() | ||
| .anyMatch(x -> x.name().equals(ProjectAwayColumns.ALL_FIELDS_PROJECTED)); | ||
julian-elastic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // LookupJoinExec represents the lookup index with EsSourceExec and this is turned into EsQueryExec by | ||
| // ReplaceSourceAttributes. Because InsertFieldExtractions doesn't apply to lookup indices, the | ||
| // right hand side will only have the EsQueryExec providing the _doc attribute and nothing else. | ||
| // We perform an optimizer run on every fragment. LookupJoinExec also contains such a fragment, | ||
| // and currently it only contains an EsQueryExec after optimization. | ||
| boolean hasLookupJoinExec = optimizedPlan instanceof EsQueryExec esQueryExec && esQueryExec.indexMode() == LOOKUP; | ||
| boolean ignoreError = hasProjectAwayColumns || hasLookupJoinExec; | ||
| if (ignoreError == false) { | ||
| failures.add( | ||
| fail( | ||
| optimizedPlan, | ||
| "Output has changed from [{}] to [{}]. ", | ||
| expectedOutputAttributes.toString(), | ||
| optimizedPlan.output().toString() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.