Bug #121083 SELECT DISTINCT ... GROUP BY <same list> drops rows
Submitted: 12 Aug 20:39 Modified: 13 Aug 6:27
Reporter: Junwen An Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.46,8.4.11, 9.7,2 OS:Linux
Assigned to: CPU Architecture:ARM

[12 Aug 20:39] Junwen An
Description:
For `SELECT DISTINCT <cols> FROM ... GROUP BY <same cols> ORDER BY ...`, when one of the columns is a boolean `||` (logical OR — no `PIPES_AS_CONCAT`) expression over a column read through a **merged VIEW**, and the `FROM` involves a join, MySQL's post-`GROUP BY` `DISTINCT` step silently omits that expression from its own deduplication key. Please see the repro

How to repeat:
CREATE TABLE t (c_pk BIGINT NOT NULL, c_txt VARCHAR(255), c_chr VARCHAR(255));
INSERT INTO t VALUES (1, NULL, NULL);
INSERT INTO t VALUES (2, 'a', 'a');
INSERT INTO t VALUES (3, 'o''brien', '');
INSERT INTO t VALUES (4, NULL, 'Zed');

CREATE VIEW t0 AS SELECT * FROM t;

SELECT a.c_pk, (t0.c_txt||t0.c_chr) FROM t a, t0 GROUP BY a.c_pk, (t0.c_txt||t0.c_chr) ORDER BY a.c_pk;
-- => (1,NULL),(1,0),(2,NULL),(2,0),(3,NULL),(3,0),(4,NULL),(4,0)
-- expected result

SELECT DISTINCT a.c_pk, (t0.c_txt||t0.c_chr) FROM t a, t0 GROUP BY a.c_pk, (t0.c_txt||t0.c_chr) ORDER BY a.c_pk;
-- => (1,NULL),(2,NULL),(3,NULL),(4,NULL)      
-- unexpected results after adding DISTINCT, which should be the same 8 rows as the above query
[13 Aug 6:27] Chaithra Marsur Gopala Reddy
Hi Junwen An,

Thank you for the test case. Verified as described.