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);
}
}
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);
}
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());
}
}
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());
}
}
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()));
}
}
}
Aggregations