Description:
A comparison predicate against a quantified subquery (>= ALL, <= ALL, > ALL, < ANY, > ANY) that evaluates to TRUE returns no rows when it is wrapped in an even number of NOT operators in a WHERE clause.
NOT(NOT P) is a strict identity under SQL three-valued logic (TRUE->TRUE, FALSE->FALSE, NULL->NULL), so WHERE P and WHERE NOT(NOT P) must select the same rows. They do not when P is a quantified comparison subquery.
The subquery column contains no NULL values, so three-valued logic is not involved. No error or warning is raised - the query silently returns the wrong result set.
Scope observed:
- 10 of the 12 operator x quantifier combinations are affected:
| predicate | bare | NOT(NOT ...) | |
|---|---|---|---|
| <= ANY | 2 | 3 | affected |
| < ANY | 2 | 3 | affected |
| >= ANY | 3 | 2 | affected |
| > ANY | 3 | 2 | affected |
| <> ANY | 3 | 0 | affected |
| = ANY | 0 | 0 | coincidentally equal |
| <= ALL | 0 | 1 | affected |
| < ALL | 0 | 1 | affected |
| >= ALL | 1 | 0 | affected |
| > ALL | 1 | 0 | affected |
| <> ALL | 3 | 3 | not affected |
| = ALL | 0 | 3 | affected |
In words: every comparison operator is affected, in both the ANY and the ALL flavour, except <> ALL - which is internally rewritten through the NOT IN path and therefore behaves correctly.
- NOT affected: <> ALL (rewritten via the NOT IN path), and plain IN
- Wrapping without negation (TRUE AND P, FALSE OR P) behaves correctly
- Replacing the quantified subquery with an equivalent scalar MAX() subquery behaves correctly
- An odd number of NOT operators behaves like a single NOT
A sharper description of the wrong rewrite: for the ANY quantifier the double negation is evaluated as x op' ANY (subq) - the comparison operator is negated but the ANY quantifier is kept, instead of the correct x op ANY (subq):
| original predicate | NOT(NOT ...) | equals | same count as |
|---|---|---|---|
| x <= ANY (subq) | wrong | x > ANY (subq) | yes |
| x < ANY (subq) | wrong | x >= ANY (subq) | yes |
| x >= ANY (subq) | wrong | x < ANY (subq) | yes |
| x > ANY (subq) | wrong | x <= ANY (subq) | yes |
This is consistent with the MAX/MIN rewrite the optimizer applies to an uncorrelated quantified comparison: the single-NOT path keeps the correct aggregate, but the double-NOT path keeps the negated operator together with the aggregate that belongs to the un-negated direction, and the ANY/ALL quantifier is not flipped back.
How it was found: a SQL fuzzer compared two logically identical statements that differed only in the boolean wrapper of the WHERE clause (18 versus 8 rows updated from the same 82-row starting state). The divergence localised to a single (t0.c6) <= ANY (SELECT ...) term.
Reproduced on 8.0.13, 8.0.18, 8.0.45 and 8.0.46 (all official community builds): 10 of 11 instances tested diverged (4 vs 3 rows on the minimal form), the one exception being a vendor 8.0.13-based build that does not carry the defect.
How to repeat:
CREATE DATABASE bugtest;
USE bugtest;
CREATE TABLE a (x int);
INSERT INTO a VALUES (1),(2),(3);
CREATE TABLE b (y int);
INSERT INTO b VALUES (5),(6);
-- The predicate evaluates to TRUE (10 is >= every value in b)
SELECT (10 >= ALL (SELECT y FROM b)) AS p;
-- returns 1
-- Bare predicate in WHERE: correct, matches all 3 rows
SELECT COUNT(*) FROM a WHERE 10 >= ALL (SELECT y FROM b);
-- returns 3 (expected 3)
-- Same predicate under double negation: returns 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 >= ALL (SELECT y FROM b)));
-- returns 0 (expected 3) <-- WRONG
Other affected operators (every comparison operator in both the ANY and the ALL flavour; only <> ALL is exempt, it is rewritten through the NOT IN path):
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 >= ALL (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT ( 1 <= ALL (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT ( 1 < ALL (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 > ALL (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT ( 1 < ANY (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 > ANY (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT ( 1 <= ANY (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 >= ANY (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 <> ANY (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 = ALL (SELECT y FROM b))); -- expected 3, got 0
Controls that behave correctly:
-- <> ALL is not affected
SELECT COUNT(*) FROM a WHERE 10 <> ALL (SELECT y FROM b); -- 3
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 <> ALL (SELECT y FROM b))); -- 3 correct
-- Equivalent scalar MAX() subquery is not affected
SELECT COUNT(*) FROM a WHERE 10 >= (SELECT MAX(y) FROM b); -- 3
SELECT COUNT(*) FROM a WHERE NOT(NOT (10 >= (SELECT MAX(y) FROM b))); -- 3 correct
-- Wrapping without negation is not affected
SELECT COUNT(*) FROM a WHERE TRUE AND (10 >= ALL (SELECT y FROM b)); -- 3 correct
SELECT COUNT(*) FROM a WHERE FALSE OR (10 >= ALL (SELECT y FROM b)); -- 3 correct
-- Deeper wrappings that contain an even number of NOTs are also affected
SELECT COUNT(*) FROM a WHERE NOT(FALSE OR NOT (10 >= ALL (SELECT y FROM b))); -- expected 3, got 0
SELECT COUNT(*) FROM a WHERE NOT(FALSE OR NOT(TRUE AND (10 >= ALL (SELECT y FROM b)))); -- expected 3, got 0
-- No NULLs in the subquery column
SELECT COUNT(*) AS total, COUNT(y) AS nonnull FROM b; -- 2, 2
Suggested fix:
Not verified against source, offered only as a starting point.
A quantified comparison is rewritten internally into a form with special NULL handling (roughly x >= ALL (subq) -> NOT (x < ANY (subq)), then handled by Item_in_optimizer / Item_allany_subselect). That rewrite depends on whether the predicate sits at the top level of the WHERE clause, where FALSE and NULL are interchangeable, versus nested under NOT where they are not.
The symptom is consistent with the top-level flag, or the parity of the enclosing negations, being tracked incorrectly when the predicate is nested under NOT - the rewritten result appears to be negated one extra time.
Note that Bug #51070 ("Query with a NOT IN subquery predicate returns a wrong result set", fixed in 5.5) describes the same mechanism for a different predicate: "When applying the NOT, the subquery was wrapped inside a NOT node ... Fixed by making a NOT node forward the notification to its child node." The fix there covered NOT IN with NULLs; the comparison-based ALL/ANY path appears not to be covered. That patch may be a useful reference.