Skip to content

Commit 28b00f4

Browse files
committed
[CALCITE-7431] RelTraitSet#getTrait seems to mishandle RelCompositeTrait
1 parent af9ba51 commit 28b00f4

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

core/src/main/java/org/apache/calcite/plan/RelTraitSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public boolean containsIfApplicable(RelTrait trait) {
629629
final RelTrait stored = getTrait(index);
630630
if (stored instanceof RelCompositeTrait) {
631631
// Any member of the composite matching the sought trait counts.
632-
for (RelMultipleTrait t : ((RelCompositeTrait<?>) stored).traitList()) {
632+
for (Object t : ((RelCompositeTrait<?>) stored).traitList()) {
633633
if (t == trait) {
634634
return true;
635635
}

core/src/test/java/org/apache/calcite/plan/RelTraitTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import org.apache.calcite.rel.RelCollation;
2121
import org.apache.calcite.rel.RelCollationTraitDef;
2222
import org.apache.calcite.rel.RelCollations;
23+
import org.apache.calcite.rel.RelNode;
24+
import org.apache.calcite.tools.FrameworkConfig;
25+
import org.apache.calcite.tools.RelBuilder;
26+
import org.apache.calcite.test.RelBuilderTest;
2327

2428
import com.google.common.collect.ImmutableList;
2529

@@ -33,6 +37,7 @@
3337
import static org.hamcrest.Matchers.hasSize;
3438
import static org.junit.jupiter.api.Assertions.assertFalse;
3539
import static org.junit.jupiter.api.Assertions.assertNotEquals;
40+
import static org.junit.jupiter.api.Assertions.assertThrows;
3641
import static org.junit.jupiter.api.Assertions.assertTrue;
3742

3843
import static java.lang.Integer.toHexString;
@@ -98,4 +103,34 @@ private void assertCanonical(String message, Supplier<List<RelCollation>> collat
98103
RelTraitSet traits3 = traits2.replace(RelCollations.of(1));
99104
assertFalse(traits3.equalsSansConvention(traits2));
100105
}
106+
107+
/** Test for
108+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7431">[CALCITE-7431]
109+
* RelTraitSet#getTrait seems to mishandle RelCompositeTrait</a>. */
110+
@Test void testRelCompositeTrait() {
111+
// Build: EMP -> Sort(MGR asc) -> Project(MGR, MGR as MGR2)
112+
// The project maps both output columns 0 and 1 back to input column 3
113+
// (MGR), so the planner derives two collations: [0 ASC] and [1 ASC], which
114+
// are stored as a RelCompositeTrait in the output trait set.
115+
final FrameworkConfig config = RelBuilderTest.config().build();
116+
final RelBuilder b = RelBuilder.create(config);
117+
final RelNode in = b
118+
.scan("EMP")
119+
.sort(3) // MGR asc
120+
.project(b.field(3), b.alias(b.field(3), "MGR2")) // MGR, MGR as MGR2
121+
.build();
122+
123+
final RelTraitSet traitSet = in.getTraitSet();
124+
125+
final List<RelCollation> collations = traitSet.getCollations();
126+
assertTrue(collations.size() >= 2,
127+
"getCollations() should expose all composite collations");
128+
129+
assertThrows(IllegalStateException.class, traitSet::getCollation,
130+
"getCollation() should throw when a RelCompositeTrait is present");
131+
132+
assertThrows(IllegalStateException.class,
133+
() -> traitSet.getTrait(RelCollationTraitDef.INSTANCE),
134+
"getTrait() should throw when a RelCompositeTrait is present");
135+
}
101136
}

0 commit comments

Comments
 (0)