Skip to content

Commit 1b4d6b6

Browse files
authored
fix(bind): parent BindContext missing when binding union (#18097)
union loss parent ctx
1 parent cf020fa commit 1b4d6b6

File tree

3 files changed

+36
-44
lines changed

3 files changed

+36
-44
lines changed

src/query/sql/src/planner/binder/select.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ impl Binder {
157157
));
158158
}
159159

160-
match (op, all) {
161-
(SetOperator::Intersect, false) => {
160+
match op {
161+
SetOperator::Intersect if !all => {
162162
// Transfer Intersect to Semi join
163163
self.bind_intersect(
164164
left.span(),
@@ -169,7 +169,7 @@ impl Binder {
169169
right_expr,
170170
)
171171
}
172-
(SetOperator::Except, false) => {
172+
SetOperator::Except if !all => {
173173
// Transfer Except to Anti join
174174
self.bind_except(
175175
left.span(),
@@ -180,24 +180,15 @@ impl Binder {
180180
right_expr,
181181
)
182182
}
183-
(SetOperator::Union, true) => self.bind_union(
183+
SetOperator::Union => self.bind_union(
184184
left.span(),
185185
right.span(),
186-
left_bind_context,
187-
right_bind_context,
186+
Some(bind_context),
187+
&left_bind_context,
188+
&right_bind_context,
188189
left_expr,
189190
right_expr,
190-
false,
191-
cte_name,
192-
),
193-
(SetOperator::Union, false) => self.bind_union(
194-
left.span(),
195-
right.span(),
196-
left_bind_context,
197-
right_bind_context,
198-
left_expr,
199-
right_expr,
200-
true,
191+
!all,
201192
cte_name,
202193
),
203194
_ => Err(ErrorCode::Unimplemented(
@@ -211,8 +202,9 @@ impl Binder {
211202
&mut self,
212203
left_span: Span,
213204
right_span: Span,
214-
left_context: BindContext,
215-
right_context: BindContext,
205+
parent_context: Option<&BindContext>,
206+
left_context: &BindContext,
207+
right_context: &BindContext,
216208
left_expr: SExpr,
217209
right_expr: SExpr,
218210
distinct: bool,
@@ -258,8 +250,9 @@ impl Binder {
258250
let (mut new_bind_context, left_outputs, right_outputs) = self.coercion_union_type(
259251
left_span,
260252
right_span,
253+
parent_context,
261254
left_context,
262-
right_context.clone(),
255+
right_context,
263256
coercion_types,
264257
)?;
265258

@@ -418,8 +411,9 @@ impl Binder {
418411
&mut self,
419412
left_span: Span,
420413
right_span: Span,
421-
left_bind_context: BindContext,
422-
right_bind_context: BindContext,
414+
parent_context: Option<&BindContext>,
415+
left_bind_context: &BindContext,
416+
right_bind_context: &BindContext,
423417
coercion_types: Vec<DataType>,
424418
) -> Result<(
425419
BindContext,
@@ -428,11 +422,11 @@ impl Binder {
428422
)> {
429423
let mut left_outputs = Vec::with_capacity(left_bind_context.columns.len());
430424
let mut right_outputs = Vec::with_capacity(right_bind_context.columns.len());
431-
let mut new_bind_context = BindContext::new();
425+
let mut new_bind_context = BindContext::with_opt_parent(parent_context)?;
432426

433427
new_bind_context
434428
.cte_context
435-
.set_cte_context(right_bind_context.cte_context);
429+
.set_cte_context(right_bind_context.cte_context.clone());
436430

437431
for (idx, (left_col, right_col)) in left_bind_context
438432
.columns

src/query/sql/src/planner/dataframe.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,9 @@ impl Dataframe {
499499
let (s_expr, bind_context) = self.binder.bind_union(
500500
None,
501501
None,
502-
self.bind_context,
503-
dataframe.bind_context,
502+
None,
503+
&self.bind_context,
504+
&dataframe.bind_context,
504505
self.s_expr,
505506
dataframe.s_expr,
506507
false,
@@ -515,8 +516,9 @@ impl Dataframe {
515516
let (s_expr, bind_context) = self.binder.bind_union(
516517
None,
517518
None,
518-
self.bind_context,
519-
dataframe.bind_context,
519+
None,
520+
&self.bind_context,
521+
&dataframe.bind_context,
520522
self.s_expr,
521523
dataframe.s_expr,
522524
true,

tests/sqllogictests/suites/query/union.test

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@ statement ok
22
use default
33

44
statement ok
5-
DROP TABLE IF EXISTS data2013
5+
CREATE OR REPLACE TABLE data2013 (name String, value UInt32)
66

77
statement ok
8-
DROP TABLE IF EXISTS data2014
8+
CREATE OR REPLACE TABLE data2014 (name String, value UInt32)
99

1010
statement ok
11-
DROP TABLE IF EXISTS data2015
11+
CREATE OR REPLACE TABLE data2015 (data_name String, data_value UInt32)
1212

1313
statement ok
14-
DROP TABLE IF EXISTS data2016
15-
16-
statement ok
17-
CREATE TABLE data2013 (name String, value UInt32)
18-
19-
statement ok
20-
CREATE TABLE data2014 (name String, value UInt32)
21-
22-
statement ok
23-
CREATE TABLE data2015 (data_name String, data_value UInt32)
24-
25-
statement ok
26-
CREATE TABLE data2016 (name String, value UInt32)
14+
CREATE OR REPLACE TABLE data2016 (name String, value UInt32)
2715

2816
statement ok
2917
INSERT INTO data2013(name,value) VALUES('Alice', 1000), ('Bob', 2000), ('Carol', 5000)
@@ -396,6 +384,14 @@ WITH cte1 AS ( SELECT t1.test FROM t1 WHERE t1.test = 0 ) ,cte2 AS ( SELECT cte1
396384
----
397385
0
398386

387+
query I
388+
select * from (select 1 n) t2
389+
where exists (
390+
select 1 from (select * from numbers(5) union select * from numbers(5)) t where t.number = t2.n
391+
);
392+
----
393+
1
394+
399395
statement ok
400396
drop table t1;
401397

0 commit comments

Comments
 (0)