Search in sources :

Example 26 with RelTraitSet

use of org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class SortPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillSortRel sort = (DrillSortRel) call.rel(0);
    final RelNode input = sort.getInput();
    // Keep the collation in logical sort. Convert input into a RelNode with 1) this collation, 2) Physical, 3) hash distributed on
    DrillDistributionTrait hashDistribution = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionField(sort)));
    final RelTraitSet traits = sort.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(hashDistribution);
    final RelNode convertedInput = convert(input, traits);
    if (isSingleMode(call)) {
        call.transformTo(convertedInput);
    } else {
        RelNode exch = new SingleMergeExchangePrel(sort.getCluster(), sort.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON), convertedInput, sort.getCollation());
        // transform logical "sort" into "SingleMergeExchange".
        call.transformTo(exch);
    }
}
Also used : DrillSortRel(org.apache.drill.exec.planner.logical.DrillSortRel) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 27 with RelTraitSet

use of org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class ScreenPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillScreenRelBase screen = (DrillScreenRelBase) call.rel(0);
    final RelNode input = call.rel(1);
    final RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON);
    final RelNode convertedInput = convert(input, traits);
    DrillScreenRelBase newScreen = new ScreenPrel(screen.getCluster(), screen.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON), convertedInput);
    call.transformTo(newScreen);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) DrillScreenRelBase(org.apache.drill.exec.planner.common.DrillScreenRelBase) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 28 with RelTraitSet

use of org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class DrillJoinRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LogicalJoin join = (LogicalJoin) call.rel(0);
    final RelNode left = join.getLeft();
    final RelNode right = join.getRight();
    final RelTraitSet traits = join.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
    final RelNode convertedLeft = convert(left, left.getTraitSet().plus(DrillRel.DRILL_LOGICAL));
    final RelNode convertedRight = convert(right, right.getTraitSet().plus(DrillRel.DRILL_LOGICAL));
    List<Integer> leftKeys = Lists.newArrayList();
    List<Integer> rightKeys = Lists.newArrayList();
    List<Boolean> filterNulls = Lists.newArrayList();
    int numLeftFields = convertedLeft.getRowType().getFieldCount();
    boolean addFilter = false;
    RexNode origJoinCondition = join.getCondition();
    RexNode newJoinCondition = origJoinCondition;
    RexNode remaining = RelOptUtil.splitJoinCondition(convertedLeft, convertedRight, origJoinCondition, leftKeys, rightKeys, filterNulls);
    boolean hasEquijoins = leftKeys.size() == rightKeys.size() && leftKeys.size() > 0;
    // For OUTER join, pulling up a non-eqivjoin filter will lead to incorrectly discarding qualified rows.
    if (!remaining.isAlwaysTrue()) {
        if (hasEquijoins && join.getJoinType() == JoinRelType.INNER) {
            addFilter = true;
            newJoinCondition = buildJoinCondition(convertedLeft, convertedRight, leftKeys, rightKeys, filterNulls, join.getCluster().getRexBuilder());
        }
    } else {
        newJoinCondition = buildJoinCondition(convertedLeft, convertedRight, leftKeys, rightKeys, filterNulls, join.getCluster().getRexBuilder());
    }
    try {
        if (!addFilter) {
            RelNode joinRel = new DrillJoinRel(join.getCluster(), traits, convertedLeft, convertedRight, newJoinCondition, join.getJoinType(), leftKeys, rightKeys);
            call.transformTo(joinRel);
        } else {
            RelNode joinRel = new DrillJoinRel(join.getCluster(), traits, convertedLeft, convertedRight, newJoinCondition, join.getJoinType(), leftKeys, rightKeys);
            call.transformTo(new DrillFilterRel(join.getCluster(), traits, joinRel, remaining));
        }
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) RexNode(org.apache.calcite.rex.RexNode)

Example 29 with RelTraitSet

