Skip to content

Commit

Permalink
fix: filter push down union in r cte (#17031)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 authored Dec 12, 2024
1 parent 8501a27 commit 78b9915
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ impl Rule for RulePushDownFilterUnion {
let filter: Filter = s_expr.plan().clone().try_into()?;
let union_s_expr = s_expr.child(0)?;
let union: UnionAll = union_s_expr.plan().clone().try_into()?;
if !union.cte_scan_names.is_empty() {
// If the union has cte scan names, it's not allowed to push down filter.
state.add_result(s_expr.clone());
return Ok(());
}

// Create a filter which matches union's right child.
let index_pairs: HashMap<IndexType, IndexType> = union
Expand Down
40 changes: 40 additions & 0 deletions tests/sqllogictests/suites/query/cte/r_cte_union_all.test
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,43 @@ SELECT * FROM t;
7
8
9

# bug: https://github.com/databendlabs/databend/issues/17027
statement ok
CREATE or replace TABLE parent_child
(
parent VARCHAR(30),
child VARCHAR(30)
);

statement ok
INSERT INTO parent_child
VALUES ('Org','Org'),('Org','Global'),('Global','North'),
('Global','South'),('Global','East'),('Global','West'),
('Global','Org detail'),('North','North East'),('North','North West');

query I
WITH RECURSIVE tree_values
(parent, child)
AS (
SELECT parent, child
FROM parent_child
WHERE parent = child
UNION ALL
SELECT c.parent, c.child
FROM parent_child c
INNER JOIN tree_values p
ON p.child = c.parent
WHERE c.parent != c.child
)
select parent, child from tree_values
where parent = 'Global';
----
Global East
Global North
Global Org detail
Global South
Global West

statement ok
drop table parent_child;

0 comments on commit 78b9915

Please sign in to comment.