Bug #121889 Wrong result: x IN (SELECT <constant> FROM t EXCEPT ALL ...) is true for every outer row
Description:
When an IN subquery is an EXCEPT ALL whose left operand is a constant, the IN predicate is true for every row of the outer query, so the WHERE keeps every row. The subquery does not depend on the outer row, so IN should have one fixed truth value. NOT IN is false for every row on the same statement, and SELECT ... IN (...) as a scalar returns 1 for values that are not in the subquery's result.
The subquery SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t returns the single row (1) on its own, so only an outer value of 1 should satisfy the IN. Instead every outer value does.
EXPLAIN shows the outer comparison pushed into both operands of the EXCEPT ALL as correlated filters (<cache>(t.c) = 1 and <cache>(t.c) = 5), evaluated once per outer row. For an outer value other than 1 both filters are false, so both operands are empty and the EXCEPT ALL is empty; the IN is then satisfied anyway. On 8.4.13 the plan keeps a "Limit: 1 row(s)" under each operand and the result is correct; from 9.4.0 the limit is lifted above the materialized EXCEPT ALL and the result is wrong. This looks related to the verified duplicate-handling bugs over INTERSECT ALL / EXCEPT ALL in a dependent position (bug#120097, bug#120098, bug#121505), seen here through IN rather than EXISTS.
Only wrong with the ALL form (EXCEPT ALL or INTERSECT ALL); EXCEPT, INTERSECT, UNION ALL and a plain subquery are correct. Still wrong when the left operand is a column expression rather than a literal, when the operands read a different table, and with (x, x) IN (...). Correct when the subquery is wrapped in a derived table. optimizer_switch settings hash_set_operations, semijoin and materialization do not change it, and internal_tmp_mem_storage_engine=MEMORY does not either.
Versions (official Docker images, default configuration): wrong on 9.4.0, 9.5.0, 9.6.0, 9.7.0, 9.7.2, 9.7.4 and 26.10.0; correct on 8.4.13. The same on x86_64 and arm64.
Found with automated metamorphic testing.
How to repeat:
CREATE TABLE t (c INT);
INSERT INTO t VALUES (1), (2), (3);
-- The subquery is (1) on its own.
SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t; -- one row: 1
-- Expected: only 1. Actual (9.4.0 to 26.10.0): 1, 2, 3.
SELECT c FROM t WHERE c IN (SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t);
-- NOT IN is then false for every row. Expected: 2, 3. Actual: empty.
SELECT c FROM t WHERE c NOT IN (SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t);
-- As a scalar the same subquery is right. Expected and actual: (1,1),(2,0),(3,0).
SELECT c, c IN (SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t) AS m FROM t;
-- Correct forms on the same server: EXCEPT (no ALL), INTERSECT, a derived table.
SELECT c FROM t WHERE c IN (SELECT 1 FROM t EXCEPT SELECT 5 FROM t); -- 1
SELECT c FROM t WHERE c IN (SELECT * FROM (SELECT 1 FROM t EXCEPT ALL SELECT 5 FROM t) AS d); -- 1
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.039 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.