Bug #119345 Presence of UNIQUE index on INT column causes inconsistent results for IF with ANY subquery
Submitted: 10 Nov 9:21 Modified: 20 Nov 11:32
Reporter: zz z Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[10 Nov 9:21] zz z
Description:
A query with a WHERE clause containing a complex IF function and multiple type conversions (INT -> DATETIME -> DECIMAL) should produce a deterministic result. Logically, the IF expression should always evaluate to 0, making the WHERE condition TRUE and returning one row. However, the actual behavior depends on whether a UNIQUE index exists on the column. When the column has no UNIQUE index, the query incorrectly returns an empty set. After adding a UNIQUE index to the column, the same query returns the correct result. This indicates that the MySQL optimizer chooses a faulty execution plan for the non-indexed table, which fails to correctly evaluate the complex expression

How to repeat:
drop TABLE if EXISTS t85;
CREATE TABLE t85 (c1 INT);
INSERT t85 () VALUES (0);
SELECT * FROM t85 WHERE (IF(t85.c1 LIKE '', 2, CAST(CAST(0 AS DATETIME) AS DECIMAL(15))) = ANY (SELECT t85.c1 FROM t85));
-- empty set
drop TABLE if EXISTS t85;
CREATE TABLE t85 (c1 INT UNIQUE);
INSERT t85 () VALUES (0);
SELECT * FROM t85 WHERE (IF(t85.c1 LIKE '', 2, CAST(CAST(0 AS DATETIME) AS DECIMAL(15))) = ANY (SELECT t85.c1 FROM t85));
-- 0
[20 Nov 11:32] Roy Lyseng
Verified as described on 8.0, 8.4 and 9.5.