Skip to content

Commit a98508f

Browse files
committed
[CALCITE-6460] SortRemoveConstantKeysRule fails with AssertionError due to mismatched collation on resulting Sort
1 parent dad9073 commit a98508f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

core/src/main/java/org/apache/calcite/rel/rules/SortRemoveConstantKeysRule.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.calcite.plan.RelOptPredicateList;
2020
import org.apache.calcite.plan.RelOptRuleCall;
2121
import org.apache.calcite.plan.RelRule;
22+
import org.apache.calcite.rel.RelCollation;
2223
import org.apache.calcite.rel.RelCollationTraitDef;
2324
import org.apache.calcite.rel.RelCollations;
2425
import org.apache.calcite.rel.RelFieldCollation;
@@ -77,8 +78,13 @@ protected SortRemoveConstantKeysRule(Config config) {
7778
return;
7879
}
7980

81+
final RelCollation collation =
82+
RelCollationTraitDef.INSTANCE.canonize(RelCollations.of(collationsList));
8083
final Sort result =
81-
sort.copy(sort.getTraitSet(), input, RelCollations.of(collationsList));
84+
sort.copy(
85+
sort.getTraitSet().replaceIf(RelCollationTraitDef.INSTANCE, () -> collation),
86+
input,
87+
collation);
8288
call.transformTo(result);
8389
call.getPlanner().prune(sort);
8490
}

core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,11 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
13831383
+ "group by deptno, sal\n"
13841384
+ "order by deptno, sal desc nulls first";
13851385
sql(sql)
1386+
// Use VolcanoPlanner with collation to ensure the correct creation of the new Sort
1387+
.withVolcanoPlanner(false, p -> {
1388+
p.addRelTraitDef(RelCollationTraitDef.INSTANCE);
1389+
RelOptUtil.registerDefaultRules(p, false, false);
1390+
})
13861391
.withRule(CoreRules.SORT_REMOVE_CONSTANT_KEYS)
13871392
.check();
13881393
}

0 commit comments

Comments
 (0)