Bug #117834 Double negation on booleans incorrectly returns negated value
Submitted: 31 Mar 3:43 Modified: 1 Apr 6:16
Reporter: Emily Ong Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:9.2, 8.0.41, 8.4.4 OS:Ubuntu
Assigned to: CPU Architecture:x86

[31 Mar 3:43] Emily Ong
Description:
Using a double negation on booleans returns the negated boolean value instead of the original boolean value. This bug is present in version 9.2 and in earlier versions 5-8+.

How to repeat:
SELECT !!1, !!0; -- expected: {1, 0}; actual: {0, 1}

The expected result is produced when:

(1) Parenthesis is used:
SELECT !(!1), !(!0); -- {1, 0}

However, based on the precedence description of the NOT operator in the documentation (https://dev.mysql.com/doc/refman/9.2/en/logical-operators.html#operator_not), it is unexpected that using parenthesis would affect the result of the query evaluation.

This is because !!1 should evaluate to !(!1) and likewise, !!0 should evaluate to !(!0).
                               
(2) Using NOT instead of !
SELECT NOT NOT 1, NOT NOT 0; -- {1, 0}
SELECT NOT(NOT 1), NOT(NOT 0) -- {1, 0}

This is unexpected because NOT is semantically equivalent to !.

It is mentioned in the documentation (https://dev.mysql.com/doc/refman/9.2/en/logical-operators.html#operator_not) that:
                                      
> The ! operator is a nonstandard extension, and is deprecated; expect it to be removed in a future version of MySQL. Applications, where necessary, should be adjusted to use the standard SQL NOT operator instead.

However, the use of ! is still documented and supported in version 9.20, thus this bug should still be relevant.
[1 Apr 6:16] MySQL Verification Team
Hello Emily Ong,

Thank you for the report and test case.

regards,
Umesh