Skip to content

Commit

Permalink
[CALCITE-6488] Ensure collations created by RelCollations are canoniz…
Browse files Browse the repository at this point in the history
…ed once
  • Loading branch information
rubenada committed Jul 21, 2024
1 parent ed1f54f commit 0deab6f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static RelCollation shift(RelCollation collation, int offset) {
for (RelFieldCollation fc : collation.getFieldCollations()) {
fieldCollations.add(fc.shift(offset));
}
return new RelCollationImpl(fieldCollations.build());
return RelCollationTraitDef.INSTANCE.canonize(new RelCollationImpl(fieldCollations.build()));
}

/** Creates a copy of this collation that changes the ordinals of input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public SortJoinCopyRule(Class<? extends Sort> sortClass,
if (leftFieldCollation.isEmpty()) {
newLeftInput = join.getLeft();
} else {
final RelCollation leftCollation =
RelCollationTraitDef.INSTANCE.canonize(
RelCollations.of(leftFieldCollation));
final RelCollation leftCollation = RelCollations.of(leftFieldCollation);
// If left table already sorted don't add a sort
if (RelMdUtil.checkInputForCollationAndLimit(
metadataQuery,
Expand All @@ -121,9 +119,8 @@ public SortJoinCopyRule(Class<? extends Sort> sortClass,
newRightInput = join.getRight();
} else {
final RelCollation rightCollation =
RelCollationTraitDef.INSTANCE.canonize(
RelCollations.shift(RelCollations.of(rightFieldCollation),
-join.getLeft().getRowType().getFieldCount()));
RelCollations.shift(RelCollations.of(rightFieldCollation),
-join.getLeft().getRowType().getFieldCount());
// If right table already sorted don't add a sort
if (RelMdUtil.checkInputForCollationAndLimit(
metadataQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollationTraitDef;
import org.apache.calcite.rel.RelCollations;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
Expand Down Expand Up @@ -145,9 +144,7 @@ public SortJoinTransposeRule(Class<? extends Sort> sortClass,
newRightInput = join.getRight();
} else {
final RelCollation rightCollation =
RelCollationTraitDef.INSTANCE.canonize(
RelCollations.shift(sort.getCollation(),
-join.getLeft().getRowType().getFieldCount()));
RelCollations.shift(sort.getCollation(), -join.getLeft().getRowType().getFieldCount());
// If the input is already sorted and we are not reducing the number of tuples,
// we bail out
if (RelMdUtil.checkInputForCollationAndLimit(mq, join.getRight(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ protected SortRemoveConstantKeysRule(Config config) {
return;
}

final RelCollation collation =
RelCollationTraitDef.INSTANCE.canonize(RelCollations.of(collationsList));
final RelCollation collation = RelCollations.of(collationsList);
final Sort result =
sort.copy(
sort.getTraitSet().replaceIf(RelCollationTraitDef.INSTANCE, () -> collation),
Expand Down

0 comments on commit 0deab6f

Please sign in to comment.