Skip to content

Apache DataFusion SQL Query Engine

Reject recursive CTEs before processing the sub-expressions (#3714)

Batuhan Taskaya27f3e90c975d
diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs
index a872e161b5..a5374cb4a1 100644
--- a/datafusion/sql/src/planner.rs
+++ b/datafusion/sql/src/planner.rs
@@ -354,6 +354,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
if let Some(with) = query.with {
// Process CTEs from top to bottom
// do not allow self-references
+ if with.recursive {
+ return Err(DataFusionError::NotImplemented(
+ "Recursive CTEs are not supported".to_string(),
+ ));
+ }
+
for cte in with.cte_tables {
// A `WITH` block can't use the same name more than once
let cte_name = normalize_ident(&cte.alias.name);
@@ -3530,6 +3536,22 @@ mod tests {
);
}
+ #[test]
+ fn recursive_ctes() {
+ let sql = "
+ WITH RECURSIVE numbers AS (
+ select 1 as n
+ UNION ALL
+ select n + 1 FROM numbers WHERE N < 10
+ )
+ select * from numbers;";
+ let err = logical_plan(sql).expect_err("query should have failed");
+ assert_eq!(
+ r#"NotImplemented("Recursive CTEs are not supported")"#,
+ format!("{:?}", err)
+ );
+ }
+
#[test]
fn select_array_non_literal_type() {
let sql = "SELECT [now()]";