Bug #113319 unnesting scalar subquery containing count and having cond produces wrong result
| Submitted: | 4 Dec 2023 6:58 | Modified: | 25 Jul 2024 16:34 |
|---|---|---|---|
| Reporter: | chen jiang | Email Updates: | |
| Status: | Closed | Impact on me: | |
| Category: | MySQL Server: Optimizer | Severity: | S2 (Serious) |
| Version: | mysql-8.0.33, 8.0.35, 8.1.0, 8.2.0 | OS: | Any |
| Assigned to: | CPU Architecture: | Any | |
| Tags: | count, Scalar subquery, unnest, wrong result | ||
[4 Dec 2023 6:58]
chen jiang
[4 Dec 2023 7:24]
MySQL Verification Team
Hello chen jiang, Thank you for the report and test case. Verified as described. regards, Umesh
[25 Jul 2024 16:34]
Jon Stephens
Documented fix as follows in the MySQL 9.0.0 changelog:
A query such as the following:
SELECT
(
SELECT COUNT(1) AS cnt
FROM t2
WHERE t2.a = t1.a
HAVING cnt > 0
)
FROM t1;
was transformed to this:
SELECT COALESCE(derived_1_2.cnt,0) AS cnt
FROM t1
LEFT JOIN
(
SELECT COUNT(1) AS cnt,
t2.a AS a from t2
GROUP BY t2.a
HAVING (cnt > 0)
) AS derived_1_2
ON derived_1_2.a = t1.a;
The presence of a false HAVING condition in the subquery should
semantically change the correct result of the scalar subquery
from zero to NULL, which happened as expected for the original
query, but not in the transformed case.
Closed.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.040 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.