diff --git a/e2e_test/batch/basic/cte.slt.part b/e2e_test/batch/basic/cte.slt.part index 8504d9cc7ffe..8d1858ac0cd3 100644 --- a/e2e_test/batch/basic/cte.slt.part +++ b/e2e_test/batch/basic/cte.slt.part @@ -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; diff --git a/src/frontend/src/binder/set_expr.rs b/src/frontend/src/binder/set_expr.rs index 5696e90a0b7d..5f2600fbeffd 100644 --- a/src/frontend/src/binder/set_expr.rs +++ b/src/frontend/src/binder/set_expr.rs @@ -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() { @@ -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,