-
Notifications
You must be signed in to change notification settings - Fork 25.2k
ESQL: Add optimization to purge join on null merge key #127583
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
Open
bpintea
wants to merge
5
commits into
elastic:main
Choose a base branch
from
bpintea:enh/drop_join_on_null_merge_key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1605c8f
Add optimization to purge join on null merge key
bpintea d0ed566
restrict join types the rule affects
bpintea 43922e8
Update docs/changelog/127583.yaml
bpintea 8a484b8
stabilize tests
bpintea c82823f
Merge branch 'main' into enh/drop_join_on_null_merge_key
bpintea 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: 127583 | ||
summary: Add optimization to purge join on null merge key | ||
area: ES|QL | ||
type: enhancement | ||
issues: | ||
- 125577 |
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
80 changes: 80 additions & 0 deletions
80
.../elasticsearch/xpack/esql/optimizer/rules/logical/local/PruneJoinOnNullMatchingField.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,80 @@ | ||
/* | ||
* 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.logical.local; | ||
|
||
import org.elasticsearch.xpack.esql.core.expression.Alias; | ||
import org.elasticsearch.xpack.esql.core.expression.AttributeMap; | ||
import org.elasticsearch.xpack.esql.core.expression.AttributeSet; | ||
import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
import org.elasticsearch.xpack.esql.core.expression.Expressions; | ||
import org.elasticsearch.xpack.esql.core.expression.Literal; | ||
import org.elasticsearch.xpack.esql.optimizer.rules.logical.OptimizerRules; | ||
import org.elasticsearch.xpack.esql.plan.logical.Eval; | ||
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.esql.plan.logical.Project; | ||
import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan; | ||
import org.elasticsearch.xpack.esql.plan.logical.join.Join; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.xpack.esql.core.expression.Expressions.isGuaranteedNull; | ||
import static org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes.INNER; | ||
import static org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes.LEFT; | ||
|
||
/** | ||
* The rule matches a plan pattern having a Join on top of a Project and/or Eval. It then checks if the join's performed on a field which | ||
* is aliased to null (in type or value); if that's the case, it prunes the join, replacing it with an Eval - returning aliases to null | ||
* for all the fields added in by the right side of the Join - plus a Project on top of it. | ||
* The rule can apply on the coordinator already, but it's more likely to be effective on the data nodes, where null aliasing is inserted | ||
* due to locally missing fields. This rule relies on that behavior -- see {@link ReplaceMissingFieldWithNull}. | ||
*/ | ||
public class PruneJoinOnNullMatchingField extends OptimizerRules.OptimizerRule<Join> { | ||
|
||
@Override | ||
protected LogicalPlan rule(Join join) { | ||
LogicalPlan plan = join; | ||
var joinType = join.config().type(); | ||
if (joinType == INNER || joinType == LEFT) { // other types will have different replacement logic | ||
AttributeMap.Builder<Expression> attributeMapBuilder = AttributeMap.builder(); | ||
loop: for (var child = join.left();; child = ((UnaryPlan) child).child()) { // cast is safe as both plans are UnaryPlans | ||
switch (child) { | ||
case Project project -> project.projections().forEach(projection -> { | ||
if (projection instanceof Alias alias) { | ||
attributeMapBuilder.put(alias.toAttribute(), alias.child()); | ||
} | ||
}); | ||
case Eval eval -> eval.fields().forEach(alias -> attributeMapBuilder.put(alias.toAttribute(), alias.child())); | ||
default -> { | ||
break loop; | ||
} | ||
} | ||
} | ||
for (var attr : AttributeSet.of(join.config().matchFields())) { | ||
var resolved = attributeMapBuilder.build().resolve(attr); | ||
if (resolved != null && isGuaranteedNull(resolved)) { | ||
plan = replaceJoin(join); | ||
break; | ||
} | ||
} | ||
} | ||
return plan; | ||
} | ||
|
||
private static LogicalPlan replaceJoin(Join join) { | ||
var joinRightOutput = join.rightOutputFields(); | ||
if (joinRightOutput.isEmpty()) { // can be empty when the join key is null and the other right side entries pruned (by an agg) | ||
return join.left(); | ||
} | ||
List<Alias> aliases = new ArrayList<>(joinRightOutput.size()); | ||
// TODO: cache aliases by type, à la ReplaceMissingFieldWithNull#missingToNull (tho lookup indices won't have Ks of fields) | ||
joinRightOutput.forEach(a -> aliases.add(new Alias(a.source(), a.name(), Literal.of(a, null), a.id()))); | ||
var eval = new Eval(join.source(), join.left(), aliases); | ||
return new Project(join.source(), eval, join.computeOutput(join.left().output(), Expressions.asAttributes(aliases))); | ||
} | ||
} |
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
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.
Not strictly related, but not sure why we wouldn't include the id, it's easier to track which exactly reference points to an alias.