You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expression simplifier treats arithmetic negation as bitwise NOT #24665
Expr::Negative represents arithmetic negation, 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.
The same path also distributes unary minus through bitwise expressions and removes nested negation without considering the expression type.
To Reproduce
SELECT i, (-i) & i, i | (-i), (-i) ^ i
FROM (VALUES (5), (6)) AS t(i);
Current result:
5 0 -1 -1
6 0 -1 -1
Expected result:
5 1 -1 -2
6 2 -2 -4
Generic double-negation removal can also hide overflow for checked types:
The inner timestamp negation should report arithmetic overflow.
Expected behavior
Arithmetic negation should not use bitwise-complement identities.
Double negation may be cancelled only where negation is total and involutive: signed integers with wrapping semantics, floating-point values, valid decimals, and NULL. Timestamp and interval negation must remain explicit because it can overflow.
Additional context
Disabling the optimizer produces the correct bitwise results and preserves the timestamp overflow.
Signed integer scalar and array execution must use the same wrapping semantics. Pruning through signed negation must also account for the discontinuity at MIN.
Activity
Amogh-2404 commentedon Aug 25, 2026
take
fix: stop treating arithmetic negation as bitwise NOT
fix: stop treating arithmetic negation as bitwise NOT (apache#24668)
fix: stop treating arithmetic negation as bitwise NOT (apache#24668)