Bug #116721 Issues with combining COALESCE and DATE functions.

Submitted: 20 Nov 2024 7:01 Modified: 20 Nov 2024 7:33
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 7:01] PeiQi Luo
Description:
Incorrect query results when combining  COALESCE and DATE.
The two queries are logically equivalent because they achieve the same goal: explicitly expanding the behavior of the COALESCE function into a CASE WHEN statement. The COALESCE function selects the first non-NULL value from a list, and the rewritten CASE WHEN statement achieves the same logic by explicitly checking conditions:
If l_quantity is not NULL, it takes the value of l_quantity.
If l_quantity is NULL but l_suppkey is not NULL, it takes the value of l_suppkey.
If both are NULL, it defaults to 0.21501538554113775.
However, the final results are different, 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 l_extendedprice , l_orderkey , l_discount 
from lineitem join nation on nation.n_name >= lineitem.l_linestatus
where (coalesce(l_quantity, 
                 L_SUPPKEY, 
                0.21501538554113775, 
                l_receiptdate) )
      <= l_returnflag
group by l_extendedprice, l_orderkey, l_discount;
return 6004 rows

--rewritten
SELECT distinct l_extendedprice, l_orderkey, l_discount 
FROM lineitem join nation on nation.n_name >= lineitem.l_linestatus
WHERE 
  CASE WHEN l_quantity IS NOT NULL THEN l_quantity
       WHEN l_suppkey IS NOT NULL THEN l_suppkey
       else 0.21501538554113775  
  END <= l_returnflag
GROUP BY l_orderkey, l_extendedprice, l_discount;
return 0 row
[20 Nov 2024 7:33] MySQL Verification Team
Hello PQ L,

Thank you for the report and feedback.

regards,
Umesh

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.054 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.