1fanwang opened a new pull request, #25138:
URL: https://github.com/apache/datafusion/pull/25138

   ## Which issue does this PR close?
   
   - Closes #25137.
   
   ## Rationale for this change
   
   `power(1.0, log(1.0, b))` returns `b` instead of `1.0`, and a filter over it
   matches rows it should not:
   
   ```sql
   SELECT count(*) FROM t WHERE power(1.0, log(1.0, b)) = 2.0;
   -- returns 1, should be 0
   ```
   
   Four rewrites assume the logarithm and power identity holds for their shared
   base, which is only true for a base that is positive and not 1. They checked
   nullability, which says nothing about the value, so a base of 1, 0, negative,
   NaN or infinity folded wrongly. The same query over a nullable column already
   returned the correct result, so the answer depended on the schema, not the 
data.
   
   ## What changes are included in this PR?
   
   The four rewrites now fire only when the base is a literal that is finite,
   positive and not 1. A base whose value cannot be proven, such as a column,
   keeps the original expression rather than folding.
   
   ## What is the testing strategy for this PR?
   
   `test_log_simplify_rejects_degenerate_base` covers all three log rewrites
   against bases of 1, 0, -2, NaN and infinity, and 
`test_log_simplify_accepts_valid_base`
   confirms a valid base still folds. The issue's reproducer was also run 
through
   `datafusion-cli` on the merge base and on this branch.
   
   <details><summary>Reproducer, before and after</summary>
   
   ```sql
   CREATE VIEW t AS SELECT * FROM (VALUES (2.0), (3.0)) s(b);
   SELECT b, power(1.0, log(1.0, b)) AS p, log(1.0, power(1.0, b)) AS l FROM t;
   SELECT count(*) AS c FROM t WHERE power(1.0, log(1.0, b)) = 2.0;
   CREATE VIEW nn AS SELECT * FROM (VALUES (1.0), (0.0), (-2.0), (2.0)) s(a);
   SELECT a, log(a, 1.0) AS l1, log(a, a) AS la FROM nn;
   ```
   
   Before, on the merge base:
   
   ```
   | b   | p   | l   |     | c |     | a    | l1  | la  |
   | 2.0 | 2.0 | 2.0 |     | 1 |     | 1.0  | 0.0 | 1.0 |
   | 3.0 | 3.0 | 3.0 |             | 0.0  | 0.0 | 1.0 |
                                    | -2.0 | 0.0 | 1.0 |
                                    | 2.0  | 0.0 | 1.0 |
   ```
   
   After, on this branch:
   
   ```
   | b   | p   | l   |     | c |     | a    | l1   | la  |
   | 2.0 | 1.0 | NaN |     | 0 |     | 1.0  | NaN  | NaN |
   | 3.0 | 1.0 | NaN |             | 0.0  | -0.0 | NaN |
                                    | -2.0 | NaN  | NaN |
                                    | 2.0  | 0.0  | 1.0 |
   ```
   
   </details>
   
   ## Are there any user-facing changes?
   
   Yes. Queries whose base is a literal 1, 0, negative, NaN or infinity now 
return
   the correct value instead of a folded one. No API change.
   


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