Skip to content

Commit f835a85

Browse files
sundy-liBohuTANGCopilot
authored
fix(query): reuse the left's column binding in union as possible (#18053)
* chore(query): reuse the left's column binding in union asas possible * chore(query): reuse the left's column binding in union asas possible * chore(query): reuse the left's column binding in union asas possible * Update tests/sqllogictests/suites/query/union.test Co-authored-by: Copilot <[email protected]> * chore(query): reuse the left's column binding in union asas possible * chore(query): reuse the left's column binding in union asas possible * chore(query): reuse the left's column binding in union asas possible --------- Co-authored-by: Bohu <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 6c04d86 commit f835a85

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/query/sql/src/planner/optimizer/optimizers/rule/union_rules/rule_eliminate_union.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ impl Rule for RuleEliminateUnion {
7272

7373
fn apply(&self, s_expr: &SExpr, state: &mut TransformResult) -> Result<()> {
7474
let union: UnionAll = s_expr.plan().clone().try_into()?;
75+
76+
// Need to check that union's output indexes are the same as left child's output indexes
77+
// currently this is always !false now, so the following codes are not necessary
78+
if !union
79+
.left_outputs
80+
.iter()
81+
.all(|(idx, _)| union.output_indexes.contains(idx))
82+
{
83+
return Ok(());
84+
}
85+
7586
let left_child = s_expr.child(0)?;
7687
let right_child = s_expr.child(1)?;
7788

tests/sqllogictests/suites/mode/standalone/explain/union.test

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ settings (ddl_column_type_nullable=0) create table t2 as select number as b from
269269
query T
270270
explain select * from t1 where t1.a < 0 union all select a from t2 join t1 on t1.a = t2.b where a <0;
271271
----
272-
EmptyResultScan
272+
UnionAll
273+
├── output columns: [a (#3)]
274+
├── estimated rows: 0.00
275+
├── EmptyResultScan
276+
└── EmptyResultScan
273277

274278

275279
query T
@@ -295,21 +299,29 @@ UnionAll
295299
query T
296300
explain select * from t1 union all select * from t2 where t2.b < 0;
297301
----
298-
TableScan
299-
├── table: default.default.t1
300-
├── output columns: []
301-
├── read rows: 10000
302-
├── read size: 0
303-
├── partitions total: 1
304-
├── partitions scanned: 1
305-
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
306-
├── push downs: [filters: [], limit: NONE]
307-
└── estimated rows: 10000.00
302+
UnionAll
303+
├── output columns: [a (#2)]
304+
├── estimated rows: 10000.00
305+
├── TableScan
306+
│ ├── table: default.default.t1
307+
│ ├── output columns: [a (#0)]
308+
│ ├── read rows: 10000
309+
│ ├── read size: 10.59 KiB
310+
│ ├── partitions total: 1
311+
│ ├── partitions scanned: 1
312+
│ ├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
313+
│ ├── push downs: [filters: [], limit: NONE]
314+
│ └── estimated rows: 10000.00
315+
└── EmptyResultScan
308316

309317
query T
310318
explain select * from t1 where t1.a < 0 union all select * from t2 where t2.b < 0;
311319
----
312-
EmptyResultScan
320+
UnionAll
321+
├── output columns: [a (#2)]
322+
├── estimated rows: 0.00
323+
├── EmptyResultScan
324+
└── EmptyResultScan
313325

314326
statement ok
315327
drop table t1;

tests/sqllogictests/suites/query/union.test

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,19 @@ statement ok
346346
unset max_set_operator_count;
347347

348348
query I
349-
select * from numbers(100) union all select * from numbers(100) ignore_result;
349+
select sum(number) as number, number* 3 as t from numbers(100) group by t union all select number, number* 3 as t from numbers(1000) where number < 0 ignore_result;
350+
----
351+
352+
query I
353+
select t, sum(number) as number from (select sum(number) as number, number* 3 as t from numbers(100) group by t union all select number, number* 3 as t from numbers(1000) where number < 0) group by t ignore_result;
354+
----
355+
356+
query I
357+
select number::int32 from numbers(100) union all select number::int from numbers(100) where number < 0 ignore_result;
358+
----
359+
360+
query I
361+
select number::int32 from numbers(100) union all select number::int from numbers(100) where number > 0 ignore_result;
350362
----
351363

352364
statement ok

0 commit comments

Comments
 (0)