Skip to content

Commit 2eaa48a

Browse files
committed
error if recursive cte is referenced multiple times within the recursive term
1 parent 21e8a4e commit 2eaa48a

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

datafusion/physical-plan/src/recursive_query.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,19 @@ fn assign_work_table(
304304
plan: Arc<dyn ExecutionPlan>,
305305
work_table: Arc<WorkTable>,
306306
) -> Result<Arc<dyn ExecutionPlan>> {
307-
plan.transform_down(&|plan| {
307+
let mut work_table_refs = 0;
308+
plan.transform_down_mut(&mut |plan| {
308309
if let Some(exec) = plan.as_any().downcast_ref::<WorkTableExec>() {
309-
Ok(Transformed::Yes(Arc::new(
310-
exec.with_work_table(work_table.clone()),
311-
)))
310+
if work_table_refs > 0 {
311+
not_impl_err!(
312+
"Multiple recursive references to the same CTE are not supported"
313+
)
314+
} else {
315+
work_table_refs += 1;
316+
Ok(Transformed::Yes(Arc::new(
317+
exec.with_work_table(work_table.clone()),
318+
)))
319+
}
312320
} else if plan.as_any().is::<RecursiveQueryExec>() {
313321
not_impl_err!("Recursive queries cannot be nested")
314322
} else {

datafusion/sqllogictest/test_files/cte.slt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,14 @@ WITH RECURSIVE outer_cte AS (
619619
)
620620
)
621621
SELECT a FROM outer_cte;
622+
623+
# expect error when recursive CTE is referenced multiple times in the recursive term
624+
query error DataFusion error: This feature is not implemented: Multiple recursive references to the same CTE are not supported
625+
WITH RECURSIVE my_cte AS (
626+
SELECT 1 as a
627+
UNION ALL
628+
SELECT my_cte.a+2 as a
629+
FROM my_cte join my_cte c2 using(a)
630+
WHERE my_cte.a<5
631+
)
632+
SELECT a FROM my_cte;

0 commit comments

Comments
 (0)