Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/main/java/org/apache/calcite/rel/core/SetOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came up with an execution plan based on SQL. The first SQL generated plan is a regular TableScan, and the second SQl generated plan is a Stream TableScan. So I think it is up to its body.

} else {
assert query.isA(SqlKind.SET_QUERY);
final SqlCall call = (SqlCall) query;
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/resources/sql/set-op.iq
Original file line number Diff line number Diff line change
Expand Up @@ -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