-
Notifications
You must be signed in to change notification settings - Fork 74
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
feat: add NestedLoopJoin rel #188
Merged
vbarua
merged 11 commits into
substrait-io:main
from
danepitkin:danepitkin/nestedloopjoin
Nov 3, 2023
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cea494f
feat: add NestedLoopJoin rel
dc5c1fe
fix: record type for anti, semi joins
90e62d9
fix: move nlj operator into physical subfolder
f7cec8c
feat: add test
3ba7fc0
fix: improve tests
bb82de7
fix: improve test case further
d75f2a2
fix: use camelCase
53d1021
feat: make expression required
47aa9d8
fix: use equality expression in test case
476a3e1
fix: remove debug output and fix indices
9f6c6ee
fix: use list of rels as input to equal expression
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 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 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 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 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 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 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
79 changes: 79 additions & 0 deletions
79
core/src/main/java/io/substrait/relation/physical/NestedLoopJoin.java
This file contains 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,79 @@ | ||
package io.substrait.relation.physical; | ||
|
||
import io.substrait.expression.Expression; | ||
import io.substrait.proto.NestedLoopJoinRel; | ||
import io.substrait.relation.BiRel; | ||
import io.substrait.relation.HasExtension; | ||
import io.substrait.relation.RelVisitor; | ||
import io.substrait.type.Type; | ||
import io.substrait.type.TypeCreator; | ||
import java.util.stream.Stream; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
public abstract class NestedLoopJoin extends BiRel implements HasExtension { | ||
|
||
public abstract Expression getCondition(); | ||
|
||
public abstract JoinType getJoinType(); | ||
|
||
public static enum JoinType { | ||
UNKNOWN(NestedLoopJoinRel.JoinType.JOIN_TYPE_UNSPECIFIED), | ||
INNER(NestedLoopJoinRel.JoinType.JOIN_TYPE_INNER), | ||
OUTER(NestedLoopJoinRel.JoinType.JOIN_TYPE_OUTER), | ||
LEFT(NestedLoopJoinRel.JoinType.JOIN_TYPE_LEFT), | ||
RIGHT(NestedLoopJoinRel.JoinType.JOIN_TYPE_RIGHT), | ||
LEFT_SEMI(NestedLoopJoinRel.JoinType.JOIN_TYPE_LEFT_SEMI), | ||
RIGHT_SEMI(NestedLoopJoinRel.JoinType.JOIN_TYPE_RIGHT_SEMI), | ||
LEFT_ANTI(NestedLoopJoinRel.JoinType.JOIN_TYPE_LEFT_ANTI), | ||
RIGHT_ANTI(NestedLoopJoinRel.JoinType.JOIN_TYPE_RIGHT_ANTI); | ||
|
||
private NestedLoopJoinRel.JoinType proto; | ||
|
||
JoinType(NestedLoopJoinRel.JoinType proto) { | ||
this.proto = proto; | ||
} | ||
|
||
public NestedLoopJoinRel.JoinType toProto() { | ||
return proto; | ||
} | ||
|
||
public static JoinType fromProto(NestedLoopJoinRel.JoinType proto) { | ||
for (var v : values()) { | ||
if (v.proto == proto) { | ||
return v; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Unknown type: " + proto); | ||
} | ||
} | ||
|
||
@Override | ||
protected Type.Struct deriveRecordType() { | ||
Stream<Type> leftTypes = | ||
switch (getJoinType()) { | ||
case RIGHT, OUTER -> getLeft().getRecordType().fields().stream() | ||
.map(TypeCreator::asNullable); | ||
case RIGHT_ANTI, RIGHT_SEMI -> Stream.empty(); | ||
default -> getLeft().getRecordType().fields().stream(); | ||
}; | ||
Stream<Type> rightTypes = | ||
switch (getJoinType()) { | ||
case LEFT, OUTER -> getRight().getRecordType().fields().stream() | ||
.map(TypeCreator::asNullable); | ||
case LEFT_ANTI, LEFT_SEMI -> Stream.empty(); | ||
default -> getRight().getRecordType().fields().stream(); | ||
}; | ||
return TypeCreator.REQUIRED.struct(Stream.concat(leftTypes, rightTypes)); | ||
} | ||
|
||
@Override | ||
public <O, E extends Exception> O accept(RelVisitor<O, E> visitor) throws E { | ||
return visitor.visit(this); | ||
} | ||
|
||
public static ImmutableNestedLoopJoin.Builder builder() { | ||
return ImmutableNestedLoopJoin.builder(); | ||
} | ||
} |
This file contains 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 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.
Ah! This is subtle and weird (and something to improve in the builder actually).
The condition on the join is evaluated relative to the joins record shape, which is the union of the left and right table records. It would look like:
b.fieldReference(rightTable, 2)
here ends up referring toc
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.
Ah that makes sense! Thank you
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.
I'm realising there's nothing else in the code that uses this, which is why it hasn't come up before.
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.
For the test case, should I create a unioned named table to refer to?
5
is out of bounds when creating the Rel, but in bounds after the roundtripThere 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.
Hmm, we have
newInputRelReference(int index, List<Rel> rels)
, which might handle this. I'll dig into this more myself.