Search in sources :

Example 6 with InvalidRelException

use of org.apache.calcite.rel.InvalidRelException 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)

Example 7 with InvalidRelException

use of org.apache.calcite.rel.InvalidRelException in project drill by apache.

the class HashAggPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    if (!PrelUtil.getPlannerSettings(call.getPlanner()).isHashAggEnabled()) {
        return;
    }
    final DrillAggregateRel aggregate = (DrillAggregateRel) call.rel(0);
    final RelNode input = call.rel(1);
    if (aggregate.containsDistinctCall() || aggregate.getGroupCount() == 0) {
        // if there are no grouping keys
        return;
    }
    RelTraitSet traits = null;
    try {
        if (aggregate.getGroupSet().isEmpty()) {
            DrillDistributionTrait singleDist = DrillDistributionTrait.SINGLETON;
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(singleDist);
            createTransformRequest(call, aggregate, input, traits);
        } else {
            // hash distribute on all grouping keys
            DrillDistributionTrait distOnAllKeys = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionField(aggregate, true)));
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(distOnAllKeys);
            createTransformRequest(call, aggregate, input, traits);
            // hash distribute on single grouping key
            DrillDistributionTrait distOnOneKey = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionField(aggregate, false)));
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(distOnOneKey);
            createTransformRequest(call, aggregate, input, traits);
            if (create2PhasePlan(call, aggregate)) {
                traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
                RelNode convertedInput = convert(input, traits);
                new TwoPhaseSubset(call, distOnAllKeys).go(aggregate, convertedInput);
            }
        }
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) RelNode(org.apache.calcite.rel.RelNode) DrillAggregateRel(org.apache.drill.exec.planner.logical.DrillAggregateRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 8 with InvalidRelException

use of org.apache.calcite.rel.InvalidRelException in project drill by apache.

the class MergeJoinPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    PlannerSettings settings = PrelUtil.getPlannerSettings(call.getPlanner());
    final DrillJoinRel join = (DrillJoinRel) call.rel(0);
    final RelNode left = join.getLeft();
    final RelNode right = join.getRight();
    if (!checkPreconditions(join, left, right, settings)) {
        return;
    }
    boolean hashSingleKey = PrelUtil.getPlannerSettings(call.getPlanner()).isHashSingleKey();
    try {
        RelCollation collationLeft = getCollation(join.getLeftKeys());
        RelCollation collationRight = getCollation(join.getRightKeys());
        if (isDist) {
            createDistBothPlan(call, join, PhysicalJoinType.MERGE_JOIN, left, right, collationLeft, collationRight, hashSingleKey);
        } else {
            if (checkBroadcastConditions(call.getPlanner(), join, left, right)) {
                createBroadcastPlan(call, join, join.getCondition(), PhysicalJoinType.MERGE_JOIN, left, right, collationLeft, collationRight);
            }
        }
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) DrillJoinRel(org.apache.drill.exec.planner.logical.DrillJoinRel) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode)

Example 9 with InvalidRelException

use of org.apache.calcite.rel.InvalidRelException 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 10 with InvalidRelException

use of org.apache.calcite.rel.InvalidRelException 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)

Aggregations

InvalidRelException (org.apache.calcite.rel.InvalidRelException)13 RelNode (org.apache.calcite.rel.RelNode)11 RelTraitSet (org.apache.calcite.plan.RelTraitSet)8 DrillJoinRel (org.apache.drill.exec.planner.logical.DrillJoinRel)4 RelCollation (org.apache.calcite.rel.RelCollation)2 DrillAggregateRel (org.apache.drill.exec.planner.logical.DrillAggregateRel)2 DrillUnionRel (org.apache.drill.exec.planner.logical.DrillUnionRel)2 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)2 HiveRulesRegistry (org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveRulesRegistry)2 ArrayList (java.util.ArrayList)1 JoinInfo (org.apache.calcite.rel.core.JoinInfo)1 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)1 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)1 LogicalUnion (org.apache.calcite.rel.logical.LogicalUnion)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RexNode (org.apache.calcite.rex.RexNode)1 DistributionField (org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)1