Bug #120923 Wrong result with CTE, UNION ALL, GROUP BY, and COUNT()

Submitted: 14 Jul 13:53 Modified: 21 Jul 18:06
Reporter: Aaditya Dubey Email Updates:
Status: Patch pending Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.4.10 OS:Any
Assigned to: CPU Architecture:Any

[14 Jul 13:53] Aaditya Dubey
Description:
A query using a VIEW returns a different result from an equivalent query using a CTE.

The query involves:

UNION ALL

NULL values inserted via default column values

GROUP BY

COUNT(c)

an outer UNION

VIEW vs CTE execution

The VIEW query returns {0, 2}, while the equivalent CTE query returns {0, 1}.

This appears to be a wrong-result bug, possibly related to CTE evaluation, UNION ALL materialization, grouping, or aggregate evaluation.

How to repeat:
DROP DATABASE IF EXISTS dd;
CREATE DATABASE dd;
USE dd;
CREATE TABLE a (
  b INT
);
INSERT INTO a VALUES (1);
INSERT INTO a VALUES ();
CREATE OR REPLACE VIEW v0 AS
SELECT b AS c FROM a
UNION ALL
SELECT b FROM a;
-- Query using VIEW
SELECT c
FROM v0
WHERE c = 0
UNION
SELECT COUNT(c)
FROM v0
GROUP BY c;
-- Equivalent query using CTE
WITH CTE AS (
  SELECT b AS c FROM a
  UNION ALL
  SELECT b FROM a
)
SELECT c
FROM CTE
WHERE c = 0
UNION
SELECT COUNT(c)
FROM CTE
GROUP BY c;
Actual result
The VIEW query returns:

+------+
| c    |
+------+
|    2 |
|    0 |
+------+
2 rows in set
The equivalent CTE query returns:

+------+
| c    |
+------+
|    1 |
|    0 |
+------+
2 rows in set
Expected result
Both queries should return the same result.

The expected result appears to be:

+------+
| c    |
+------+
|    2 |
|    0 |
+------+

Suggested fix:
Reasoning
The base table a contains two rows:

b
----
1
NULL
The VIEW/CTE body is:

SELECT b AS c FROM a
UNION ALL
SELECT b FROM a
Because this uses UNION ALL, duplicates should be preserved. Therefore the VIEW/CTE result should contain four rows:

c
----
1
NULL
1
NULL
The first branch of the outer query is:

SELECT c
FROM v0
WHERE c = 0
This should return no rows, because neither 1 nor NULL satisfies c = 0.

The second branch is:

SELECT COUNT(c)
FROM v0
GROUP BY c
Grouping the four rows by c should produce:

Group c = 1:    COUNT(c) = 2
Group c = NULL: COUNT(c) = 0
Therefore the final result of the outer UNION should be {2, 0}.

The VIEW query returns {2, 0}, which matches this reasoning. The CTE query returns {1, 0}, which appears incorrect because it seems to count only one non-NULL row from the UNION ALL result.
[21 Jul 18:06] Chaithra Marsur Gopala Reddy
Hi Aaditya Dubey,

Thank you for the test case. Verified as described.
[27 Oct 15:06] OCA Admin
Contribution submitted via Github - Bug#120923: Retain required sorts for shared TempTable CTEs 
(*) Contribution by PR specific confirm no longer needed

Contribution: git_patch_4660692152.txt (text/plain), 59.69 KiB.

[30 Oct 15:48] MySQL Admin
Posted by developer: Bug status updated to 'Patch pending'
[17 Nov 0:38] Abhishek Soni
Another shape that looks like the same problem (a CTE read twice, where the grouping on one read goes wrong depending on which read runs first), from our own testing. GROUP BY on a repeated column returns 87 twice when the other read of the CTE comes first. Same results on 8.4.13, 9.4.0, 9.5.0, 9.6.0, 9.7.0, 9.7.2, 9.7.4 and 26.10.0 (official Docker images, arm64), so it may be worth checking the patch against it:

CREATE TABLE g2 (a TINYINT, b SMALLINT);
INSERT INTO g2 VALUES (1,87),(2,87),(3,0),(4,1),(5,100),(1,18),(2,25),(3,46),(4,51),(5,54),(6,71),(7,86),(8,87),(9,93),(9,93);
-- 12 rows, each value once (correct):
WITH c AS (SELECT NULL AS x, NULL AS y, a, b FROM g2 GROUP BY a, b)
(SELECT b FROM c GROUP BY b, b) UNION ALL SELECT NULL FROM c WHERE b = NULL;
-- 13 rows, 87 twice (wrong); only the order of the UNION ALL operands differs:
WITH c AS (SELECT NULL AS x, NULL AS y, a, b FROM g2 GROUP BY a, b)
SELECT NULL FROM c WHERE b = NULL UNION ALL ((SELECT b FROM c GROUP BY b, b));

In other reduced cases, DISTINCT or ORDER BY over such a CTE also depended on which read ran first.

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

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