Skip to content

Commit 1ddcd54

Browse files
committed
fix: the output columns of cte related columns lack the columns required by cte itself
Signed-off-by: Kould <[email protected]>
1 parent a0dc899 commit 1ddcd54

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/query/sql/src/executor/physical_plans/physical_union_all.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ impl PhysicalPlanBuilder {
6262
// 1. Prune unused Columns.
6363
let metadata = self.metadata.read().clone();
6464
let lazy_columns = metadata.lazy_columns();
65-
required.extend(lazy_columns.clone());
65+
required.extend(lazy_columns);
6666

6767
let indices: Vec<usize> = (0..union_all.left_outputs.len())
68-
.filter(|index| required.contains(&union_all.output_indexes[*index]))
68+
.filter(|index| {
69+
!union_all.cte_scan_names.is_empty()
70+
|| required.contains(&union_all.output_indexes[*index])
71+
})
6972
.collect();
7073

7174
let (left_required, right_required) = if indices.is_empty() {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ impl Binder {
6363
RelOperator::RecursiveCteScan(plan) => {
6464
cte_scan_names.push(plan.table_name.clone());
6565
if cte_types.is_empty() {
66-
cte_types.extend(
67-
plan.fields
68-
.iter()
69-
.map(|f| f.data_type().clone())
70-
.collect::<Vec<DataType>>(),
71-
);
66+
cte_types.extend(plan.fields.iter().map(|f| f.data_type().clone()));
7267
}
7368
}
7469

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,10 @@ with t(tt) as (select number as tt from numbers(10)), t2(tt) AS MATERIALIZED (S
578578

579579
# https://github.com/databendlabs/databend/issues/17295
580580
statement ok
581-
create table t1(a int);
581+
create or replace table t1(a int);
582582

583583
statement ok
584-
create table t2(a int);
584+
create or replace table t2(a int);
585585

586586
query I
587587
WITH RECURSIVE
@@ -591,3 +591,33 @@ UNION all SELECT a from t2, closure where t2.a = closure.x
591591
) SELECT x FROM closure;
592592
----
593593
1
594+
595+
# https://github.com/databendlabs/databend/issues/17432
596+
597+
statement ok
598+
CREATE OR REPLACE TABLE some_table (
599+
id INTEGER NOT NULL,
600+
"data" VARCHAR(50),
601+
parent_id INTEGER
602+
);
603+
604+
statement ok
605+
INSERT INTO some_table (id, "data", parent_id) VALUES (1, 'd1', NULL),(2, 'd2', 1),(3, 'd3', 1),(4, 'd4', 3),(5, 'd5', 3);
606+
607+
query T
608+
WITH RECURSIVE some_cte(id, "data", parent_id) AS
609+
(SELECT some_table.id AS id, some_table."data" AS "data", some_table.parent_id AS parent_id
610+
FROM some_table
611+
WHERE some_table."data" IN ('d2', 'd3', 'd4') UNION ALL SELECT some_table_1.id AS id, some_table_1."data" AS "data", some_table_1.parent_id AS parent_id
612+
FROM some_table AS some_table_1, some_cte AS c1
613+
WHERE some_table_1.id = c1.parent_id)
614+
SELECT some_cte."data"
615+
FROM some_cte
616+
WHERE some_cte."data" != 'd2';
617+
----
618+
d1
619+
d1
620+
d1
621+
d3
622+
d3
623+
d4

0 commit comments

Comments
 (0)