Apache DataFusion SQL Query Engine
Refactor PhysicalExprSimplfier to &self instead of &mut self (#19212)
Adrian Garcia Badaraccof2b476f390d0
diff --git a/datafusion/physical-expr/src/simplifier/mod.rs b/datafusion/physical-expr/src/simplifier/mod.rsindex bb7ec4d1e0..97395f4fe8 100644--- a/datafusion/physical-expr/src/simplifier/mod.rs+++ b/datafusion/physical-expr/src/simplifier/mod.rs@@ -18,10 +18,7 @@//! Simplifier for Physical Expressionsuse arrow::datatypes::Schema;-use datafusion_common::{- Result,- tree_node::{Transformed, TreeNode, TreeNodeRewriter},-};+use datafusion_common::{Result, tree_node::TreeNode};use std::sync::Arc;use crate::{PhysicalExpr, simplifier::not::simplify_not_expr};@@ -48,15 +45,35 @@ impl<'a> PhysicalExprSimplifier<'a> {}/// Simplify a physical expression- pub fn simplify(- &mut self,- expr: Arc<dyn PhysicalExpr>,- ) -> Result<Arc<dyn PhysicalExpr>> {+ pub fn simplify(&self, expr: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> {let mut current_expr = expr;let mut count = 0;+ let schema = self.schema;+while count < MAX_LOOP_COUNT {count += 1;- let result = current_expr.rewrite(self)?;+ let result = current_expr.transform(|node| {+ #[cfg(test)]+ let original_type = node.data_type(schema).unwrap();++ // Apply NOT expression simplification first, then unwrap cast optimization,+ // then constant expression evaluation+ let rewritten = simplify_not_expr(&node, schema)?+ .transform_data(|node| {+ unwrap_cast::unwrap_cast_in_comparison(node, schema)+ })?+ .transform_data(|node| const_evaluator::simplify_const_expr(&node))?;++ #[cfg(test)]+ assert_eq!(+ rewritten.data.data_type(schema).unwrap(),+ original_type,+ "Simplified expression should have the same data type as the original"+ );++ Ok(rewritten)+ })?;+if !result.transformed {return Ok(result.data);}@@ -66,32 +83,6 @@ impl<'a> PhysicalExprSimplifier<'a> {}}-impl<'a> TreeNodeRewriter for PhysicalExprSimplifier<'a> {- type Node = Arc<dyn PhysicalExpr>;-- fn f_up(&mut self, node: Self::Node) -> Result<Transformed<Self::Node>> {- #[cfg(test)]- let original_type = node.data_type(self.schema).unwrap();-- // Apply NOT expression simplification first, then unwrap cast optimization,- // then constant expression evaluation- let rewritten = simplify_not_expr(&node, self.schema)?- .transform_data(|node| {- unwrap_cast::unwrap_cast_in_comparison(node, self.schema)- })?- .transform_data(|node| const_evaluator::simplify_const_expr(&node))?;-- #[cfg(test)]- assert_eq!(- rewritten.data.data_type(self.schema).unwrap(),- original_type,- "Simplified expression should have the same data type as the original"- );-- Ok(rewritten)- }-}-#[cfg(test)]mod tests {use super::*;@@ -134,7 +125,7 @@ mod tests {/// Assert that simplifying `input` produces `expected`fn assert_not_simplify(- simplifier: &mut PhysicalExprSimplifier,+ simplifier: &PhysicalExprSimplifier,input: Arc<dyn PhysicalExpr>,expected: Arc<dyn PhysicalExpr>,) {@@ -148,7 +139,7 @@ mod tests {#[test]fn test_simplify() {let schema = test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// Create: cast(c2 as INT32) != INT32(99)let column_expr = col("c2", &schema).unwrap();@@ -175,7 +166,7 @@ mod tests {#[test]fn test_nested_expression_simplification() {let schema = test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// Create nested expression: (cast(c1 as INT64) > INT64(5)) OR (cast(c2 as INT32) <= INT32(10))let c1_expr = col("c1", &schema).unwrap();@@ -228,7 +219,7 @@ mod tests {#[test]fn test_double_negation_elimination() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(NOT(c > 5)) -> c > 5let inner_expr: Arc<dyn PhysicalExpr> = Arc::new(BinaryExpr::new(@@ -240,24 +231,24 @@ mod tests {let double_not: Arc<dyn PhysicalExpr> = Arc::new(NotExpr::new(inner_not));let expected = inner_expr;- assert_not_simplify(&mut simplifier, double_not, expected);+ assert_not_simplify(&simplifier, double_not, expected);Ok(())}#[test]fn test_not_literal() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(TRUE) -> FALSElet not_true = Arc::new(NotExpr::new(lit(ScalarValue::Boolean(Some(true)))));let expected = lit(ScalarValue::Boolean(Some(false)));- assert_not_simplify(&mut simplifier, not_true, expected);+ assert_not_simplify(&simplifier, not_true, expected);// NOT(FALSE) -> TRUElet not_false = Arc::new(NotExpr::new(lit(ScalarValue::Boolean(Some(false)))));let expected = lit(ScalarValue::Boolean(Some(true)));- assert_not_simplify(&mut simplifier, not_false, expected);+ assert_not_simplify(&simplifier, not_false, expected);Ok(())}@@ -265,7 +256,7 @@ mod tests {#[test]fn test_negate_comparison() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(c = 5) -> c != 5let not_eq = Arc::new(NotExpr::new(Arc::new(BinaryExpr::new(@@ -278,7 +269,7 @@ mod tests {Operator::NotEq,lit(ScalarValue::Int32(Some(5))),));- assert_not_simplify(&mut simplifier, not_eq, expected);+ assert_not_simplify(&simplifier, not_eq, expected);Ok(())}@@ -286,7 +277,7 @@ mod tests {#[test]fn test_demorgans_law_and() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(a AND b) -> NOT a OR NOT blet and_expr = Arc::new(BinaryExpr::new(@@ -301,7 +292,7 @@ mod tests {Operator::Or,Arc::new(NotExpr::new(col("b", &schema)?)),));- assert_not_simplify(&mut simplifier, not_and, expected);+ assert_not_simplify(&simplifier, not_and, expected);Ok(())}@@ -309,7 +300,7 @@ mod tests {#[test]fn test_demorgans_law_or() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(a OR b) -> NOT a AND NOT blet or_expr = Arc::new(BinaryExpr::new(@@ -324,7 +315,7 @@ mod tests {Operator::And,Arc::new(NotExpr::new(col("b", &schema)?)),));- assert_not_simplify(&mut simplifier, not_or, expected);+ assert_not_simplify(&simplifier, not_or, expected);Ok(())}@@ -332,7 +323,7 @@ mod tests {#[test]fn test_demorgans_with_comparison_simplification() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(c = 1 AND c = 2) -> c != 1 OR c != 2let eq1 = Arc::new(BinaryExpr::new(@@ -361,7 +352,7 @@ mod tests {lit(ScalarValue::Int32(Some(2))),)),));- assert_not_simplify(&mut simplifier, not_and, expected);+ assert_not_simplify(&simplifier, not_and, expected);Ok(())}@@ -369,7 +360,7 @@ mod tests {#[test]fn test_not_of_not_and_not() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(NOT(a) AND NOT(b)) -> a OR blet not_a = Arc::new(NotExpr::new(col("a", &schema)?));@@ -382,7 +373,7 @@ mod tests {Operator::Or,col("b", &schema)?,));- assert_not_simplify(&mut simplifier, not_and, expected);+ assert_not_simplify(&simplifier, not_and, expected);Ok(())}@@ -390,7 +381,7 @@ mod tests {#[test]fn test_not_in_list() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(c IN (1, 2, 3)) -> c NOT IN (1, 2, 3)let list = vec![@@ -402,7 +393,7 @@ mod tests {let not_in: Arc<dyn PhysicalExpr> = Arc::new(NotExpr::new(in_list_expr));let expected = in_list(col("c", &schema)?, list, &true, &schema)?;- assert_not_simplify(&mut simplifier, not_in, expected);+ assert_not_simplify(&simplifier, not_in, expected);Ok(())}@@ -410,7 +401,7 @@ mod tests {#[test]fn test_not_not_in_list() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(c NOT IN (1, 2, 3)) -> c IN (1, 2, 3)let list = vec![@@ -422,7 +413,7 @@ mod tests {let not_not_in: Arc<dyn PhysicalExpr> = Arc::new(NotExpr::new(not_in_list_expr));let expected = in_list(col("c", &schema)?, list, &false, &schema)?;- assert_not_simplify(&mut simplifier, not_not_in, expected);+ assert_not_simplify(&simplifier, not_not_in, expected);Ok(())}@@ -430,7 +421,7 @@ mod tests {#[test]fn test_double_not_in_list() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// NOT(NOT(c IN (1, 2, 3))) -> c IN (1, 2, 3)let list = vec![@@ -443,7 +434,7 @@ mod tests {let double_not: Arc<dyn PhysicalExpr> = Arc::new(NotExpr::new(not_in));let expected = in_list(col("c", &schema)?, list, &false, &schema)?;- assert_not_simplify(&mut simplifier, double_not, expected);+ assert_not_simplify(&simplifier, double_not, expected);Ok(())}@@ -451,7 +442,7 @@ mod tests {#[test]fn test_deeply_nested_not() -> Result<()> {let schema = not_test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// Create a deeply nested NOT expression: NOT(NOT(NOT(...NOT(c > 5)...)))// This tests that we don't get stack overflow with many nested NOTs.@@ -471,7 +462,7 @@ mod tests {// With 200 NOTs (even number), should simplify back to the original expressionlet expected = inner_expr;- assert_not_simplify(&mut simplifier, Arc::clone(&expr), expected);+ assert_not_simplify(&simplifier, Arc::clone(&expr), expected);// Manually dismantle the deep input expression to avoid Stack Overflow on Drop// If we just let `expr` go out of scope, Rust's recursive Drop will blow the stack@@ -497,7 +488,7 @@ mod tests {#[test]fn test_simplify_literal_binary_expr() {let schema = Schema::empty();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// 1 + 2 -> 3let expr: Arc<dyn PhysicalExpr> =@@ -510,7 +501,7 @@ mod tests {#[test]fn test_simplify_literal_comparison() {let schema = Schema::empty();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// 5 > 3 -> truelet expr: Arc<dyn PhysicalExpr> =@@ -530,7 +521,7 @@ mod tests {#[test]fn test_simplify_nested_literal_expr() {let schema = Schema::empty();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// (1 + 2) * 3 -> 9let inner: Arc<dyn PhysicalExpr> =@@ -545,7 +536,7 @@ mod tests {#[test]fn test_simplify_deeply_nested_literals() {let schema = Schema::empty();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// ((1 + 2) * 3) + ((4 - 1) * 2) -> 9 + 6 -> 15let left: Arc<dyn PhysicalExpr> = Arc::new(BinaryExpr::new(@@ -568,7 +559,7 @@ mod tests {#[test]fn test_no_simplify_with_column() {let schema = test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// c1 + 2 should NOT be simplified (has column reference)let expr: Arc<dyn PhysicalExpr> = Arc::new(BinaryExpr::new(@@ -584,7 +575,7 @@ mod tests {#[test]fn test_partial_simplify_with_column() {let schema = test_schema();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// (1 + 2) + c1 should simplify the literal part: 3 + c1let literal_part: Arc<dyn PhysicalExpr> =@@ -605,7 +596,7 @@ mod tests {#[test]fn test_simplify_literal_string_concat() {let schema = Schema::empty();- let mut simplifier = PhysicalExprSimplifier::new(&schema);+ let simplifier = PhysicalExprSimplifier::new(&schema);// 'hello' || ' world' -> 'hello world'let expr: Arc<dyn PhysicalExpr> = Arc::new(BinaryExpr::new(