Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(binder): multiple unions should retain the same cte_to_relation context #9007

Merged
merged 3 commits into from
Apr 5, 2023
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
7 changes: 7 additions & 0 deletions e2e_test/batch/basic/cte.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ with c as (values(1)) select * from (with c as (values(2)) select * from c) foo
1
2

query I rowsort
with table1 as ( select 1 as n), table2 as ( select 2 as n), table3 as ( select 3 as n) select * from table1 union select * from table2 union select * from table3;
----
1
2
3

statement ok
drop table t1;

Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/binder/set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Binder {
let left = Box::new(self.bind_set_expr(*left)?);
// Reset context for right side, but keep `cte_to_relation`.
let new_context = std::mem::take(&mut self.context);
self.context.cte_to_relation = new_context.cte_to_relation;
self.context.cte_to_relation = new_context.cte_to_relation.clone();
let right = Box::new(self.bind_set_expr(*right)?);

if left.schema().fields.len() != right.schema().fields.len() {
Expand Down Expand Up @@ -160,6 +160,7 @@ impl Binder {
// select a from t2 union all select b from t2 order by a+1; should throw an
// error.
self.context = BindContext::default();
self.context.cte_to_relation = new_context.cte_to_relation;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since CTE can only be defined at the top level, I suppose I can just retain the cte_to_relation without concern?

Ok(BoundSetExpr::SetOperation {
op: BoundSetOperation::Union,
all,
Expand Down