From 1edd36002bd411cef7f3972fb7001d4124e433af Mon Sep 17 00:00:00 2001 From: Tisya Bhatia Date: Mon, 27 Jul 2026 13:55:28 -0700 Subject: [PATCH 1/2] [CALCITE-7675] Exclude constant literals from GROUP BY ALL / ORDER BY ALL keys --- .../calcite/sql/validate/SqlValidatorImpl.java | 11 ++++++++++- .../org/apache/calcite/test/SqlValidatorTest.java | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java index 02a044820f4..691fafcd1bc 100644 --- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java +++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java @@ -5320,6 +5320,12 @@ protected void rewriteOrderByAll(SqlSelect select) { throw newValidationError(expr, RESOURCE.orderByAllRequiresExplicitSelectList()); } + // A constant literal is a no-op sort key (nothing to reorder); excluding it also + // avoids materializing an ambiguous "ORDER BY " on unparse, which + // would be re-parsed as an ordinal position. + if (expr instanceof SqlLiteral) { + continue; + } keys.add(applyOrderByAllDirection(expr, desc, nulls, pos)); } select.setOrderBy(new SqlNodeList(keys, pos)); @@ -5521,7 +5527,10 @@ private void rewriteGroupByAll(SqlSelect select) { throw newValidationError(expr, RESOURCE.groupByAllRequiresExplicitSelectList()); } - if (aggOrOverFinder.findAgg(expr) == null) { + // A constant literal is a no-op grouping key (one group either way); excluding it + // also avoids materializing an ambiguous "GROUP BY " on unparse, + // which would be re-parsed as an ordinal position. + if (aggOrOverFinder.findAgg(expr) == null && !(expr instanceof SqlLiteral)) { keys.add(expr); } } 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 7bfc58eafe6..cf3359ddaa0 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java @@ -7367,6 +7367,13 @@ public boolean isBangEqualAllowed() { .withValidatorIdentifierExpansion(true) .withConformance(SqlConformanceEnum.BABEL) .ok(); + + // A constant literal is a no-op sort key and is excluded: only SAL is a + // sort key, so the rewrite does not emit an ambiguous "ORDER BY 42". + sql("select sal, 42 from emp order by all") + .rewritesTo("SELECT `SAL`, 42\n" + + "FROM `EMP`\n" + + "ORDER BY `SAL`"); } @Test void testOrder() { @@ -7829,6 +7836,13 @@ public boolean isBangEqualAllowed() { sql("select deptno as d, count(*) from emp group by all") .withConformance(SqlConformanceEnum.LENIENT) .ok(); + + // A constant literal is a no-op grouping key and is excluded: only DEPTNO + // becomes a key, so the rewrite does not emit an ambiguous "GROUP BY 42". + sql("select deptno, 42 from emp group by all") + .rewritesTo("SELECT `DEPTNO`, 42\n" + + "FROM `EMP`\n" + + "GROUP BY `EMP`.`DEPTNO`"); } /** Test case for From 5e4cae701996dfcdf38448d354389c22c48d0e26 Mon Sep 17 00:00:00 2001 From: Tisya Bhatia Date: Tue, 28 Jul 2026 08:18:46 -0700 Subject: [PATCH 2/2] [CALCITE-7675] Add ordinal-conformance tests for literal exclusion in GROUP BY ALL / ORDER BY ALL --- .../org/apache/calcite/test/SqlValidatorTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 cf3359ddaa0..88b6ce9fb5e 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java @@ -7374,6 +7374,13 @@ public boolean isBangEqualAllowed() { .rewritesTo("SELECT `SAL`, 42\n" + "FROM `EMP`\n" + "ORDER BY `SAL`"); + + // Under a conformance that sorts by ordinal, an emitted "ORDER BY 42" would + // be read as ordinal position 42 rather than as the constant; excluding the + // literal keeps the expansion valid in every conformance. + sql("select sal, 42 from emp order by all") + .withConformance(SqlConformanceEnum.LENIENT) + .ok(); } @Test void testOrder() { @@ -7843,6 +7850,13 @@ public boolean isBangEqualAllowed() { .rewritesTo("SELECT `DEPTNO`, 42\n" + "FROM `EMP`\n" + "GROUP BY `EMP`.`DEPTNO`"); + + // Under a conformance that groups by ordinal, an emitted "GROUP BY 42" would + // be read as ordinal position 42 and rejected as out of range; excluding the + // literal keeps the expansion valid in every conformance. + sql("select deptno, 42, count(*) from emp group by all") + .withConformance(SqlConformanceEnum.LENIENT) + .ok(); } /** Test case for