Details
-
Bug
-
Status: Confirmed (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.11, 11.4, 11.8, 12.3, 13.1, 12.3.3
-
None
-
MariaDB 12.3.3, default optimizer_switch. Reproduced with both MyISAM and InnoDB.
-
Unexpected results
-
Correlated scalar COUNT(*) could return an incorrect result for multiple NULL values in a nullable UNIQUE key.
Description
A correlated scalar subquery with COUNT
returns an incorrect result
when a nullable UNIQUE column contains multiple NULL values.
Tested on MariaDB 12.3.3 with the default optimizer_switch.
The issue reproduces with both MyISAM and InnoDB.
Minimal reproducer:
CREATE TABLE n(c INT UNIQUE) ENGINE=MyISAM;
INSERT INTO n VALUES (NULL), (NULL);
SELECT (SELECT COUNT
FROM n z WHERE z.c <=> x.k) AS scalar_count
FROM (SELECT NULL AS k) x;
SELECT COUNT
AS direct_count
FROM n WHERE c IS NULL;
Actual result:
scalar_count
------------
1
direct_count
------------
2
Expected result:
scalar_count
------------
2
The nullable UNIQUE column permits both rows with c = NULL.
In the correlated scalar subquery, x.k is NULL. Since <=> is
NULL-safe equality, both rows where z.c is NULL satisfy:
z.c <=> x.k
Therefore, COUNT
should return 2.
The direct query:
SELECT COUNT
FROM n WHERE c IS NULL;
correctly returns 2, while the correlated scalar form incorrectly
returns 1.
The same wrong result is reproduced when the table uses InnoDB
instead of MyISAM.
Attachments
Issue Links
- relates to
-
MDEV-36215 Wrong query result due to Table Elimination hitting unique_col IS NULL condition
-
- Closed
-
-
MDEV-37057 Wrong result with LATERAL DERIVED
-
- Closed
-