-
Notifications
You must be signed in to change notification settings - Fork 25.8k
ESQL: Handle right hand side of Inline Stats coming optimized with LocalRelation shortcut #135011
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
Changes from all commits
0f4cb80
0fc78fb
c7204f0
e05190c
836235f
109e856
912b968
6b4952c
135acf9
4c41f21
ba52bd0
da6f6cb
b57f4dd
3e64b2f
8f90299
325ad7b
e877281
7518784
0c63ba9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 135011 | ||
| summary: Handle right hand side of Inline Stats coming optimized with `LocalRelation` | ||
| shortcut | ||
| area: ES|QL | ||
| type: bug | ||
| issues: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * 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.rules; | ||
|
|
||
| import org.elasticsearch.xpack.esql.optimizer.rules.logical.OptimizerRules; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
| import org.elasticsearch.xpack.esql.plan.logical.join.InlineJoin; | ||
| import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation; | ||
|
|
||
| public final class PruneInlineJoinOnEmptyRightSide extends OptimizerRules.OptimizerRule<InlineJoin> { | ||
|
|
||
| @Override | ||
| protected LogicalPlan rule(InlineJoin plan) { | ||
| return plan.right() instanceof LocalRelation lr ? InlineJoin.inlineData(plan, lr) : plan; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputAttributes; | ||
| import static org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes.LEFT; | ||
|
|
@@ -142,15 +143,29 @@ public record LogicalPlanTuple(LogicalPlan stubReplacedSubPlan, LogicalPlan orig | |
| * \_Limit[1000[INTEGER],false] | ||
| * \_LocalRelation[[x{r}#99],[IntVectorBlock[vector=ConstantIntVector[positions=1, value=1]]]] | ||
| */ | ||
| public static LogicalPlanTuple firstSubPlan(LogicalPlan optimizedPlan) { | ||
| public static LogicalPlanTuple firstSubPlan(LogicalPlan optimizedPlan, Set<LocalRelation> subPlansResults) { | ||
| Holder<LogicalPlanTuple> subPlan = new Holder<>(); | ||
| // Collect the first inlinejoin (bottom up in the tree) | ||
| optimizedPlan.forEachUp(InlineJoin.class, ij -> { | ||
| // extract the right side of the plan and replace its source | ||
| if (subPlan.get() == null && ij.right().anyMatch(p -> p instanceof StubRelation)) { | ||
| var p = replaceStub(ij.left(), ij.right()); | ||
| p.setOptimized(); | ||
| subPlan.set(new LogicalPlanTuple(p, ij.right())); | ||
| if (subPlan.get() == null) { | ||
| if (ij.right().anyMatch(p -> p instanceof StubRelation)) { | ||
| var p = replaceStub(ij.left(), ij.right()); | ||
| p.setOptimized(); | ||
| subPlan.set(new LogicalPlanTuple(p, ij.right())); | ||
| } else if (ij.right() instanceof LocalRelation relation | ||
| && (subPlansResults.isEmpty() || subPlansResults.contains(relation) == false) | ||
| || ij.right() instanceof LocalRelation == false && ij.right().anyMatch(p -> p instanceof LocalRelation)) { | ||
|
Comment on lines
+156
to
+158
Contributor
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. Might be worth extracting this check into a method, it's a bit hard to read. |
||
| // In case the plan was optimized further and the StubRelation was replaced with a LocalRelation | ||
| // or the right hand side became a LocalRelation alltogether, there is no need to replace the source of the | ||
| // right-hand side anymore. | ||
| var p = ij.right(); | ||
| p.setOptimized(); | ||
| subPlan.set(new LogicalPlanTuple(p, ij.right())); | ||
|
Comment on lines
+162
to
+164
Contributor
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. Don't know if it'd be worth creating a light static method to do this here and the similar thing above. |
||
| // TODO: INLINE STATS this is essentially an optimization similar to the one in PruneInlineJoinOnEmptyRightSide | ||
| // this further supports the idea of running the optimization step again after the substitutions (see EsqlSession | ||
| // executeSubPlan() method where we could run the optimizer after the results are replaced in place). | ||
| } | ||
| } | ||
| }); | ||
| return subPlan.get(); | ||
|
|
||
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.
Unrelated to the meat of the PR