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

xiong 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 28e0ab23a3 [CALCITE-6303] UNION with CTE(s) results in exception 
during query validation
28e0ab23a3 is described below

commit 28e0ab23a32b70a8da27b7db472527d8265bcf43
Author: Xiong Duan <[email protected]>
AuthorDate: Wed Jan 22 20:02:26 2025 +0800

    [CALCITE-6303] UNION with CTE(s) results in exception during query 
validation
---
 core/src/main/java/org/apache/calcite/rel/core/SetOp.java   |  6 +++---
 .../org/apache/calcite/sql/validate/SqlValidatorImpl.java   | 11 +++++++++++
 core/src/test/resources/sql/set-op.iq                       | 13 +++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rel/core/SetOp.java 
b/core/src/main/java/org/apache/calcite/rel/core/SetOp.java
index 45a3febb32..04e1409e78 100644
--- a/core/src/main/java/org/apache/calcite/rel/core/SetOp.java
+++ b/core/src/main/java/org/apache/calcite/rel/core/SetOp.java
@@ -38,6 +38,8 @@
 
 import static com.google.common.base.Preconditions.checkArgument;
 
+import static org.apache.calcite.sql.SqlKind.SET_QUERY;
+
 /**
  * <code>SetOp</code> is an abstract base for relational set operators such
  * as UNION, MINUS (aka EXCEPT), and INTERSECT.
@@ -58,9 +60,7 @@ public abstract class SetOp extends AbstractRelNode 
implements Hintable {
   protected SetOp(RelOptCluster cluster, RelTraitSet traits, List<RelHint> 
hints,
       List<RelNode> inputs, SqlKind kind, boolean all) {
     super(cluster, traits);
-    checkArgument(kind == SqlKind.UNION
-        || kind == SqlKind.INTERSECT
-        || kind == SqlKind.EXCEPT);
+    checkArgument(SET_QUERY.contains(kind));
     this.kind = kind;
     this.inputs = ImmutableList.copyOf(inputs);
     this.all = all;
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 dbf3d26863..841b992c1c 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
@@ -4431,6 +4431,15 @@ private void validateModality(SqlNode query) {
       default:
         break;
       }
+    } else if (query.getKind() == SqlKind.WITH) {
+     // The modality of WITH clause depends on its body
+     // For example:
+     // SQL: WITH STREAMTABLE AS (SELECT STREAM * FROM KAFKA.MOCKTABLE) SELECT 
* FROM STREAMTABLE
+     // The modality should be RELATION.
+     // SQL: WITH STREAMTABLE AS (SELECT STREAM * FROM KAFKA.MOCKTABLE)
+     //      SELECT STREAM * FROM STREAMTABLE
+     // The modality should be STREAM.
+      validateModality(((SqlWith) query).body);
     } else {
       assert query.isA(SqlKind.SET_QUERY);
       final SqlCall call = (SqlCall) query;
@@ -4453,6 +4462,8 @@ private static SqlModality deduceModality(SqlNode query) {
           : SqlModality.RELATION;
     } else if (query.getKind() == SqlKind.VALUES) {
       return SqlModality.RELATION;
+    } else if (query.getKind() == SqlKind.WITH) {
+      return deduceModality(((SqlWith) query).body);
     } else {
       assert query.isA(SqlKind.SET_QUERY);
       final SqlCall call = (SqlCall) query;
diff --git a/core/src/test/resources/sql/set-op.iq 
b/core/src/test/resources/sql/set-op.iq
index c5e277fe9d..3e6c56b1d2 100644
--- a/core/src/test/resources/sql/set-op.iq
+++ b/core/src/test/resources/sql/set-op.iq
@@ -245,4 +245,17 @@ select * from emp natural join dept where job = 'CLERK';
 Non-query expression encountered in illegal context
 !error
 
+# [CALCITE-6303] UNION with CTE(s) results in exception during query validation
+(SELECT 123)
+UNION
+(WITH t (col) AS (VALUES (456)) SELECT col FROM t);
++--------+
+| EXPR$0 |
++--------+
+|    123 |
+|    456 |
++--------+
+(2 rows)
+
+!ok
 # End set-op.iq

Reply via email to