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
SELECT i, (-i) & i, i | (-i), (-i) ^ i
FROM (VALUES (5), (6)) AS t(i);
Current result:
Expected result:
Double negation can also hide overflow:
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.
Describe the bug
Expr::Negativerepresents 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
Current result:
Expected result:
Double negation can also hide overflow:
This returns
-128after 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_passesis set to0.