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

   ## Which issue does this PR close?
   
   - Closes #25137.
   
   ## Rationale for this change
   
   The simplifier rewrites `power(a, log(a, b))` to `b`, `log(a, 1)` to `0`, 
`log(a, a)` to `1` and `log(a, power(a, b))` to `b` whenever the base is 
non-nullable. Those identities hold only for a finite base greater than 0 and 
not equal to 1. For a base of 1, 0 or a negative value the rewritten expression 
gives a different answer from evaluation, so `log(a, 1.0)` over a non-nullable 
column returns `0` on a row where `a` is `-2.0` while the same query over a 
nullable column returns `NaN`.
   
   ## What changes are included in this PR?
   
   A helper in `math/common.rs` accepts a literal base whose value, cast to 
`Float64`, is finite, greater than 0 and not 1. The four rewrites are gated on 
it instead of on nullability. `power(a, 0)` and `power(a, 1)` are untouched, 
since they hold for every finite base.
   
   A column base is no longer rewritten at all, rather than value-checked, 
because the condition has to hold for every row and a column gives no such 
guarantee; the non-nullable column in the existing `math.slt` test is exactly 
the shape that produced the wrong answer. The cost is that a column whose rows 
all happen to be valid loses the rewrite, which would need per-column value 
bounds to recover.
   
   ## Are these changes tested?
   
   `math.slt` gains the issue's five queries with the correct results, and an 
`EXPLAIN` showing a literal base of 2 still simplifies. The existing `EXPLAIN` 
over a non-nullable column base now shows the calls preserved. Unit tests in 
`log.rs` and `power.rs` cover bases of 1, 0, -2 (no rewrite) and 2 (rewrite). 
`cargo test -p datafusion-functions` and the `math`, `expr`, `scalar`, 
`simplify_expr`, `simplify_predicates` and `explain` sqllogictest files pass 
with no other snapshot change.
   
   ## Are there any user-facing changes?
   
   Queries with a degenerate literal base, or with a column base, now return 
the same results as evaluation instead of the folded identity.
   
   Written with AI assistance (Claude Code); I checked the four rewrite sites, 
the value cast in the helper, and ran the tests myself.
   


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