Skip to content

Commit 6613966

Browse files
authored
fix: with recursive bind table panic (#17554)
Signed-off-by: Kould <[email protected]>
1 parent 64bd0ec commit 6613966

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/query/service/src/pipelines/processors/transforms/transform_recursive_cte_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ async fn create_memory_table_for_cte_scan(
290290
let schema = TableSchemaRefExt::create(table_fields);
291291

292292
let create_table_plan = CreateTablePlan {
293-
create_option: CreateOption::Create,
293+
create_option: CreateOption::CreateIfNotExists,
294294
tenant: Tenant {
295295
tenant: ctx.get_tenant().tenant,
296296
},

src/query/sql/src/planner/binder/bind_query/bind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Binder {
5555

5656
// Bind query body.
5757
let (mut s_expr, mut bind_context) =
58-
self.bind_set_expr(bind_context, &query.body, &query.order_by, limit)?;
58+
self.bind_set_expr(bind_context, &query.body, &query.order_by, limit, None)?;
5959

6060
// Bind order by for `SetOperation` and `Values`.
6161
s_expr = self.bind_query_order_by(&mut bind_context, query, s_expr)?;

src/query/sql/src/planner/binder/bind_query/bind_set_expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Binder {
2727
set_expr: &SetExpr,
2828
order_by: &[OrderByExpr],
2929
limit: Option<usize>,
30+
cte_name: Option<String>,
3031
) -> Result<(SExpr, BindContext)> {
3132
match set_expr {
3233
SetExpr::Select(stmt) => self.bind_select(bind_context, stmt, order_by, limit),
@@ -37,7 +38,7 @@ impl Binder {
3738
&set_operation.right,
3839
&set_operation.op,
3940
&set_operation.all,
40-
None,
41+
cte_name,
4142
),
4243
SetExpr::Values { span, values } => self.bind_values(bind_context, *span, values),
4344
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ impl Binder {
119119
all: &bool,
120120
cte_name: Option<String>,
121121
) -> Result<(SExpr, BindContext)> {
122-
let (left_expr, left_bind_context) = self.bind_set_expr(bind_context, left, &[], None)?;
122+
let (left_expr, left_bind_context) =
123+
self.bind_set_expr(bind_context, left, &[], None, cte_name.clone())?;
123124
if let Some(cte_name) = cte_name.as_ref() {
124125
if !all {
125126
return Err(ErrorCode::Internal(
@@ -147,7 +148,7 @@ impl Binder {
147148
.cte_context
148149
.merge(left_bind_context.cte_context.clone());
149150
let (right_expr, right_bind_context) =
150-
self.bind_set_expr(bind_context, right, &[], None)?;
151+
self.bind_set_expr(bind_context, right, &[], None, None)?;
151152

152153
if left_bind_context.columns.len() != right_bind_context.columns.len() {
153154
return Err(ErrorCode::SemanticError(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,19 @@ with t(tt) as (select number as tt from numbers(10)), t2(tt) AS MATERIALIZED (S
575575
7
576576
8
577577
9
578+
579+
# https://github.com/databendlabs/databend/issues/17295
580+
statement ok
581+
create table t1(a int);
582+
583+
statement ok
584+
create table t2(a int);
585+
586+
query I
587+
WITH RECURSIVE
588+
closure(x) AS (VALUES(1)
589+
UNION all SELECT a from t1, closure where t1.a = closure.x
590+
UNION all SELECT a from t2, closure where t2.a = closure.x
591+
) SELECT x FROM closure;
592+
----
593+
1

0 commit comments

Comments
 (0)