xiedeyantu commented on code in PR #4745:
URL: https://github.com/apache/calcite/pull/4745#discussion_r2690580520


##########
core/src/main/java/org/apache/calcite/plan/RelTraitSet.java:
##########
@@ -389,7 +389,14 @@ public RelTraitSet getDefaultSansConvention() {
    */
   @SuppressWarnings("unchecked")
   public <T extends RelCollation> @Nullable T getCollation() {
-    return (@Nullable T) getTrait(RelCollationTraitDef.INSTANCE);
+    RelTrait trait = getTrait(RelCollationTraitDef.INSTANCE);
+    if (trait instanceof RelCompositeTrait) {
+      // If the trait is a composite trait, we return the first one as a
+      // representative trait. If RelCompositeTrait contain [[a, b], [a]].
+      // Selects [a,b] as it implies [a]; preserves the strongest ordering.
+      return (T) ((RelCompositeTrait) trait).trait(0);

Review Comment:
   In the case of `order by arr nulls last`, the following error will be 
reported:
   ```
   > java.sql.SQLException: Error while executing SQL "select * from
   > (values
   >     (2, array[null, 3]),
   >     (3, array[3, 4]),
   >     (1, array[1, 2]),
   >     (4, array[4, 5]),
   >     (5, cast(null as integer array))) as t(id, arr)
   > order by arr nulls last": class org.apache.calcite.plan.RelCompositeTrait 
cannot be cast to class org.apache.calcite.rel.RelCollation 
(org.apache.calcite.plan.RelCompositeTrait and 
org.apache.calcite.rel.RelCollation are in unnamed module of loader 'app')
   >    at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
   >    at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
   >    at 
org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:164)
   >    at 
org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:228)
   >    at net.hydromatic.quidem.Quidem.checkResult(Quidem.java:317)
   >    at net.hydromatic.quidem.Quidem.access$2600(Quidem.java:54)
   >    at 
net.hydromatic.quidem.Quidem$ContextImpl.checkResult(Quidem.java:1778)
   >    at 
net.hydromatic.quidem.Quidem$CheckResultCommand.execute(Quidem.java:985)
   >    at 
net.hydromatic.quidem.Quidem$CompositeCommand.execute(Quidem.java:1522)
   >    at net.hydromatic.quidem.Quidem.execute(Quidem.java:204)
   >    at org.apache.calcite.test.QuidemTest.checkRun(QuidemTest.java:350)
   >    at org.apache.calcite.test.QuidemTest.test(QuidemTest.java:532)
   >    at org.apache.calcite.test.CoreQuidemTest2.main(CoreQuidemTest2.java:38)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to