-
Notifications
You must be signed in to change notification settings - Fork 25.5k
ESQL: Enable pushing down LOOKUP JOIN past Project #127776
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e400761
Sketch solution
alex-spies 958659a
Sketch it out some more
alex-spies d1961d0
Start another approach
alex-spies d15f5e4
Implement the optimization and add a csv test
alex-spies f810bea
Update required capability for test
alex-spies 2e93947
Merge remote-tracking branch 'upstream/main' into pushdown-lu-join-pa…
alex-spies 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
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
184 changes: 184 additions & 0 deletions
184
...in/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/PushDownJoinPastProject.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,184 @@ | ||
/* | ||
* 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; | ||
|
||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.index.IndexMode; | ||
import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
import org.elasticsearch.xpack.esql.core.expression.AttributeSet; | ||
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.esql.core.expression.NameId; | ||
import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
import org.elasticsearch.xpack.esql.core.tree.Source; | ||
import org.elasticsearch.xpack.esql.plan.GeneratingPlan; | ||
import org.elasticsearch.xpack.esql.plan.logical.EsRelation; | ||
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 org.elasticsearch.xpack.esql.plan.logical.join.JoinType; | ||
import org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Pushing down {@link Join}s past {@link Project}s has the benefit that field extraction can happen later. Also, once bubbled downstream, | ||
* multiple projects can be combined which can eliminate some fields altogether (c.f. | ||
* {@link org.elasticsearch.xpack.esql.optimizer.rules.physical.local.InsertFieldExtraction}). Even just field extractions before joins are | ||
* expensive because joins create new rows in case of multiple matches, which means that the extracted columns need to be deeply copied | ||
* and blown up. | ||
* | ||
* This follows the same approach as {@link PushDownUtils#pushGeneratingPlanPastProjectAndOrderBy(UnaryPlan)}. To deal with name conflicts, | ||
* we rename the fields that a {@code LOOKUP JOIN} "generates" by renaming the {@link FieldAttribute}s in the join's right hand side | ||
* {@link EsRelation}. Once we have qualifiers, this can be simplified by just assigning temporary qualifiers and later stripping them away. | ||
*/ | ||
public final class PushDownJoinPastProject extends OptimizerRules.OptimizerRule<Join> { | ||
|
||
/** | ||
* Wrapper class for a {@link Join} representing a {@code LOOKUP JOIN}, so we can treat it as if it was a {@link UnaryPlan} that is also | ||
* a {@link GeneratingPlan}; | ||
*/ | ||
private class JoinAsUnaryGeneratingPlan extends UnaryPlan implements GeneratingPlan<JoinAsUnaryGeneratingPlan> { | ||
private final Join lookupJoin; | ||
private List<Attribute> lazyGeneratedAttributes; | ||
|
||
JoinAsUnaryGeneratingPlan(Join lookupJoin) { | ||
super(lookupJoin.source(), lookupJoin.left()); | ||
this.lookupJoin = lookupJoin; | ||
} | ||
|
||
private JoinAsUnaryGeneratingPlan( | ||
Source source, | ||
LogicalPlan left, | ||
LogicalPlan right, | ||
JoinType type, | ||
List<Attribute> matchFields, | ||
List<Attribute> leftFields, | ||
List<Attribute> rightFields | ||
) { | ||
this(new Join(source, left, right, type, matchFields, leftFields, rightFields)); | ||
} | ||
|
||
Join unwrap() { | ||
return lookupJoin; | ||
} | ||
|
||
@Override | ||
public UnaryPlan replaceChild(LogicalPlan newChild) { | ||
return new JoinAsUnaryGeneratingPlan(lookupJoin.replaceChildren(newChild, lookupJoin.right())); | ||
} | ||
|
||
@Override | ||
public boolean expressionsResolved() { | ||
return lookupJoin.expressionsResolved(); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<? extends LogicalPlan> info() { | ||
return NodeInfo.create( | ||
this, | ||
JoinAsUnaryGeneratingPlan::new, | ||
lookupJoin.left(), | ||
lookupJoin.right(), | ||
lookupJoin.config().type(), | ||
lookupJoin.config().matchFields(), | ||
lookupJoin.config().leftFields(), | ||
lookupJoin.config().rightFields() | ||
); | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
throw new UnsupportedOperationException("lives only for a single optimizer rule application"); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
throw new UnsupportedOperationException("lives only for a single optimizer rule application"); | ||
} | ||
|
||
@Override | ||
public List<Attribute> generatedAttributes() { | ||
if (lazyGeneratedAttributes == null) { | ||
lazyGeneratedAttributes = lookupJoin.rightOutputFields(); | ||
} | ||
return lazyGeneratedAttributes; | ||
} | ||
|
||
@Override | ||
public JoinAsUnaryGeneratingPlan withGeneratedNames(List<String> newNames) { | ||
checkNumberOfNewNames(newNames); | ||
|
||
if (lookupJoin.right() instanceof EsRelation esRelation) { | ||
AttributeSet generatedSet = AttributeSet.of(generatedAttributes()); | ||
int numOutputAttributes = esRelation.output().size(); | ||
List<Attribute> newAttributes = new ArrayList<>(numOutputAttributes); | ||
// The match field from the LOOKUP JOIN's right hand side EsRelation is not added to the output from the left hand side. | ||
// It's not part of the "generated" attributes and needs to be skipped. | ||
int newNamesIndex = 0; | ||
for (Attribute attr : esRelation.output()) { | ||
if (generatedSet.contains(attr)) { | ||
String newName = newNames.get(newNamesIndex++); | ||
if (newName.equals(attr.name())) { | ||
newAttributes.add(attr); | ||
} else { | ||
newAttributes.add(attr.withName(newName).withId(new NameId())); | ||
} | ||
} else { | ||
newAttributes.add(attr); | ||
} | ||
} | ||
|
||
assert newAttributes.size() == numOutputAttributes; | ||
return new JoinAsUnaryGeneratingPlan( | ||
lookupJoin.replaceChildren(lookupJoin.left(), esRelation.withAttributes(newAttributes)) | ||
); | ||
} | ||
throw new IllegalStateException( | ||
"right hand side of LOOKUP JOIN must be a relation, found [" + lookupJoin.right().getClass() + "]" | ||
); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return lookupJoin.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
JoinAsUnaryGeneratingPlan other = (JoinAsUnaryGeneratingPlan) obj; | ||
|
||
return lookupJoin.equals(other.lookupJoin); | ||
} | ||
} | ||
|
||
@Override | ||
protected LogicalPlan rule(Join join) { | ||
if (join.left() instanceof Project projectChild | ||
&& JoinTypes.LEFT.equals(join.config().type()) | ||
&& join.right() instanceof EsRelation lookupIndex | ||
&& lookupIndex.indexMode() == IndexMode.LOOKUP) { | ||
|
||
var joinAsGeneratingUnary = new JoinAsUnaryGeneratingPlan(join); | ||
var pushedDown = PushDownUtils.pushGeneratingPlanPastProjectAndOrderBy(joinAsGeneratingUnary); | ||
|
||
return pushedDown.transformDown(JoinAsUnaryGeneratingPlan.class, JoinAsUnaryGeneratingPlan::unwrap); | ||
} | ||
|
||
return join; | ||
|
||
} | ||
} |
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 |
---|---|---|
|
@@ -718,7 +718,8 @@ private PhysicalOperation planLookupJoin(LookupJoinExec join, LocalExecutionPlan | |
private record MatchConfig(String fieldName, int channel, DataType type) { | ||
private MatchConfig(FieldAttribute match, Layout.ChannelAndType input) { | ||
// Note, this handles TEXT fields with KEYWORD subfields | ||
this(match.exactAttribute().name(), input.channel(), input.type()); | ||
// TODO: This probably also led to bugs for LOOKUP JOIN on a union typed field, let's add a test. | ||
this(match.exactAttribute().fieldName(), input.channel(), input.type()); | ||
Comment on lines
+721
to
+722
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. The diff touches multiple places that should have used field names but used attribute names, instead. To make this PR cleaner, I think we should have a separate PR just with these fixes + corresponding tests. This should also address #127521. |
||
} | ||
} | ||
|
||
|
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
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.
Needs a comment: alias and reference attribute cases only relevant for ENRICH