You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2023/09/27 18:18:24 UTC
[calcite] branch main updated: [CALCITE-6007] Sub-query that contains WITH and has no alias generates invalid SQL after expansion
This is an automated email from the ASF dual-hosted git repository.
jhyde 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 1245db6201 [CALCITE-6007] Sub-query that contains WITH and has no alias generates invalid SQL after expansion
1245db6201 is described below
commit 1245db620114ee5174641ad540af6b27dcd772e3
Author: wenrui <we...@singleorigin.tech>
AuthorDate: Fri Sep 22 22:56:53 2023 -0700
[CALCITE-6007] Sub-query that contains WITH and has no alias generates invalid SQL after expansion
Add Quidem test (Julian Hyde).
Close apache/calcite#3440
---
.../calcite/sql/validate/SqlValidatorImpl.java | 2 +-
.../org/apache/calcite/test/SqlValidatorTest.java | 19 +++++++++++++++
core/src/test/resources/sql/misc.iq | 28 ++++++++++++++++++++++
3 files changed, 48 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 83fbc51382..75eb3637ed 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
@@ -2350,7 +2350,7 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
case PIVOT:
case UNPIVOT:
case MATCH_RECOGNIZE:
-
+ case WITH:
// give this anonymous construct a name since later
// query processing stages rely on it
alias = SqlValidatorUtil.alias(node, nextGeneratedId++);
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 150dbdf785..8f0d1c002f 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -7993,6 +7993,25 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
+ "FROM `DEPT`");
}
+ /** Test case for
+ * <a href="https://issues.apache.org/jira/browse/CALCITE-6007">[CALCITE-6007]
+ * Sub-query that contains WITH and has no alias generates invalid SQL after
+ * expansion</a>. */
+ @Test void testSubQueryWithoutAlias() {
+ // Note the 'AS `EXPR$0`' in the rewritten form of each query.
+ // Before [CALCITE-6007] was fixed, that alias was missing.
+ sql("select a from (select 1 as a)")
+ .withValidatorIdentifierExpansion(true)
+ .rewritesTo("SELECT `EXPR$0`.`A`\n"
+ + "FROM (SELECT 1 AS `A`) AS `EXPR$0`");
+ sql("select a from (with sub as (select 1 as a) select a from sub)")
+ .withValidatorIdentifierExpansion(true)
+ .rewritesTo("SELECT `EXPR$0`.`A`\n"
+ + "FROM (WITH `SUB` AS (SELECT 1 AS `A`) "
+ + "SELECT `SUB`.`A`\n"
+ + "FROM `SUB` AS `SUB`) AS `EXPR$0`");
+ }
+
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1238">[CALCITE-1238]
* Unparsing LIMIT without ORDER BY after validation</a>. */
diff --git a/core/src/test/resources/sql/misc.iq b/core/src/test/resources/sql/misc.iq
index 65fe7f085d..9876587d19 100644
--- a/core/src/test/resources/sql/misc.iq
+++ b/core/src/test/resources/sql/misc.iq
@@ -796,6 +796,34 @@ having exists
!ok
+# [CALCITE-6007] Sub-query that contains WITH and has no alias generates invalid SQL after expansion
+SELECT a, b
+FROM (
+ WITH sub AS (SELECT 1 AS a, 2 AS b)
+ SELECT * FROM sub)
+WHERE a IS NOT NULL;
++---+---+
+| A | B |
++---+---+
+| 1 | 2 |
++---+---+
+(1 row)
+
+!ok
+
+# Similar pattern to previous, without WITH
+SELECT a, b
+FROM (SELECT 1 AS a, 2 AS b)
+WHERE a IS NOT NULL;
++---+---+
+| A | B |
++---+---+
+| 1 | 2 |
++---+---+
+(1 row)
+
+!ok
+
# [CALCITE-411] Duplicate aliases
select 1 as a, 2 as a from (values (true));
+---+---+