Search in sources :

Example 1 with Union

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

the class JoinUnionTransposeRule method onMatch.

public void onMatch(RelOptRuleCall call) {
    final Join join = call.rel(0);
    final Union unionRel;
    RelNode otherInput;
    boolean unionOnLeft;
    if (call.rel(1) instanceof Union) {
        unionRel = call.rel(1);
        otherInput = call.rel(2);
        unionOnLeft = true;
    } else {
        otherInput = call.rel(1);
        unionRel = call.rel(2);
        unionOnLeft = false;
    }
    if (!unionRel.all) {
        return;
    }
    if (!join.getVariablesSet().isEmpty()) {
        return;
    }
    // in one or both branches of the union)
    if (unionOnLeft) {
        if (join.getJoinType().generatesNullsOnLeft()) {
            return;
        }
    } else {
        if (join.getJoinType().generatesNullsOnRight()) {
            return;
        }
    }
    List<RelNode> newUnionInputs = new ArrayList<RelNode>();
    for (RelNode input : unionRel.getInputs()) {
        RelNode joinLeft;
        RelNode joinRight;
        if (unionOnLeft) {
            joinLeft = input;
            joinRight = otherInput;
        } else {
            joinLeft = otherInput;
            joinRight = input;
        }
        newUnionInputs.add(join.copy(join.getTraitSet(), join.getCondition(), joinLeft, joinRight, join.getJoinType(), join.isSemiJoinDone()));
    }
    final SetOp newUnionRel = unionRel.copy(unionRel.getTraitSet(), newUnionInputs, true);
    call.transformTo(newUnionRel);
}
Also used : SetOp(org.apache.calcite.rel.core.SetOp) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) Join(org.apache.calcite.rel.core.Join) Union(org.apache.calcite.rel.core.Union)

Example 2 with Union

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

the class AggregateUnionTransposeRule method onMatch.

public void onMatch(RelOptRuleCall call) {
    Aggregate aggRel = call.rel(0);
    Union union = call.rel(1);
    if (!union.all) {
        // which yields 25 (incorrect).
        return;
    }
    int groupCount = aggRel.getGroupSet().cardinality();
    List<AggregateCall> transformedAggCalls = transformAggCalls(aggRel.copy(aggRel.getTraitSet(), aggRel.getInput(), false, aggRel.getGroupSet(), null, aggRel.getAggCallList()), groupCount, aggRel.getAggCallList());
    if (transformedAggCalls == null) {
        // which we can't handle
        return;
    }
    // create corresponding aggregates on top of each union child
    final RelBuilder relBuilder = call.builder();
    int transformCount = 0;
    final RelMetadataQuery mq = call.getMetadataQuery();
    for (RelNode input : union.getInputs()) {
        boolean alreadyUnique = RelMdUtil.areColumnsDefinitelyUnique(mq, input, aggRel.getGroupSet());
        relBuilder.push(input);
        if (!alreadyUnique) {
            ++transformCount;
            relBuilder.aggregate(relBuilder.groupKey(aggRel.getGroupSet(), null), aggRel.getAggCallList());
        }
    }
    if (transformCount == 0) {
        // planners would succumb)
        return;
    }
    // create a new union whose children are the aggregates created above
    relBuilder.union(true, union.getInputs().size());
    relBuilder.aggregate(relBuilder.groupKey(aggRel.getGroupSet(), aggRel.getGroupSets()), transformedAggCalls);
    call.transformTo(relBuilder.build());
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) Union(org.apache.calcite.rel.core.Union) LogicalUnion(org.apache.calcite.rel.logical.LogicalUnion)

Example 3 with Union

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

the class UnionEliminatorRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    Union union = call.rel(0);
    if (union.getInputs().size() != 1) {
        return;
    }
    if (!union.all) {
        return;
    }
    // REVIEW jvs 14-Mar-2006:  why don't we need to register
    // the equivalence here like we do in AggregateRemoveRule?
    call.transformTo(union.getInputs().get(0));
}
Also used : Union(org.apache.calcite.rel.core.Union) LogicalUnion(org.apache.calcite.rel.logical.LogicalUnion)

Example 4 with Union

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

