From 205be55d2a5e14c1fce4ff1c8a1f985b7753b223 Mon Sep 17 00:00:00 2001 From: lmatz Date: Wed, 5 Apr 2023 15:23:52 +0800 Subject: [PATCH 1/2] fix(binder): multiple unions should retain the same cte_to_relation context --- src/frontend/src/binder/set_expr.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/frontend/src/binder/set_expr.rs b/src/frontend/src/binder/set_expr.rs index dd1d646ee3da..421ae8a042d7 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, From 53251ddc6ccbd3061a89c3597d62c74251e5165f Mon Sep 17 00:00:00 2001 From: lmatz Date: Wed, 5 Apr 2023 15:29:45 +0800 Subject: [PATCH 2/2] add e2e test --- e2e_test/batch/basic/cte.slt.part | 7 +++++++ 1 file changed, 7 insertions(+) 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;