Search in sources :

Example 31 with CorrelationId

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());
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelOptUtil(org.apache.calcite.plan.RelOptUtil) ArrayList(java.util.ArrayList) Project(org.apache.calcite.rel.core.Project) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

CorrelationId (org.apache.calcite.rel.core.CorrelationId)29 RelNode (org.apache.calcite.rel.RelNode)18 RexNode (org.apache.calcite.rex.RexNode)15 ArrayList (java.util.ArrayList)11 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)10 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 LogicalCorrelate (org.apache.calcite.rel.logical.LogicalCorrelate)6 RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)6 ImmutableList (com.google.common.collect.ImmutableList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)5 RelOptCluster (org.apache.calcite.plan.RelOptCluster)4 RelBuilder (org.apache.calcite.tools.RelBuilder)4 Function2 (org.apache.calcite.linq4j.function.Function2)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 NlsString (org.apache.calcite.util.NlsString)3 Map (java.util.Map)2