This is an automated email from the ASF dual-hosted git repository.

zhenchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new d59c7f3493 [CALCITE-7427] Query with "ORDER BY NULL" throws 
"NoSuchMethodException: compareNullsLast"
d59c7f3493 is described below

commit d59c7f34936ef3cc0801812963686f4bd2c1f9e0
Author: Zhen Chen <[email protected]>
AuthorDate: Wed Mar 4 21:55:26 2026 +0800

    [CALCITE-7427] Query with "ORDER BY NULL" throws "NoSuchMethodException: 
compareNullsLast"
---
 .../calcite/adapter/enumerable/PhysTypeImpl.java   |  8 +++++++
 core/src/test/resources/sql/sort.iq                | 25 ++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/PhysTypeImpl.java 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/PhysTypeImpl.java
index e3fdd4d366..a44d4f69df 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/PhysTypeImpl.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/PhysTypeImpl.java
@@ -319,6 +319,10 @@ static PhysType of(
     body.add(Expressions.declare(mod, parameterC, null));
     for (RelFieldCollation collation : collations) {
       final int index = collation.getFieldIndex();
+      // NULL literal (Void) is always null; comparing null == null yields 0, 
skip.
+      if (fieldClass(index) == Void.class) {
+        continue;
+      }
       final RelDataType fieldType = 
rowType.getFieldList().get(index).getType();
       final Expression fieldComparator = 
generateCollatorExpression(fieldType.getCollation());
       Expression arg0 = fieldReference(parameterV0, index);
@@ -443,6 +447,10 @@ private Expression generateComparator(RelCollation 
collation,
     body.add(Expressions.declare(mod, parameterC, null));
     for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
       final int index = fieldCollation.getFieldIndex();
+      // NULL literal (Void) is always null; comparing null == null yields 0, 
skip.
+      if (fieldClass(index) == Void.class) {
+        continue;
+      }
       final RelDataType fieldType = 
rowType.getFieldList().get(index).getType();
       final Expression fieldComparator = 
generateCollatorExpression(fieldType.getCollation());
       Expression arg0 = fieldReference(parameterV0, index);
diff --git a/core/src/test/resources/sql/sort.iq 
b/core/src/test/resources/sql/sort.iq
index b9e0412ff9..b93be76837 100644
--- a/core/src/test/resources/sql/sort.iq
+++ b/core/src/test/resources/sql/sort.iq
@@ -508,4 +508,29 @@ order by arr desc nulls last;
 
 !ok
 
+# [CALCITE-7427] Query with "ORDER BY NULL" throws "NoSuchMethodException: 
compareNullsLast"
+!use scott
+SELECT * FROM emp ORDER BY deptno, null, empno;
++-------+--------+-----------+------+------------+---------+---------+--------+
+| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
++-------+--------+-----------+------+------------+---------+---------+--------+
+|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |         |     10 |
+|  7839 | KING   | PRESIDENT |      | 1981-11-17 | 5000.00 |         |     10 |
+|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |         |     10 |
+|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |         |     20 |
+|  7566 | JONES  | MANAGER   | 7839 | 1981-02-04 | 2975.00 |         |     20 |
+|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |         |     20 |
+|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |         |     20 |
+|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |         |     20 |
+|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
+|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
+|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
+|  7698 | BLAKE  | MANAGER   | 7839 | 1981-01-05 | 2850.00 |         |     30 |
+|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
+|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |         |     30 |
++-------+--------+-----------+------+------------+---------+---------+--------+
+(14 rows)
+
+!ok
+
 # End sort.iq

Reply via email to