Amogh-2404 opened a new issue, #24665:
URL: https://github.com/apache/datafusion/issues/24665

   ### Describe the bug
   
   `Expr::Negative` represents unary minus, but the expression simplifier 
applies bitwise-NOT identities to it. This produces wrong results for 
expressions combining unary minus with bitwise AND, OR, or XOR.
   
   ### To Reproduce
   
   ```sql
   SELECT i, (-i) & i, i | (-i), (-i) ^ i
   FROM (VALUES (5), (6)) AS t(i);
   ```
   
   Current result:
   
   ```text
   5  0  -1  -1
   6  0  -1  -1
   ```
   
   Expected result:
   
   ```text
   5  1  -1  -2
   6  2  -2  -4
   ```
   
   Double negation can also hide overflow:
   
   ```sql
   SELECT -(-CAST(-128 AS TINYINT));
   ```
   
   This returns `-128` after simplification instead of reporting overflow.
   
   ### Expected behavior
   
   The optimiser should only apply rewrites that are valid for arithmetic 
negation.
   
   ### Additional context
   
   Both cases behave correctly when `datafusion.optimizer.max_passes` is set to 
`0`.
   
   


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