-
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,105 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| package io.substrait.expression; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import io.substrait.TestBase; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import io.substrait.type.Type; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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(1), N.I64, FieldReference.StructField.of(1)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. Dereference a different field than the base's
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @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) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ImmutableFieldReference.Builder builder = | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. Nit: build these with the existing factories ( |
||||||||||||||||||||||||||||||||||||||||||||||||
| FieldReference.builder().type(type).addSegments(FieldReference.StructField.of(1)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| switch (scope) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| case EXPRESSION: | ||||||||||||||||||||||||||||||||||||||||||||||||
| builder.inputExpression( | ||||||||||||||||||||||||||||||||||||||||||||||||
| Expression.DynamicParameter.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .type(R.struct(R.BOOLEAN, type)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .parameterReference(0) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .build()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| case OUTER_STEPS: | ||||||||||||||||||||||||||||||||||||||||||||||||
| builder.outerReferenceStepsOut(2); | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| case OUTER_ANCHOR: | ||||||||||||||||||||||||||||||||||||||||||||||||
| builder.outerReferenceRelReference(7); | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| case LAMBDA_CURRENT: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. A dereferenced lambda parameter now writes a nested |
||||||||||||||||||||||||||||||||||||||||||||||||
| builder.lambdaParameterReferenceStepsOut(0); | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| case LAMBDA_OUTER: | ||||||||||||||||||||||||||||||||||||||||||||||||
| builder.lambdaParameterReferenceStepsOut(2); | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| case ROOT: | ||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Unexpected reference scope: " + scope); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return builder.build(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private void assertDereference( | ||||||||||||||||||||||||||||||||||||||||||||||||
| FieldReference original, | ||||||||||||||||||||||||||||||||||||||||||||||||
| FieldReference dereferenced, | ||||||||||||||||||||||||||||||||||||||||||||||||
| Type expectedType, | ||||||||||||||||||||||||||||||||||||||||||||||||
| FieldReference.ReferenceSegment nextSegment) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals(expectedType, dereferenced.getType()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals(List.of(nextSegment, original.segments().get(0)), dereferenced.segments()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals(original.inputExpression(), dereferenced.inputExpression()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals(original.outerReferenceStepsOut(), dereferenced.outerReferenceStepsOut()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals(original.outerReferenceRelReference(), dereferenced.outerReferenceRelReference()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assertEquals( | ||||||||||||||||||||||||||||||||||||||||||||||||
| original.lambdaParameterReferenceStepsOut(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| dereferenced.lambdaParameterReferenceStepsOut()); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| 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()); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+103
Member
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. Compare the whole object instead of listing attributes by hand, so a newly added attribute that
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.substrait.isthmus; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.substrait.isthmus.sql.SubstraitCreateStatementParser; | ||
| import io.substrait.plan.PlanProtoConverter; | ||
| import io.substrait.proto.Expression; | ||
| import io.substrait.proto.FilterRel; | ||
| import io.substrait.proto.Plan; | ||
| import org.apache.calcite.prepare.Prepare; | ||
| import org.apache.calcite.sql.parser.SqlParseException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CorrelatedNestedFieldTest { | ||
|
Member
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. Nit: move this into |
||
|
|
||
| @Test | ||
| void nestedOuterFieldKeepsItsCorrelationAnchor() throws SqlParseException { | ||
| Prepare.CatalogReader catalog = | ||
| SubstraitCreateStatementParser.processCreateStatementsToCatalog( | ||
| "CREATE TABLE outer_table (id INTEGER NOT NULL, s ROW(v INTEGER NOT NULL) NOT NULL);" | ||
| + "CREATE TABLE inner_table (id INTEGER NOT NULL, s ROW(v INTEGER NOT NULL) NOT NULL)"); | ||
| Plan plan = | ||
| new PlanProtoConverter() | ||
| .toProto( | ||
| new SqlToSubstrait() | ||
| .convert( | ||
| "SELECT o.id FROM outer_table o WHERE EXISTS" | ||
| + " (SELECT 1 FROM inner_table i WHERE i.id = o.s.v)", | ||
| catalog)); | ||
|
|
||
| FilterRel outerFilter = | ||
| plan.getRelations(0).getRoot().getInput().getProject().getInput().getFilter(); | ||
| FilterRel innerFilter = | ||
| outerFilter.getCondition().getSubquery().getSetPredicate().getTuples().getFilter(); | ||
| Expression.FieldReference outerField = | ||
| innerFilter.getCondition().getScalarFunction().getArguments(1).getValue().getSelection(); | ||
|
|
||
| assertTrue(outerFilter.getInput().getRead().getCommon().hasRelAnchor()); | ||
| assertTrue(outerField.hasOuterReference()); | ||
| assertTrue(outerField.getOuterReference().hasRelReference()); | ||
| assertEquals( | ||
| outerFilter.getInput().getRead().getCommon().getRelAnchor(), | ||
| outerField.getOuterReference().getRelReference()); | ||
| assertEquals(1, outerField.getDirectReference().getStructField().getField()); | ||
| assertTrue(outerField.getDirectReference().getStructField().hasChild()); | ||
| assertEquals( | ||
| 0, outerField.getDirectReference().getStructField().getChild().getStructField().getField()); | ||
| assertTrue( | ||
| innerFilter | ||
| .getCondition() | ||
| .getScalarFunction() | ||
| .getArguments(0) | ||
| .getValue() | ||
| .getSelection() | ||
| .hasRootReference()); | ||
| } | ||
| } | ||
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.