@Override public void onMatch(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); final HiveJoin join = call.rel(1); RelNode inputLeft = join.getLeft(); RelNode inputRight = join.getRight(); // We create a new sort operator on the corresponding input if (join.getJoinType() == JoinRelType.LEFT) { inputLeft = sortLimit.copy(sortLimit.getTraitSet(), inputLeft, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputLeft).setRuleCreated(true); } else { // Adjust right collation final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sortLimit.getCollation(), -join.getLeft().getRowType().getFieldCount())); inputRight = sortLimit.copy(sortLimit.getTraitSet().replace(rightCollation), inputRight, rightCollation, sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputRight).setRuleCreated(true); } // We copy the join and the top sort operator RelNode result = join.copy(join.getTraitSet(), join.getCondition(), inputLeft, inputRight, join.getJoinType(), join.isSemiJoinDone()); result = sortLimit.copy(sortLimit.getTraitSet(), result, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); call.transformTo(result); }
@Override public void onMatch(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); final HiveJoin join = call.rel(1); RelNode inputLeft = join.getLeft(); RelNode inputRight = join.getRight(); // We create a new sort operator on the corresponding input if (join.getJoinType() == JoinRelType.LEFT) { inputLeft = sortLimit.copy(sortLimit.getTraitSet(), inputLeft, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputLeft).setRuleCreated(true); } else { // Adjust right collation final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sortLimit.getCollation(), -join.getLeft().getRowType().getFieldCount())); inputRight = sortLimit.copy(sortLimit.getTraitSet().replace(rightCollation), inputRight, rightCollation, sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputRight).setRuleCreated(true); } // We copy the join and the top sort operator RelNode result = join.copy(join.getTraitSet(), join.getCondition(), inputLeft, inputRight, join.getJoinType(), join.isSemiJoinDone()); result = sortLimit.copy(sortLimit.getTraitSet(), result, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); call.transformTo(result); }
/** Helper method to determine a {@link Join}'s collation assuming that it * uses a merge-join algorithm. * * <p>If the inputs are sorted on other keys <em>in addition to</em> the join * key, the result preserves those collations too. */ public static List<RelCollation> mergeJoin(RelMetadataQuery mq, RelNode left, RelNode right, ImmutableIntList leftKeys, ImmutableIntList rightKeys) { final ImmutableList.Builder<RelCollation> builder = ImmutableList.builder(); final ImmutableList<RelCollation> leftCollations = mq.collations(left); assert RelCollations.contains(leftCollations, leftKeys) : "cannot merge join: left input is not sorted on left keys"; builder.addAll(leftCollations); final ImmutableList<RelCollation> rightCollations = mq.collations(right); assert RelCollations.contains(rightCollations, rightKeys) : "cannot merge join: right input is not sorted on right keys"; final int leftFieldCount = left.getRowType().getFieldCount(); for (RelCollation collation : rightCollations) { builder.add(RelCollations.shift(collation, leftFieldCount)); } return builder.build(); } }
/** Helper method to determine a {@link Join}'s collation assuming that it * uses a merge-join algorithm. * * <p>If the inputs are sorted on other keys <em>in addition to</em> the join * key, the result preserves those collations too. */ public static List<RelCollation> mergeJoin(RelMetadataQuery mq, RelNode left, RelNode right, ImmutableIntList leftKeys, ImmutableIntList rightKeys) { final ImmutableList.Builder<RelCollation> builder = ImmutableList.builder(); final ImmutableList<RelCollation> leftCollations = mq.collations(left); assert RelCollations.contains(leftCollations, leftKeys) : "cannot merge join: left input is not sorted on left keys"; builder.addAll(leftCollations); final ImmutableList<RelCollation> rightCollations = mq.collations(right); assert RelCollations.contains(rightCollations, rightKeys) : "cannot merge join: right input is not sorted on right keys"; final int leftFieldCount = left.getRowType().getFieldCount(); for (RelCollation collation : rightCollations) { builder.add(RelCollations.shift(collation, leftFieldCount)); } return builder.build(); } }
collations.add(RelCollations.shift(collation, offset)); traits = traits.replace(collation);
collations.add(RelCollations.shift(collation, offset)); traits = traits.replace(collation);
final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sort.getCollation(), -join.getLeft().getRowType().getFieldCount()));
final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sort.getCollation(), -join.getLeft().getRowType().getFieldCount()));