Search in sources :

Example 1 with DrillUnionRel

use of org.apache.drill.exec.planner.logical.DrillUnionRel in project drill by apache.

the class UnionAllPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillUnionRel union = (DrillUnionRel) call.rel(0);
    final List<RelNode> inputs = union.getInputs();
    List<RelNode> convertedInputList = Lists.newArrayList();
    PlannerSettings settings = PrelUtil.getPlannerSettings(call.getPlanner());
    boolean allHashDistributed = true;
    for (int i = 0; i < inputs.size(); i++) {
        RelNode child = inputs.get(i);
        List<DistributionField> childDistFields = Lists.newArrayList();
        RelNode convertedChild;
        for (RelDataTypeField f : child.getRowType().getFieldList()) {
            childDistFields.add(new DistributionField(f.getIndex()));
        }
        if (settings.isUnionAllDistributeEnabled()) {
            /*
         * Strictly speaking, union-all does not need re-distribution of data; but in Drill's execution
         * model, the data distribution and parallelism operators are the same. Here, we insert a
         * hash distribution operator to allow parallelism to be determined independently for the parent
         * and children. (See DRILL-4833).
         * Note that a round robin distribution would have sufficed but we don't have one.
         */
            DrillDistributionTrait hashChild = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(childDistFields));
            RelTraitSet traitsChild = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(hashChild);
            convertedChild = convert(child, PrelUtil.fixTraits(call, traitsChild));
        } else {
            RelTraitSet traitsChild = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
            convertedChild = convert(child, PrelUtil.fixTraits(call, traitsChild));
            allHashDistributed = false;
        }
        convertedInputList.add(convertedChild);
    }
    try {
        RelTraitSet traits;
        if (allHashDistributed) {
            // since all children of union-all are hash distributed, propagate the traits of the left child
            traits = convertedInputList.get(0).getTraitSet();
        } else {
            // output distribution trait is set to ANY since union-all inputs may be distributed in different ways
            // and unlike a join there are no join keys that allow determining how the output would be distributed.
            // Note that a downstream operator may impose a required distribution which would be satisfied by
            // inserting an Exchange after the Union-All.
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.ANY);
        }
        Preconditions.checkArgument(convertedInputList.size() >= 2, "Union list must be at least two items.");
        RelNode left = convertedInputList.get(0);
        for (int i = 1; i < convertedInputList.size(); i++) {
            left = new UnionAllPrel(union.getCluster(), traits, ImmutableList.of(left, convertedInputList.get(i)), false);
        }
        call.transformTo(left);
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) DrillUnionRel(org.apache.drill.exec.planner.logical.DrillUnionRel) DistributionField(org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)

Example 2 with DrillUnionRel

use of org.apache.drill.exec.planner.logical.DrillUnionRel in project drill by apache.

the class UnionDistinctPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillUnionRel union = (DrillUnionRel) call.rel(0);
    final List<RelNode> inputs = union.getInputs();
    List<RelNode> convertedInputList = Lists.newArrayList();
    RelTraitSet traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
    try {
        for (int i = 0; i < inputs.size(); i++) {
            RelNode convertedInput = convert(inputs.get(i), PrelUtil.fixTraits(call, traits));
            convertedInputList.add(convertedInput);
        }
        traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON);
        UnionDistinctPrel unionDistinct = new UnionDistinctPrel(union.getCluster(), traits, convertedInputList, false);
        call.transformTo(unionDistinct);
    } catch (InvalidRelException e) {
        tracer.warning(e.toString());
    }
}
Also used : InvalidRelException(org.apache.calcite.rel.InvalidRelException) RelNode(org.apache.calcite.rel.RelNode) DrillUnionRel(org.apache.drill.exec.planner.logical.DrillUnionRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 InvalidRelException (org.apache.calcite.rel.InvalidRelException)2 RelNode (org.apache.calcite.rel.RelNode)2 DrillUnionRel (org.apache.drill.exec.planner.logical.DrillUnionRel)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 DistributionField (org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)1