-
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 8 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,34 +12,35 @@ | |
| import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expressions; | ||
| import org.elasticsearch.xpack.esql.optimizer.rules.PlanConsistencyChecker; | ||
| import org.elasticsearch.xpack.esql.plan.QueryPlan; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Enrich; | ||
| import org.elasticsearch.xpack.esql.plan.physical.EnrichExec; | ||
| import org.elasticsearch.xpack.esql.plan.physical.FieldExtractExec; | ||
| import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan; | ||
|
|
||
| import static org.elasticsearch.xpack.esql.common.Failure.fail; | ||
|
|
||
| /** Physical plan verifier. */ | ||
| public final class PhysicalVerifier { | ||
| public final class PhysicalVerifier extends PostOptimizationPhasePlanVerifier { | ||
|
||
|
|
||
| public static final PhysicalVerifier INSTANCE = new PhysicalVerifier(); | ||
|
|
||
| private PhysicalVerifier() {} | ||
|
|
||
| /** Verifies the physical plan. */ | ||
| public Failures verify(PhysicalPlan plan, boolean skipRemoteEnrichVerification) { | ||
| Failures failures = new Failures(); | ||
| Failures depFailures = new Failures(); | ||
|
|
||
| @Override | ||
| boolean skipVerification(QueryPlan<?> optimizedPlan, boolean skipRemoteEnrichVerification) { | ||
| if (skipRemoteEnrichVerification) { | ||
| // AwaitsFix https://github.com/elastic/elasticsearch/issues/118531 | ||
| var enriches = plan.collectFirstChildren(EnrichExec.class::isInstance); | ||
| var enriches = optimizedPlan.collectFirstChildren(EnrichExec.class::isInstance); | ||
| if (enriches.isEmpty() == false && ((EnrichExec) enriches.get(0)).mode() == Enrich.Mode.REMOTE) { | ||
| return failures; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| plan.forEachDown(p -> { | ||
| @Override | ||
| void checkPlanConsistency(QueryPlan<?> optimizedPlan, Failures failures, Failures depFailures) { | ||
| optimizedPlan.forEachDown(p -> { | ||
| if (p instanceof FieldExtractExec fieldExtractExec) { | ||
| Attribute sourceAttribute = fieldExtractExec.sourceAttribute(); | ||
| if (sourceAttribute == null) { | ||
|
|
@@ -66,11 +67,5 @@ public Failures verify(PhysicalPlan plan, boolean skipRemoteEnrichVerification) | |
| }); | ||
| } | ||
| }); | ||
|
|
||
| if (depFailures.hasFailures()) { | ||
| throw new IllegalStateException(depFailures.toString()); | ||
| } | ||
|
|
||
| return failures; | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
...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,71 @@ | ||
| /* | ||
| * 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.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 { | ||
|
|
||
| /** Verifies the optimized plan */ | ||
| public Failures verify(QueryPlan<?> 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(QueryPlan<?> optimizedPlan, boolean skipRemoteEnrichVerification); | ||
|
|
||
| abstract void checkPlanConsistency(QueryPlan<?> optimizedPlan, Failures failures, Failures depFailures); | ||
|
|
||
| private static void verifyOutputNotChanged(QueryPlan<?> optimizedPlan, List<Attribute> expectedOutputAttributes, Failures failures) { | ||
| if (datatypeEquals(expectedOutputAttributes, optimizedPlan.output()) == false) { | ||
| // 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. | ||
| if ((optimizedPlan instanceof EsQueryExec esQueryExec && esQueryExec.indexMode() == LOOKUP) == false) { | ||
julian-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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
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.