use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId in project drill by apache.
the class ComplexUnnestVisitor method visit.
public RelNode visit(Uncollect uncollect) {
RelBuilder builder = DrillRelFactories.LOGICAL_BUILDER.create(uncollect.getCluster(), null);
RexBuilder rexBuilder = builder.getRexBuilder();
assert uncollect.getInput() instanceof Project : "Uncollect should have Project input";
Project project = (Project) uncollect.getInput();
// If project below uncollect contains only field references, no need to rewrite it
List<RexNode> projectChildExps = project.getChildExps();
assert projectChildExps.size() == 1 : "Uncollect does not support multiple expressions";
RexNode projectExpr = projectChildExps.iterator().next();
if (projectExpr.getKind() == SqlKind.FIELD_ACCESS) {
return uncollect;
}
// Collects CorrelationId instances used in current rel node
RelOptUtil.VariableUsedVisitor variableUsedVisitor = new RelOptUtil.VariableUsedVisitor(null);
project.accept(variableUsedVisitor);
assert variableUsedVisitor.variables.size() == 1 : "Uncollect supports only single correlated reference";
CorrelationId oldCorrId = variableUsedVisitor.variables.iterator().next();
RelNode left = leftInputs.get(oldCorrId);
// Creates new project to be placed on top of the left input of correlate
List<RexNode> leftProjExprs = new ArrayList<>();
List<String> fieldNames = new ArrayList<>();
for (RelDataTypeField field : left.getRowType().getFieldList()) {
leftProjExprs.add(rexBuilder.makeInputRef(left, field.getIndex()));
fieldNames.add(field.getName());
}
fieldNames.add(COMPLEX_FIELD_NAME);
builder.push(left);
// Adds complex expression with replaced correlation
// to the projected list from the left
leftProjExprs.add(new RexFieldAccessReplacer(builder).apply(projectExpr));
RelNode leftProject = builder.project(leftProjExprs, fieldNames).build();
leftInputs.put(oldCorrId, leftProject);
builder.push(project.getInput());
CorrelationId newCorrId = uncollect.getCluster().createCorrel();
// stores new CorrelationId to be used during the creation of new Correlate
updatedCorrelationIds.put(oldCorrId, newCorrId);
RexNode rexCorrel = rexBuilder.makeCorrel(leftProject.getRowType(), newCorrId);
// constructs Project below Uncollect with updated RexCorrelVariable
builder.project(ImmutableList.of(rexBuilder.makeFieldAccess(rexCorrel, leftProjExprs.size() - 1)), ImmutableList.of(COMPLEX_FIELD_NAME));
return uncollect.copy(uncollect.getTraitSet(), builder.build());
}
Aggregations