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

   Closes #27949
   
   ## Which issue does this PR close?
   
   Closes #27949
   
   ## Rationale for this change
   
   In `TypeCoercionRewriter`, `TreeNode::rewrite` traverses expressions 
bottom-up, calling `f_up` on each parent after its children are coerced. 
Previously, `f_up` called `expr.get_type(schema)` on each child (recursively 
walking already-coerced child subtrees) and then `expr.cast_to(target, schema)` 
(calling `get_type` a second time). For a left-leaning chain of `N` binary 
expressions (such as `x + 1 + 1 ... + 1`), every level walked its child 
subtrees, causing `O(N^2)` work.
   
   ## What changes are included in this PR?
   
   - Track bottom-up coerced child `DataType`s across 
`TreeNodeRewriter::f_down` and `f_up` using `type_stack` and `frame_starts` in 
`TypeCoercionRewriter` (with fallback to `expr.get_type(self.schema)` if `f_up` 
is invoked directly without `f_down`).
   - Reuse those known child types in `coerce_binary_op_with_types`, 
`coerce_like_operands`, `get_casted_expr_for_bool_op_with_type`, 
`coerce_case_expression_with_types`, `Between`, `InList`, `Negative`, 
`InSubquery`, and `SetComparison`, along with `cast_expr_with_known_type` so 
casting does not re-walk child expressions.
   - Add unit tests `deep_binary_expr_chain_coercion` (1,000 chained binary 
ops) and `direct_f_up_without_f_down_falls_back_cleanly` in 
`datafusion/optimizer/src/analyzer/type_coercion.rs`.
   
   ## Are these changes tested?
   
   - `cargo fmt -p datafusion-optimizer -- --check`
   - `cargo +stable-x86_64-pc-windows-gnullvm clippy -p datafusion-optimizer 
--no-deps --lib --tests -- -D warnings`
   - `cargo +stable-x86_64-pc-windows-gnullvm test -p datafusion-optimizer 
--lib` (986 passed, 0 failed)


-- 
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