Bug #116720 Issues with the combination of CASE and DATE.
Submitted: 20 Nov 2024 6:48 Modified: 20 Nov 2024 7:32
Reporter: PeiQi Luo Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S1 (Critical)
Version:8.4.2, 8.0.40 OS:Any
Assigned to: CPU Architecture:Any

[20 Nov 2024 6:48] PeiQi Luo
Description:
Incorrect query results when combining CASE and DATE.
The first query uses COALESCE and NOT IN to compare l_shipmode and l_commitdate, while the second query uses CASE WHEN and NOT =. However, their essence is the same: if l_shipmode is not NULL, its value is compared to l_commitdate; if they are equal, the row is excluded. If l_shipmode is NULL, then return NULL. The two SQL queries are logically equivalent, but they return different results, indicating the presence of a bug.

How to repeat:
This link provides the exported sql file for the database:
https://github.com/LLuopeiqi/newtpcd/blob/main/mysql/tpcd.sql

--original
select all lineitem.l_shipinstruct , lineitem.l_extendedprice , lineitem.l_comment  
from lineitem 
where (coalesce(lineitem.l_shipmode, null)) not in (lineitem.l_commitdate)
return 5805 rows

--rewritten
SELECT l_shipinstruct , l_extendedprice , l_comment  FROM lineitem 
WHERE 
NOT CASE 
WHEN l_shipmode IS NOT NULL 
	THEN CAST(l_shipmode AS CHAR) 
ELSE
	NULL 
END = l_commitdate ;
return 0 row
[20 Nov 2024 7:32] MySQL Verification Team
Hello PQ L,

Thank you for the report and feedback.

regards,
Umesh