Skip to content

Commit

Permalink
fix(binder): multiple unions should retain the same cte_to_relation c…
Browse files Browse the repository at this point in the history
…ontext (#9007)
  • Loading branch information
lmatz authored Apr 5, 2023
1 parent be4f748 commit 110972a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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;
Ok(BoundSetExpr::SetOperation {
op: BoundSetOperation::Union,
all,
Expand Down

0 comments on commit 110972a

Please sign in to comment.