| 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.
