Simplify a physical expression
(&self, expr: Arc<dyn PhysicalExpr>)
| 51 | |
| 52 | /// Simplify a physical expression |
| 53 | pub fn simplify(&self, expr: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> { |
| 54 | let mut current_expr = expr; |
| 55 | let mut count = 0; |
| 56 | let schema = self.schema; |
| 57 | |
| 58 | let batch = create_dummy_batch()?; |
| 59 | |
| 60 | while count < MAX_LOOP_COUNT { |
| 61 | count += 1; |
| 62 | let result = current_expr.transform(|node| { |
| 63 | #[cfg(debug_assertions)] |
| 64 | let original_type = node.data_type(schema).unwrap(); |
| 65 | |
| 66 | // Apply NOT expression simplification first, then unwrap cast optimization, |
| 67 | // then constant expression evaluation |
| 68 | #[expect(deprecated, reason = "`simplify_not_expr` is marked as deprecated until it's made private.")] |
| 69 | let rewritten = not::simplify_not_expr(node, schema)? |
| 70 | .transform_data(|node| unwrap_cast_in_comparison(node, schema))? |
| 71 | .transform_data(|node| { |
| 72 | const_evaluator::simplify_const_expr_immediate(node, batch) |
| 73 | })?; |
| 74 | |
| 75 | #[cfg(debug_assertions)] |
| 76 | assert_eq!( |
| 77 | rewritten.data.data_type(schema).unwrap(), |
| 78 | original_type, |
| 79 | "Simplified expression should have the same data type as the original" |
| 80 | ); |
| 81 | |
| 82 | Ok(rewritten) |
| 83 | })?; |
| 84 | |
| 85 | if !result.transformed { |
| 86 | return Ok(result.data); |
| 87 | } |
| 88 | current_expr = result.data; |
| 89 | } |
| 90 | Ok(current_expr) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |