Skip to content

Commit 78b9915

Browse files
authored
fix: filter push down union in r cte (#17031)
1 parent 8501a27 commit 78b9915

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/query/sql/src/planner/optimizer/rule/rewrite/rule_push_down_filter_union.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ impl Rule for RulePushDownFilterUnion {
7272
let filter: Filter = s_expr.plan().clone().try_into()?;
7373
let union_s_expr = s_expr.child(0)?;
7474
let union: UnionAll = union_s_expr.plan().clone().try_into()?;
75+
if !union.cte_scan_names.is_empty() {
76+
// If the union has cte scan names, it's not allowed to push down filter.
77+
state.add_result(s_expr.clone());
78+
return Ok(());
79+
}
7580

7681
// Create a filter which matches union's right child.
7782
let index_pairs: HashMap<IndexType, IndexType> = union

tests/sqllogictests/suites/query/cte/r_cte_union_all.test

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,43 @@ SELECT * FROM t;
7676
7
7777
8
7878
9
79+
80+
# bug: https://github.com/databendlabs/databend/issues/17027
81+
statement ok
82+
CREATE or replace TABLE parent_child
83+
(
84+
parent VARCHAR(30),
85+
child VARCHAR(30)
86+
);
87+
88+
statement ok
89+
INSERT INTO parent_child
90+
VALUES ('Org','Org'),('Org','Global'),('Global','North'),
91+
('Global','South'),('Global','East'),('Global','West'),
92+
('Global','Org detail'),('North','North East'),('North','North West');
93+
94+
query I
95+
WITH RECURSIVE tree_values
96+
(parent, child)
97+
AS (
98+
SELECT parent, child
99+
FROM parent_child
100+
WHERE parent = child
101+
UNION ALL
102+
SELECT c.parent, c.child
103+
FROM parent_child c
104+
INNER JOIN tree_values p
105+
ON p.child = c.parent
106+
WHERE c.parent != c.child
107+
)
108+
select parent, child from tree_values
109+
where parent = 'Global';
110+
----
111+
Global East
112+
Global North
113+
Global Org detail
114+
Global South
115+
Global West
116+
117+
statement ok
118+
drop table parent_child;

0 commit comments

Comments
 (0)