Search in sources :

Example 26 with CorrelationId

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId in project calcite by apache.

the class RelFieldTrimmer method result.

protected TrimResult result(RelNode r, final Mapping mapping) {
    final RexBuilder rexBuilder = relBuilder.getRexBuilder();
    for (final CorrelationId correlation : r.getVariablesSet()) {
        r = r.accept(new CorrelationReferenceFinder() {

            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.id.equals(correlation) && v.getType().getFieldCount() == mapping.getSourceCount()) {
                    final int old = fieldAccess.getField().getIndex();
                    final int new_ = mapping.getTarget(old);
                    final RelDataTypeFactory.Builder typeBuilder = relBuilder.getTypeFactory().builder();
                    for (int target : Util.range(mapping.getTargetCount())) {
                        typeBuilder.add(v.getType().getFieldList().get(mapping.getSource(target)));
                    }
                    final RexNode newV = rexBuilder.makeCorrel(typeBuilder.build(), v.id);
                    if (old != new_) {
                        return rexBuilder.makeFieldAccess(newV, new_);
                    }
                }
                return fieldAccess;
            }
        });
    }
    return new TrimResult(r, mapping);
}
Also used : RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexNode(org.apache.calcite.rex.RexNode)

Example 27 with CorrelationId

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId in project calcite by apache.

the class RelStructuredTypeFlattener method updateRelInMap.

// ~ Methods ----------------------------------------------------------------
public void updateRelInMap(SortedSetMultimap<RelNode, CorrelationId> mapRefRelToCorVar) {
    for (RelNode rel : Lists.newArrayList(mapRefRelToCorVar.keySet())) {
        if (oldToNewRelMap.containsKey(rel)) {
            SortedSet<CorrelationId> corVarSet = mapRefRelToCorVar.removeAll(rel);
            mapRefRelToCorVar.putAll(oldToNewRelMap.get(rel), corVarSet);
        }
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 28 with CorrelationId

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId in project calcite by apache.

the class RelStructuredTypeFlattener method updateRelInMap.

public void updateRelInMap(SortedMap<CorrelationId, LogicalCorrelate> mapCorVarToCorRel) {
    for (CorrelationId corVar : mapCorVarToCorRel.keySet()) {
        LogicalCorrelate oldRel = mapCorVarToCorRel.get(corVar);
        if (oldToNewRelMap.containsKey(oldRel)) {
            RelNode newRel = oldToNewRelMap.get(oldRel);
            assert newRel instanceof LogicalCorrelate;
            mapCorVarToCorRel.put(corVar, (LogicalCorrelate) newRel);
        }
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) CorrelationId(org.apache.calcite.rel.core.CorrelationId) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate)

Example 29 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 LateralUnnestRowIDVisitor method visitLateral.

@Override
public Prel visitLateral(LateralJoinPrel prel, Boolean isRightOfLateral) throws RuntimeException {
    List<RelNode> children = Lists.newArrayList();
    children.add(((Prel) prel.getInput(0)).accept(this, isRightOfLateral));
    children.add(((Prel) prel.getInput(1)).accept(this, true));
    if (!isRightOfLateral) {
        return (Prel) prel.copy(prel.getTraitSet(), children);
    } else {
        // Adjust the column numbering due to an additional column "$drill_implicit_field$" is added to the inputs.
        Map<Integer, Integer> requiredColsMap = new HashMap<>();
        for (Integer corrColIndex : prel.getRequiredColumns()) {
            requiredColsMap.put(corrColIndex, corrColIndex + 1);
        }
        ImmutableBitSet requiredColumns = prel.getRequiredColumns().shift(1);
        CorrelationId corrId = prel.getCluster().createCorrel();
        RexCorrelVariable updatedCorrel = (RexCorrelVariable) prel.getCluster().getRexBuilder().makeCorrel(children.get(0).getRowType(), corrId);
        RelNode rightChild = children.get(1).accept(new CorrelateVarReplacer(new ProjectCorrelateTransposeRule.RexFieldAccessReplacer(prel.getCorrelationId(), updatedCorrel, prel.getCluster().getRexBuilder(), requiredColsMap)));
        return (Prel) prel.copy(prel.getTraitSet(), children.get(0), rightChild, corrId, requiredColumns, prel.getJoinType());
    }
}
Also used : RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashMap(java.util.HashMap) CorrelationId(org.apache.calcite.rel.core.CorrelationId) LateralJoinPrel(org.apache.drill.exec.planner.physical.LateralJoinPrel) Prel(org.apache.drill.exec.planner.physical.Prel) UnnestPrel(org.apache.drill.exec.planner.physical.UnnestPrel)

Example 30 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.

@Override
public RelNode visit(LogicalCorrelate correlate) {
    RelNode left = correlate.getLeft().accept(this);
    leftInputs.put(correlate.getCorrelationId(), left);
    RelNode right = correlate.getRight().accept(this);
    // after rewriting right input, no need to create Correlate with new CorrelationId
    if (correlate.getRight() == right || left == leftInputs.get(correlate.getCorrelationId())) {
        if (correlate.getLeft() == left) {
            return correlate;
        }
        // changed only inputs, but CorrelationId left the same
        return correlate.copy(correlate.getTraitSet(), Arrays.asList(left, right));
    }
    Correlate newCorrelate = correlate.copy(correlate.getTraitSet(), leftInputs.get(correlate.getCorrelationId()), right, updatedCorrelationIds.get(correlate.getCorrelationId()), ImmutableBitSet.of(left.getRowType().getFieldCount()), correlate.getJoinType());
    RelBuilder builder = DrillRelFactories.LOGICAL_BUILDER.create(correlate.getCluster(), null);
    builder.push(newCorrelate);
    List<RexNode> topProjectExpressions = left.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex())).collect(Collectors.toList());
    // Accommodate the new $COMPLEX_FIELD_NAME column.
    int rightStartIndex = left.getRowType().getFieldList().size() + 1;
    switch(correlate.getJoinType()) {
        case LEFT:
        case INNER:
            // adds field from the right input of correlate to the top project
            topProjectExpressions.addAll(right.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex() + rightStartIndex)).collect(Collectors.toList()));
        // fall through
        case ANTI:
        case SEMI:
            builder.project(topProjectExpressions, correlate.getRowType().getFieldNames());
    }
    return builder.build();
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Project(org.apache.calcite.rel.core.Project) Uncollect(org.apache.calcite.rel.core.Uncollect) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) Arrays(java.util.Arrays) SqlKind(org.apache.calcite.sql.SqlKind) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Correlate(org.apache.calcite.rel.core.Correlate) RexBuilder(org.apache.calcite.rex.RexBuilder) HashMap(java.util.HashMap) RelNode(org.apache.calcite.rel.RelNode) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Collectors(java.util.stream.Collectors) ImmutableList(org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) DrillRelFactories(org.apache.drill.exec.planner.logical.DrillRelFactories) RexShuttle(org.apache.calcite.rex.RexShuttle) Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) 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