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

mihaibudiu 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 7478530c55 [CALCITE-7763] "TABLE t ORDER BY c" and "(SELECT ... LIMIT 
n) ORDER BY c" fail with UnsupportedOperationException
7478530c55 is described below

commit 7478530c55e4c112f5639a12c12cc551c449baea
Author: Guanyu Chen <[email protected]>
AuthorDate: Wed Sep 23 20:33:48 2026 -0700

    [CALCITE-7763] "TABLE t ORDER BY c" and "(SELECT ... LIMIT n) ORDER BY c" 
fail with UnsupportedOperationException
---
 .../java/org/apache/calcite/sql/SqlOrderBy.java    | 28 ++++++++++++---
 .../org/apache/calcite/test/SqlValidatorTest.java  | 41 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java 
b/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
index c0c88240f8..fe22547555 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
@@ -41,10 +41,10 @@ public class SqlOrderBy extends SqlCall {
     }
   };
 
-  public final SqlNode query;
-  public final SqlNodeList orderList;
-  public final @Nullable SqlNode offset;
-  public final @Nullable SqlNode fetch;
+  public SqlNode query;
+  public SqlNodeList orderList;
+  public @Nullable SqlNode offset;
+  public @Nullable SqlNode fetch;
 
   //~ Constructors -----------------------------------------------------------
 
@@ -72,6 +72,26 @@ public SqlOrderBy(SqlParserPos pos, SqlNode query, 
SqlNodeList orderList,
     return ImmutableNullableList.of(query, orderList, offset, fetch);
   }
 
+  @SuppressWarnings("assignment.type.incompatible")
+  @Override public void setOperand(int i, @Nullable SqlNode operand) {
+    switch (i) {
+    case 0:
+      query = operand;
+      break;
+    case 1:
+      orderList = (SqlNodeList) operand;
+      break;
+    case 2:
+      offset = operand;
+      break;
+    case 3:
+      fetch = operand;
+      break;
+    default:
+      throw new AssertionError(i);
+    }
+  }
+
   /** Definition of {@code ORDER BY} operator. */
   private static class Operator extends SqlSpecialOperator {
     private Operator() {
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 1336a92029..b1d296e252 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -10475,6 +10475,47 @@ void testGroupExpressionEquivalenceParams() {
         .fails("Object 'NONEXISTENT' not found");
   }
 
+  @Test void testExplicitTableWithOrderBy() {
+    final String empRecordType =
+        "RecordType(INTEGER NOT NULL EMPNO,"
+            + " VARCHAR(20) NOT NULL ENAME,"
+            + " VARCHAR(10) NOT NULL JOB,"
+            + " INTEGER MGR,"
+            + " TIMESTAMP(0) NOT NULL HIREDATE,"
+            + " INTEGER NOT NULL SAL,"
+            + " INTEGER NOT NULL COMM,"
+            + " INTEGER NOT NULL DEPTNO,"
+            + " BOOLEAN NOT NULL SLACKER) NOT NULL";
+
+    sql("table emp order by empno")
+        .type(empRecordType)
+        .rewritesTo("SELECT *\n"
+            + "FROM `EMP`\n"
+            + "ORDER BY `EMP`.`EMPNO`");
+
+    sql("table emp order by empno desc")
+        .type(empRecordType)
+        .rewritesTo("SELECT *\n"
+            + "FROM `EMP`\n"
+            + "ORDER BY `EMP`.`EMPNO` DESC");
+
+    sql("table emp order by empno offset 2 rows fetch next 5 rows only")
+        .type(empRecordType)
+        .rewritesTo("SELECT *\n"
+            + "FROM `EMP`\n"
+            + "ORDER BY `EMP`.`EMPNO`\n"
+            + "OFFSET 2 ROWS\n"
+            + "FETCH NEXT 5 ROWS ONLY");
+
+    sql("(select empno from emp fetch next 5 rows only) order by empno")
+        .type("RecordType(INTEGER NOT NULL EMPNO) NOT NULL")
+        .rewritesTo("SELECT *\n"
+            + "FROM (SELECT `EMPNO`\n"
+            + "FROM `EMP`\n"
+            + "FETCH NEXT 5 ROWS ONLY)\n"
+            + "ORDER BY `EXPR$0`.`EMPNO`");
+  }
+
   @Test void testCollectionTable() {
     sql("select * from table(ramp(3))")
         .type("RecordType(INTEGER NOT NULL I) NOT NULL");

Reply via email to