Skip to content
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

bug: fix vist of nested struct #150

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public FieldReference dereferenceStruct(int index) {
private FieldReference dereference(Type newType, ReferenceSegment nextSegment) {
return ImmutableFieldReference.builder()
.type(newType)
.addAllSegments(segments())
.addSegments(nextSegment)
.addAllSegments(segments())
.inputExpression(inputExpression())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public Expression visit(io.substrait.expression.Expression.MultiOrList expr)

@Override
public Expression visit(FieldReference expr) {
Expression.ReferenceSegment top = null;

Expression.ReferenceSegment seg = null;
for (var segment : expr.segments()) {
Expression.ReferenceSegment.Builder protoSegment;
Expand All @@ -351,13 +351,11 @@ public Expression visit(FieldReference expr) {
throw new IllegalArgumentException("Unhandled type: " + segment);
}
var builtSegment = protoSegment.build();
if (top == null) {
top = builtSegment;
}
seg = builtSegment;
}

var out = Expression.FieldReference.newBuilder().setDirectReference(top);
var out = Expression.FieldReference.newBuilder().setDirectReference(seg);

if (expr.inputExpression().isPresent()) {
out.setExpression(from(expr.inputExpression().get()));
} else if (expr.outerReferenceStepsOut().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.substrait.type.Type;
import io.substrait.type.proto.ProtoTypeConverter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -67,7 +68,7 @@ public FieldReference from(io.substrait.proto.Expression.FieldReference referenc
"Unhandled type: " + segment.getReferenceTypeCase());
});
}

Collections.reverse(segments);
var fieldReference =
switch (reference.getRootTypeCase()) {
case EXPRESSION -> FieldReference.ofExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.util.stream.Collectors;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rex.*;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;

public class RexExpressionConverter implements RexVisitor<Expression> {

static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(RexExpressionConverter.class);

Expand Down Expand Up @@ -119,16 +121,28 @@ public Expression visitRangeRef(RexRangeRef rangeRef) {

@Override
public Expression visitFieldAccess(RexFieldAccess fieldAccess) {
if (fieldAccess.getReferenceExpr() instanceof RexCorrelVariable) {
int stepsOut = relVisitor.getFieldAccessDepth(fieldAccess);

return FieldReference.newRootStructOuterReference(
fieldAccess.getField().getIndex(),
typeConverter.toSubstrait(fieldAccess.getType()),
stepsOut);
SqlKind kind = fieldAccess.getReferenceExpr().getKind();
switch (kind) {
case CORREL_VARIABLE -> {
int stepsOut = relVisitor.getFieldAccessDepth(fieldAccess);

return FieldReference.newRootStructOuterReference(
fieldAccess.getField().getIndex(),
typeConverter.toSubstrait(fieldAccess.getType()),
stepsOut);
}
case ITEM, INPUT_REF, FIELD_ACCESS -> {
Expression expression = fieldAccess.getReferenceExpr().accept(this);
if (expression instanceof FieldReference) {
FieldReference nestedReference = (FieldReference) expression;
return nestedReference.dereferenceStruct(fieldAccess.getField().getIndex());
} else {
return FieldReference.newStructReference(fieldAccess.getField().getIndex(), expression);
}
}
default -> throw new UnsupportedOperationException(
String.format("RexFieldAccess for SqlKind %s not supported", kind));
}
throw new UnsupportedOperationException(
"RexFieldAccess for other than RexCorrelVariable not supported");
}

@Override
Expand Down
Loading