Skip to content

Commit

Permalink
[CALCITE-6007] Sub-query that contains WITH and has no alias generate…
Browse files Browse the repository at this point in the history
…s invalid SQL after expansion

Add Quidem test (Julian Hyde).

Close apache#3440
  • Loading branch information
wenruimeng authored and julianhyde committed Sep 27, 2023
1 parent aef9bdb commit 26f5d4a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2350,7 +2350,7 @@ private SqlNode registerFrom(
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++);
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7993,6 +7993,25 @@ void testGroupExpressionEquivalenceParams() {
+ "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>. */
Expand Down
57 changes: 57 additions & 0 deletions core/src/test/resources/sql/misc.iq
Original file line number Diff line number Diff line change
Expand Up @@ -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));
+---+---+
Expand Down Expand Up @@ -2555,4 +2583,33 @@ select decimal'12.3' + 5.6;

!ok


# [CALCITE-6007] Subquery that contains WITH and has no alias throws
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

# End misc.iq

0 comments on commit 26f5d4a

Please sign in to comment.