the class UnionMergeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final SetOp topOp = call.rel(0);
    @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass = (Class) operands.get(0).getMatchedClass();
    // For Union and Intersect, we want to combine the set-op that's in the
    // second input first.
    // 
    // For example, we reduce
    // Union(Union(a, b), Union(c, d))
    // to
    // Union(Union(a, b), c, d)
    // in preference to
    // Union(a, b, Union(c, d))
    // 
    // But for Minus, we can only reduce the left input. It is not valid to
    // reduce
    // Minus(a, Minus(b, c))
    // to
    // Minus(a, b, c)
    // 
    // Hence, that's why the rule pattern matches on generic RelNodes rather
    // than explicit sub-classes of SetOp.  By doing so, and firing this rule
    // in a bottom-up order, it allows us to only specify a single
    // pattern for this rule.
    final SetOp bottomOp;
    if (setOpClass.isInstance(call.rel(2)) && !Minus.class.isAssignableFrom(setOpClass)) {
        bottomOp = call.rel(2);
    } else if (setOpClass.isInstance(call.rel(1))) {
        bottomOp = call.rel(1);
    } else {
        return;
    }
    // In case (2), all operators become DISTINCT.
    if (topOp.all && !bottomOp.all) {
        return;
    }
    // Combine the inputs from the bottom set-op with the other inputs from
    // the top set-op.
    final RelBuilder relBuilder = call.builder();
    if (setOpClass.isInstance(call.rel(2)) && !Minus.class.isAssignableFrom(setOpClass)) {
        relBuilder.push(topOp.getInput(0));
        relBuilder.pushAll(bottomOp.getInputs());
        // topOp.getInputs().size() may be more than 2
        for (int index = 2; index < topOp.getInputs().size(); index++) {
            relBuilder.push(topOp.getInput(index));
        }
    } else {
        relBuilder.pushAll(bottomOp.getInputs());
        relBuilder.pushAll(Util.skip(topOp.getInputs()));
    }
    int n = bottomOp.getInputs().size() + topOp.getInputs().size() - 1;
    if (topOp instanceof Union) {
        relBuilder.union(topOp.all, n);
    } else if (topOp instanceof Intersect) {
        relBuilder.intersect(topOp.all, n);
    } else if (topOp instanceof Minus) {
        relBuilder.minus(topOp.all, n);
    }
    call.transformTo(relBuilder.build());
}
Also used : SetOp(org.apache.calcite.rel.core.SetOp) LogicalIntersect(org.apache.calcite.rel.logical.LogicalIntersect) Intersect(org.apache.calcite.rel.core.Intersect) RelBuilder(org.apache.calcite.tools.RelBuilder) Union(org.apache.calcite.rel.core.Union) LogicalUnion(org.apache.calcite.rel.logical.LogicalUnion) Minus(org.apache.calcite.rel.core.Minus) LogicalMinus(org.apache.calcite.rel.logical.LogicalMinus)

Example 5 with Union

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

the class UnionToDistinctRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final Union union = call.rel(0);
    if (union.all) {
        // nothing to do
        return;
    }
    final RelBuilder relBuilder = call.builder();
    relBuilder.pushAll(union.getInputs());
    relBuilder.union(true, union.getInputs().size());
    relBuilder.distinct();
    call.transformTo(relBuilder.build());
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) Union(org.apache.calcite.rel.core.Union) LogicalUnion(org.apache.calcite.rel.logical.LogicalUnion)

Aggregations

Union (org.apache.calcite.rel.core.Union)23 RelNode (org.apache.calcite.rel.RelNode)14 RelBuilder (org.apache.calcite.tools.RelBuilder)9 ArrayList (java.util.ArrayList)8 Aggregate (org.apache.calcite.rel.core.Aggregate)7 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)6 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RexNode (org.apache.calcite.rex.RexNode)6 Join (org.apache.calcite.rel.core.Join)4 Sort (org.apache.calcite.rel.core.Sort)4 LogicalUnion (org.apache.calcite.rel.logical.LogicalUnion)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)3 Filter (org.apache.calcite.rel.core.Filter)3 Project (org.apache.calcite.rel.core.Project)3 SetOp (org.apache.calcite.rel.core.SetOp)3 HashMap (java.util.HashMap)2 BeamRelMetadataQuery (org.apache.beam.sdk.extensions.sql.impl.planner.BeamRelMetadataQuery)2 NodeStats (org.apache.beam.sdk.extensions.sql.impl.planner.NodeStats)2 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)2