haohuaijin opened a new pull request, #27984:
URL: https://github.com/apache/datafusion/pull/27984

   ## Which issue does this PR close?
   
   - N/A (standalone planning performance improvement)
   
   ## Rationale for this change
   
   `SimplifyExpressions` runs bottom-up on every node in every logical 
optimization pass and currently does avoidable schema building, cloning, and 
expression rewriting work on no-op nodes and leaf expressions:
   
   1. **Expression-less / leaf-only nodes**: Even when a plan node has no 
simplifiable expressions (for example, `TableScan` with `filters.is_empty()`, 
`Union`, `SubqueryAlias`, `Projection` of bare columns, or equi-`Join` with no 
filter), `SimplifyExpressions` unconditionally builds a `DFSchema` (for 
`TableScan`, converting the full unprojected `source.schema()` via 
`DFSchema::try_from_qualified_schema`), constructs `SimplifyContext` / 
`ExprSimplifier` / `NamePreserver`, and runs `map_expressions`.
   2. **Single-input schema cloning**: For unary nodes (`Projection`, `Filter`, 
`Sort`, `Aggregate`, `Window`, etc.), `merge_schema(&plan.inputs())` 
deep-clones the child `DFSchema` instead of cloning the `Arc`.
   3. **Bare `Expr::Column` / `Expr::Literal` expressions**: For each leaf 
expression, `SimplifyExpressions` allocates its `schema_name` via 
`NamePreserver::save`, and 
`ExprSimplifier::simplify_with_cycle_count_transformed` allocates a 
`ConstEvaluator` (including `ExecutionProps` and `Arc<AliasGenerator>`) and 
runs all simplification passes even when `guarantees` is empty.
   4. **Empty `guarantees` traversal**: When `self.guarantees` is empty, 
`ExprSimplifier` still walks the expression tree in 
`rewrite_with_guarantees_map` on every cycle.
   
   ## What changes are included in this PR?
   
   - In `SimplifyExpressions::optimize_internal` 
(`datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs`):
     - Check via `plan.apply_expressions` whether the node has any expression 
other than bare `Expr::Column` / `Expr::Literal`; return 
`Transformed::no(plan)` immediately if not.
     - Avoid calling `plan.inputs()` twice and use `Arc::clone(input.schema())` 
for single-input nodes instead of deep-cloning via `merge_schema`.
     - Skip `NamePreserver::save` and simplification for bare `Expr::Column` / 
`Expr::Literal` expressions.
   - In `ExprSimplifier::simplify_with_cycle_count_transformed` 
(`datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs`):
     - Short-circuit bare `Expr::Column` / `Expr::Literal` when 
`self.guarantees.is_empty()`.
     - Skip `rewrite_with_guarantees_map` when `guarantees_map.is_empty()`.
   
   ### Benchmarks (`sql_planner`, `optimizer_*`)
   
   Measured locally with Criterion (`critcmp opt_base simplify_opt`):
   
   ```text
   group                                   opt_base                             
  simplify_opt
   -----                                   --------                             
  ------------
   optimizer_correlated_exists             2.38    188.1±2.48µs        ? ?/sec  
  1.00     79.1±0.78µs        ? ?/sec
   optimizer_join_4_with_agg_filter        1.08    243.3±9.76µs        ? ?/sec  
  1.00    224.7±4.51µs        ? ?/sec
   optimizer_join_chain_4                  1.11     81.6±0.64µs        ? ?/sec  
  1.00     73.7±1.06µs        ? ?/sec
   optimizer_join_chain_8                  1.08    227.3±3.24µs        ? ?/sec  
  1.00    210.5±2.54µs        ? ?/sec
   optimizer_select_all_from_1000          1.31      2.0±0.01ms        ? ?/sec  
  1.00  1530.9±14.75µs        ? ?/sec
   optimizer_select_one_from_700           9.99    232.1±2.74µs        ? ?/sec  
  1.00     23.2±1.47µs        ? ?/sec
   optimizer_tpcds_all                     1.07    145.2±1.94ms        ? ?/sec  
  1.00    135.7±4.13ms        ? ?/sec
   optimizer_tpch_all                      1.05      8.6±0.08ms        ? ?/sec  
  1.00      8.1±0.09ms        ? ?/sec
   optimizer_wide_aggregate_100            1.07  1247.1±12.65µs        ? ?/sec  
  1.00  1165.3±14.97µs        ? ?/sec
   optimizer_wide_filter_200               1.04      2.6±0.02ms        ? ?/sec  
  1.00      2.5±0.07ms        ? ?/sec
   optimizer_wide_projection_stack_all     1.03      4.2±0.03ms        ? ?/sec  
  1.00      4.0±0.05ms        ? ?/sec
   optimizer_wide_projection_stack_half    1.04      2.9±0.02ms        ? ?/sec  
  1.00      2.8±0.02ms        ? ?/sec
   ```
   
   ## What is the testing strategy for this PR?
   
   Existing unit tests in `datafusion-optimizer` (`cargo test -p 
datafusion-optimizer --lib`, 984 tests) and CI suite cover 
`SimplifyExpressions` and `ExprSimplifier` (including guarantee-based 
simplification and expression simplification on all plan types). Verified 
`cargo fmt --all` and `cargo clippy -p datafusion-optimizer --all-targets 
--all-features -- -D warnings`.
   
   ## Are there any user-facing changes?
   
   No.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to