use of org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class DrillAggregateRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LogicalAggregate aggregate = (LogicalAggregate) call.rel(0);
    final RelNode input = call.rel(1);
    if (aggregate.containsDistinctCall()) {
        // currently, don't use this rule if any of the aggregates contains DISTINCT
        return;
    }
    final RelTraitSet traits = aggregate.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
    final RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL));
    try {
        call.transformTo(new DrillAggregateRel(aggregate.getCluster(), traits, convertedInput, aggregate.indicator, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList()));
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 30 with RelTraitSet

use of org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class JoinPruleBase method createBroadcastPlan.

// Create join plan with left child ANY distributed and right child BROADCAST distributed. If the physical join type
// is MergeJoin, a collation must be provided for both left and right child and the plan will contain sort converter
// if necessary to provide the collation.
protected void createBroadcastPlan(final RelOptRuleCall call, final DrillJoinRel join, final RexNode joinCondition, final PhysicalJoinType physicalJoinType, final RelNode left, final RelNode right, final RelCollation collationLeft, final RelCollation collationRight) throws InvalidRelException {
    DrillDistributionTrait distBroadcastRight = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.BROADCAST_DISTRIBUTED);
    RelTraitSet traitsRight = null;
    RelTraitSet traitsLeft = left.getTraitSet().plus(Prel.DRILL_PHYSICAL);
    if (physicalJoinType == PhysicalJoinType.MERGE_JOIN) {
        assert collationLeft != null && collationRight != null;
        traitsLeft = traitsLeft.plus(collationLeft);
        traitsRight = right.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(collationRight).plus(distBroadcastRight);
    } else if (physicalJoinType == PhysicalJoinType.HASH_JOIN || physicalJoinType == PhysicalJoinType.NESTEDLOOP_JOIN) {
        traitsRight = right.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(distBroadcastRight);
    }
    final RelNode convertedLeft = convert(left, traitsLeft);
    final RelNode convertedRight = convert(right, traitsRight);
    boolean traitProp = false;
    if (traitProp) {
        if (physicalJoinType == PhysicalJoinType.MERGE_JOIN) {
            new SubsetTransformer<DrillJoinRel, InvalidRelException>(call) {

                @Override
                public RelNode convertChild(final DrillJoinRel join, final RelNode rel) throws InvalidRelException {
                    DrillDistributionTrait toDist = rel.getTraitSet().getTrait(DrillDistributionTraitDef.INSTANCE);
                    RelTraitSet newTraitsLeft = newTraitSet(Prel.DRILL_PHYSICAL, collationLeft, toDist);
                    RelNode newLeft = convert(left, newTraitsLeft);
                    return new MergeJoinPrel(join.getCluster(), newTraitsLeft, newLeft, convertedRight, joinCondition, join.getJoinType());
                }
            }.go(join, convertedLeft);
        } else if (physicalJoinType == PhysicalJoinType.HASH_JOIN) {
            new SubsetTransformer<DrillJoinRel, InvalidRelException>(call) {

                @Override
                public RelNode convertChild(final DrillJoinRel join, final RelNode rel) throws InvalidRelException {
                    DrillDistributionTrait toDist = rel.getTraitSet().getTrait(DrillDistributionTraitDef.INSTANCE);
                    RelTraitSet newTraitsLeft = newTraitSet(Prel.DRILL_PHYSICAL, toDist);
                    RelNode newLeft = convert(left, newTraitsLeft);
                    return new HashJoinPrel(join.getCluster(), newTraitsLeft, newLeft, convertedRight, joinCondition, join.getJoinType());
                }
            }.go(join, convertedLeft);
        } else if (physicalJoinType == PhysicalJoinType.NESTEDLOOP_JOIN) {
            new SubsetTransformer<DrillJoinRel, InvalidRelException>(call) {

                @Override
                public RelNode convertChild(final DrillJoinRel join, final RelNode rel) throws InvalidRelException {
                    DrillDistributionTrait toDist = rel.getTraitSet().getTrait(DrillDistributionTraitDef.INSTANCE);
                    RelTraitSet newTraitsLeft = newTraitSet(Prel.DRILL_PHYSICAL, toDist);
                    RelNode newLeft = convert(left, newTraitsLeft);
                    return new NestedLoopJoinPrel(join.getCluster(), newTraitsLeft, newLeft, convertedRight, joinCondition, join.getJoinType());
                }
            }.go(join, convertedLeft);
        }
    } else {
        if (physicalJoinType == PhysicalJoinType.MERGE_JOIN) {
            call.transformTo(new MergeJoinPrel(join.getCluster(), convertedLeft.getTraitSet(), convertedLeft, convertedRight, joinCondition, join.getJoinType()));
        } else if (physicalJoinType == PhysicalJoinType.HASH_JOIN) {
            call.transformTo(new HashJoinPrel(join.getCluster(), convertedLeft.getTraitSet(), convertedLeft, convertedRight, joinCondition, join.getJoinType()));
        } else if (physicalJoinType == PhysicalJoinType.NESTEDLOOP_JOIN) {
            call.transformTo(new NestedLoopJoinPrel(join.getCluster(), convertedLeft.getTraitSet(), convertedLeft, convertedRight, joinCondition, join.getJoinType()));
        }
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) DrillJoinRel(org.apache.drill.exec.planner.logical.DrillJoinRel) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)36 RelNode (org.apache.calcite.rel.RelNode)25 InvalidRelException (org.apache.calcite.rel.InvalidRelException)8 ArrayList (java.util.ArrayList)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 RexNode (org.apache.calcite.rex.RexNode)4 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)3 RelCollation (org.apache.calcite.rel.RelCollation)3 Stopwatch (com.google.common.base.Stopwatch)2 ImmutableList (com.google.common.collect.ImmutableList)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 Sort (org.apache.calcite.rel.core.Sort)2 Window (org.apache.calcite.rel.core.Window)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RexInputRef (org.apache.calcite.rex.RexInputRef)2 Table (org.apache.calcite.schema.Table)2 DrillAggregateRel (org.apache.drill.exec.planner.logical.DrillAggregateRel)2 DrillDirectScanRel (org.apache.drill.exec.planner.logical.DrillDirectScanRel)2 DrillRel (org.apache.drill.exec.planner.logical.DrillRel)2