-
Notifications
You must be signed in to change notification settings - Fork 96
fix(core): retain reference scope during dereference #1268
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
bvolpato
wants to merge
2
commits into
substrait-io:main
Choose a base branch
from
bvolpato:bvolpato/fix-correlated-dereference
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
87 changes: 87 additions & 0 deletions
87
core/src/test/java/io/substrait/expression/FieldReferenceDereferenceTest.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,87 @@ | ||
| package io.substrait.expression; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import io.substrait.TestBase; | ||
| import io.substrait.type.Type; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
|
|
||
| class FieldReferenceDereferenceTest extends TestBase { | ||
|
|
||
| enum ReferenceScope { | ||
| ROOT, | ||
| EXPRESSION, | ||
| OUTER_STEPS, | ||
| OUTER_ANCHOR, | ||
| LAMBDA_CURRENT, | ||
| LAMBDA_OUTER | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(ReferenceScope.class) | ||
| void structDereferencePreservesScope(ReferenceScope scope) { | ||
| FieldReference reference = reference(scope, R.struct(R.BOOLEAN, N.I64)); | ||
|
|
||
| assertDereference( | ||
| reference, reference.dereferenceStruct(0), R.BOOLEAN, FieldReference.StructField.of(0)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(ReferenceScope.class) | ||
| void listDereferencePreservesScope(ReferenceScope scope) { | ||
| FieldReference reference = reference(scope, R.list(N.I64)); | ||
|
|
||
| assertDereference( | ||
| reference, reference.dereferenceList(2), N.I64, FieldReference.ListElement.of(2)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(ReferenceScope.class) | ||
| void mapDereferencePreservesScope(ReferenceScope scope) { | ||
| FieldReference reference = reference(scope, R.map(R.STRING, N.I64)); | ||
| Expression.Literal key = ExpressionCreator.string(false, "key"); | ||
|
|
||
| assertDereference( | ||
| reference, reference.dereferenceMap(key), N.I64, FieldReference.MapKey.of(key)); | ||
| } | ||
|
|
||
| private FieldReference reference(ReferenceScope scope, Type type) { | ||
| return switch (scope) { | ||
| case ROOT -> FieldReference.newRootStructReference(1, type); | ||
| case EXPRESSION -> | ||
| FieldReference.newStructReference( | ||
| 1, | ||
| Expression.DynamicParameter.builder() | ||
| .type(R.struct(R.BOOLEAN, type)) | ||
| .parameterReference(0) | ||
| .build()); | ||
| case OUTER_STEPS -> FieldReference.newRootStructOuterReference(1, type, 2); | ||
| case OUTER_ANCHOR -> FieldReference.newRootStructOuterReferenceByRelReference(1, type, 7); | ||
| case LAMBDA_CURRENT -> FieldReference.newLambdaParameterReference(0, 1, type); | ||
| case LAMBDA_OUTER -> FieldReference.newLambdaParameterReference(2, 1, type); | ||
| }; | ||
| } | ||
|
|
||
| private void assertDereference( | ||
| FieldReference original, | ||
| FieldReference dereferenced, | ||
| Type expectedType, | ||
| FieldReference.ReferenceSegment nextSegment) { | ||
| assertEquals( | ||
| ImmutableFieldReference.copyOf(original) | ||
| .withType(expectedType) | ||
| .withSegments(nextSegment, original.segments().get(0)), | ||
| dereferenced); | ||
|
|
||
| io.substrait.proto.Expression.FieldReference originalProto = | ||
| expressionProtoConverter.toProto(original).getSelection(); | ||
| io.substrait.proto.Expression.FieldReference dereferencedProto = | ||
| expressionProtoConverter.toProto(dereferenced).getSelection(); | ||
| assertEquals(originalProto.getRootTypeCase(), dereferencedProto.getRootTypeCase()); | ||
| assertEquals(originalProto.getOuterReference(), dereferencedProto.getOuterReference()); | ||
| assertEquals( | ||
| originalProto.getLambdaParameterReference(), | ||
| dereferencedProto.getLambdaParameterReference()); | ||
| } | ||
| } |
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
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.
Reject nested scoped paths in these readers, or handle them. Before this change they threw on these references; now they return a wrong result. These lines are outside the diff, so I couldn't attach suggestions:
ExpressionRexConverter.java:834(outer) and:863(lambda) takesegments().get(0), which is the innermost step. The new isthmus query converts back to Calcite asi.id = $cor0.ID, and a lambdax.f1becomes(p0, p1) -> p1. Throwing whensegments().size() > 1covers both.ProtoExpressionConverter.java:94reads only the topstruct_fieldand drops itschild, so the exported plan reads back asi.id = o.s(INTEGER vs ROW). The lambda case at:119already throws onhasChild(); do the same here.An
assertProtoPlanRoundripon the new isthmus query would have caught the second one.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.
Thanks for tracing the consumer paths. The local patch adds guards in both readers and regressions for nested outer and lambda references, including matching-type fields that previously selected the wrong field without an error. Unsupported paths now fail explicitly. The full build passes, and the update is not pushed yet.