Search in sources :

Example 1 with LogicalCorrelate

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate in project flink by apache.

the class FlinkRelDecorrelator method createCopyHook.

private Function2<RelNode, RelNode, Void> createCopyHook() {
    return new Function2<RelNode, RelNode, Void>() {

        public Void apply(RelNode oldNode, RelNode newNode) {
            if (cm.mapRefRelToCorVar.containsKey(oldNode)) {
                cm.mapRefRelToCorVar.putAll(newNode, cm.mapRefRelToCorVar.get(oldNode));
            }
            if (oldNode instanceof LogicalCorrelate && newNode instanceof LogicalCorrelate) {
                LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
                CorrelationId c = oldCor.getCorrelationId();
                if (cm.mapCorVarToCorRel.get(c) == oldNode) {
                    cm.mapCorVarToCorRel.put(c, newNode);
                }
                if (generatedCorRels.contains(oldNode)) {
                    generatedCorRels.add((LogicalCorrelate) newNode);
                }
            }
            return null;
        }
    };
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Function2(org.apache.calcite.linq4j.function.Function2) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 2 with LogicalCorrelate

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate in project hive by apache.

the class HiveRelDecorrelator method decorrelateRel.

public Frame decorrelateRel(HiveFilter rel) throws SemanticException {
    // 
    // Rewrite logic:
    // 
    // 1. If a LogicalFilter references a correlated field in its filter
    // condition, rewrite the LogicalFilter to be
    // LogicalFilter
    // LogicalJoin(cross product)
    // OriginalFilterInput
    // ValueGenerator(produces distinct sets of correlated variables)
    // and rewrite the correlated fieldAccess in the filter condition to
    // reference the LogicalJoin output.
    // 
    // 2. If LogicalFilter does not reference correlated variables, simply
    // rewrite the filter condition using new input.
    // 
    final RelNode oldInput = rel.getInput();
    Frame frame = getInvoke(oldInput, rel);
    if (frame == null) {
        // If input has not been rewritten, do not rewrite this rel.
        return null;
    }
    Frame oldInputFrame = frame;
    // and produce the correlated variables in the new output.
    if (cm.mapRefRelToCorRef.containsKey(rel)) {
        frame = decorrelateInputWithValueGenerator(rel);
    }
    boolean valueGenerator = true;
    if (frame.r == oldInputFrame.r) {
        // this means correated value generator wasn't generated
        valueGenerator = false;
    }
    if (oldInput instanceof LogicalCorrelate && ((LogicalCorrelate) oldInput).getJoinType() == SemiJoinType.SEMI && !cm.mapRefRelToCorRef.containsKey(rel)) {
        // this conditions need to be pushed into semi-join since this condition
        // corresponds to IN
        HiveSemiJoin join = ((HiveSemiJoin) frame.r);
        final List<RexNode> conditions = new ArrayList<>();
        RexNode joinCond = join.getCondition();
        conditions.add(joinCond);
        conditions.add(decorrelateExpr(rel.getCondition(), valueGenerator));
        final RexNode condition = RexUtil.composeConjunction(rexBuilder, conditions, false);
        RelNode newRel = HiveSemiJoin.getSemiJoin(frame.r.getCluster(), frame.r.getTraitSet(), join.getLeft(), join.getRight(), condition, join.getLeftKeys(), join.getRightKeys());
        return register(rel, newRel, frame.oldToNewOutputs, frame.corDefOutputs);
    }
    // Replace the filter expression to reference output of the join
    // Map filter to the new filter over join
    relBuilder.push(frame.r).filter((decorrelateExpr(rel.getCondition(), valueGenerator)));
    // input rel.
    return register(rel, relBuilder.build(), frame.oldToNewOutputs, frame.corDefOutputs);
}
Also used : HiveRelNode(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode) RelNode(org.apache.calcite.rel.RelNode) HiveSemiJoin(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSemiJoin) ArrayList(java.util.ArrayList) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with LogicalCorrelate

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate in project calcite by apache.

the class EnumerableCorrelateRule method convert.

public RelNode convert(RelNode rel) {
    final LogicalCorrelate c = (LogicalCorrelate) rel;
    final RelTraitSet traitSet = c.getTraitSet().replace(EnumerableConvention.INSTANCE);
    return new EnumerableCorrelate(rel.getCluster(), traitSet, convert(c.getLeft(), c.getLeft().getTraitSet().replace(EnumerableConvention.INSTANCE)), convert(c.getRight(), c.getRight().getTraitSet().replace(EnumerableConvention.INSTANCE)), c.getCorrelationId(), c.getRequiredColumns(), c.getJoinType());
}
Also used : LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 4 with LogicalCorrelate

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate in project calcite by apache.

the class RelDecorrelator method createCopyHook.

private Function2<RelNode, RelNode, Void> createCopyHook() {
    return new Function2<RelNode, RelNode, Void>() {

        public Void apply(RelNode oldNode, RelNode newNode) {
            if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
                cm.mapRefRelToCorRef.putAll(newNode, cm.mapRefRelToCorRef.get(oldNode));
            }
            if (oldNode instanceof LogicalCorrelate && newNode instanceof LogicalCorrelate) {
                LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
                CorrelationId c = oldCor.getCorrelationId();
                if (cm.mapCorToCorRel.get(c) == oldNode) {
                    cm.mapCorToCorRel.put(c, newNode);
                }
                if (generatedCorRels.contains(oldNode)) {
                    generatedCorRels.add((LogicalCorrelate) newNode);
                }
            }
            return null;
        }
    };
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Function2(org.apache.calcite.linq4j.function.Function2) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 5 with LogicalCorrelate

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate in project beam by apache.

the class BeamZetaSqlUnnestRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    LogicalCorrelate correlate = call.rel(0);
    RelNode outer = call.rel(1);
    RelNode uncollect = call.rel(2);
    if (correlate.getRequiredColumns().cardinality() != 1) {
        // can only unnest a single column
        return;
    }
    if (correlate.getJoinType() != JoinRelType.INNER) {
        return;
    }
    if (!(uncollect instanceof ZetaSqlUnnest)) {
        // Drop projection
        uncollect = ((SingleRel) uncollect).getInput();
        if (uncollect instanceof RelSubset) {
            uncollect = ((RelSubset) uncollect).getOriginal();
        }
        if (!(uncollect instanceof ZetaSqlUnnest)) {
            return;
        }
    }
    RelNode project = ((ZetaSqlUnnest) uncollect).getInput();
    if (project instanceof RelSubset) {
        project = ((RelSubset) project).getOriginal();
    }
    if (!(project instanceof LogicalProject)) {
        return;
    }
    if (((LogicalProject) project).getProjects().size() != 1) {
        // can only unnest a single column
        return;
    }
    RexNode exp = ((LogicalProject) project).getProjects().get(0);
    if (!(exp instanceof RexFieldAccess)) {
        return;
    }
    RexFieldAccess fieldAccess = (RexFieldAccess) exp;
    // Innermost field index comes first (e.g. struct.field1.field2 => [2, 1])
    ImmutableList.Builder<Integer> fieldAccessIndices = ImmutableList.builder();
    while (true) {
        fieldAccessIndices.add(fieldAccess.getField().getIndex());
        if (!(fieldAccess.getReferenceExpr() instanceof RexFieldAccess)) {
            break;
        }
        fieldAccess = (RexFieldAccess) fieldAccess.getReferenceExpr();
    }
    call.transformTo(new BeamZetaSqlUnnestRel(correlate.getCluster(), correlate.getTraitSet().replace(BeamLogicalConvention.INSTANCE), convert(outer, outer.getTraitSet().replace(BeamLogicalConvention.INSTANCE)), call.rel(2).getRowType(), fieldAccessIndices.build()));
}
Also used : RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) LogicalCorrelate(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate) LogicalProject(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalProject) RexFieldAccess(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexFieldAccess) RelSubset(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.volcano.RelSubset) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Aggregations

LogicalCorrelate (org.apache.calcite.rel.logical.LogicalCorrelate)12 RelNode (org.apache.calcite.rel.RelNode)9 CorrelationId (org.apache.calcite.rel.core.CorrelationId)6 ArrayList (java.util.ArrayList)4 RexNode (org.apache.calcite.rex.RexNode)4 HiveRelNode (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode)4 Function2 (org.apache.calcite.linq4j.function.Function2)3 RelSubset (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.volcano.RelSubset)2 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)2 LogicalCorrelate (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate)2 LogicalProject (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalProject)2 RexFieldAccess (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexFieldAccess)2 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 Join (org.apache.calcite.rel.core.Join)2 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)2 HiveSemiJoin (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSemiJoin)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1