Apache DataFusion SQL Query Engine
Add recursive protection on planner's `create_physical_expr` (#19299)
Renan GEHAN9a9e4dd5c9f9
diff --git a/Cargo.lock b/Cargo.lockindex 866c4a34c0..663c64bf8a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2502,6 +2502,7 @@ dependencies = ["paste","petgraph 0.8.3","rand 0.9.2",+ "recursive","rstest","tokio",]diff --git a/datafusion/core/Cargo.toml b/datafusion/core/Cargo.tomlindex f584fea0b5..bd88ed3b9c 100644--- a/datafusion/core/Cargo.toml+++ b/datafusion/core/Cargo.toml@@ -87,6 +87,7 @@ recursive_protection = ["datafusion-expr/recursive_protection","datafusion-optimizer/recursive_protection","datafusion-physical-optimizer/recursive_protection",+ "datafusion-physical-expr/recursive_protection","datafusion-sql/recursive_protection","sqlparser/recursive-protection",]diff --git a/datafusion/physical-expr/Cargo.toml b/datafusion/physical-expr/Cargo.tomlindex 772a1f8f4a..1b23beeaa3 100644--- a/datafusion/physical-expr/Cargo.toml+++ b/datafusion/physical-expr/Cargo.toml@@ -40,6 +40,9 @@ workspace = true[lib]name = "datafusion_physical_expr"+[features]+recursive_protection = ["dep:recursive"]+[dependencies]ahash = { workspace = true }arrow = { workspace = true }@@ -54,6 +57,7 @@ itertools = { workspace = true, features = ["use_std"] }parking_lot = { workspace = true }paste = { workspace = true }petgraph = "0.8.3"+recursive = { workspace = true, optional = true }tokio = { workspace = true }half = { workspace = true }diff --git a/datafusion/physical-expr/src/planner.rs b/datafusion/physical-expr/src/planner.rsindex 3323ea029c..84a6aa4309 100644--- a/datafusion/physical-expr/src/planner.rs+++ b/datafusion/physical-expr/src/planner.rs@@ -105,6 +105,7 @@ use datafusion_expr::{/// * `e` - The logical expression/// * `input_dfschema` - The DataFusion schema for the input, used to resolve `Column` references/// to qualified or unqualified fields by name.+#[cfg_attr(feature = "recursive_protection", recursive::recursive)]pub fn create_physical_expr(e: &Expr,input_dfschema: &DFSchema,@@ -417,7 +418,7 @@ mod tests {use arrow::array::{ArrayRef, BooleanArray, RecordBatch, StringArray};use arrow::datatypes::{DataType, Field};- use datafusion_expr::{col, lit};+ use datafusion_expr::{Operator, col, lit};use super::*;@@ -445,4 +446,34 @@ mod tests {Ok(())}++ /// Test that deeply nested expressions do not cause a stack overflow.+ ///+ /// This test only runs when the `recursive_protection` feature is enabled,+ /// as it would overflow the stack otherwise.+ #[test]+ #[cfg_attr(not(feature = "recursive_protection"), ignore)]+ fn test_deeply_nested_binary_expr() -> Result<()> {+ // Create a deeply nested binary expression tree: ((((a + a) + a) + a) + ... )+ // With 1000 levels of nesting, this would overflow the stack without recursion protection.+ let depth = 1000;++ let mut expr = col("a");+ for _ in 0..depth {+ expr = Expr::BinaryExpr(BinaryExpr {+ left: Box::new(expr),+ op: Operator::Plus,+ right: Box::new(col("a")),+ });+ }++ let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);+ let df_schema = DFSchema::try_from(schema)?;++ // This should not stack overflow+ let _physical_expr =+ create_physical_expr(&expr, &df_schema, &ExecutionProps::new())?;++ Ok(())+ }}