WITH ROLLUP generates duplicate group rows when grouping keys are repeated
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
```SQL
mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected (0.03 sec)
mysql> CREATE TABLE t1 (a INT, b INT, c INT);
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO t1 VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT a, b, a AS d, SUM(c) FROM t1 GROUP BY a, b, d WITH ROLLUP;
+------+------+------+--------+
| a | b | d | SUM(c) |
+------+------+------+--------+
| NULL | NULL | NULL | 18 |
| 1 | NULL | 1 | 3 |
| 4 | NULL | 4 | 6 |
| 7 | NULL | 7 | 9 |
| 7 | 8 | 7 | 9 |
| 1 | 2 | 1 | 3 |
| 1 | 2 | 1 | 3 |
| 4 | 5 | 4 | 6 |
| 4 | 5 | 4 | 6 |
| 7 | 8 | 7 | 9 |
+------+------+------+--------+
10 rows in set (0.00 sec)
```
### 2. What did you expect to see? (Required)
IMO, result had better to be consistent with MySQL 8.4 (and SQL standard logic). It should strictly follow the ROLLUP hierarchy (d -> b -> a) and should not produce duplicate rows.
```SQL
mysql> SELECT a, b, a AS d, SUM(c) FROM t1 GROUP BY a, b, d WITH ROLLUP;
+------+------+------+--------+
| a | b | d | SUM(c) |
+------+------+------+--------+
| 1 | 2 | 1 | 3 |
| 1 | 2 | NULL | 3 |
| 1 | NULL | NULL | 3 |
| 4 | 5 | 4 | 6 |
| 4 | 5 | NULL | 6 |
| 4 | NULL | NULL | 6 |
| 7 | 8 | 7 | 9 |
| 7 | 8 | NULL | 9 |
| 7 | NULL | NULL | 9 |
| NULL | NULL | NULL | 18 |
+------+------+------+--------+
10 rows in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.4.4 |
+-----------+
1 row in set (0.03 sec)
```
### 3. What did you see instead (Required)
```SQL
mysql> SELECT a, b, a AS d, SUM(c) FROM t1 GROUP BY a, b, d WITH ROLLUP;
+------+------+------+--------+
| a | b | d | SUM(c) |
+------+------+------+--------+
| NULL | NULL | NULL | 18 |
| 1 | NULL | 1 | 3 |
| 4 | NULL | 4 | 6 |
| 7 | NULL | 7 | 9 |
| 7 | 8 | 7 | 9 |
| 1 | 2 | 1 | 3 |
| 1 | 2 | 1 | 3 |
| 4 | 5 | 4 | 6 |
| 4 | 5 | 4 | 6 |
| 7 | 8 | 7 | 9 |
+------+------+------+--------+
10 rows in set (0.00 sec)
```
### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1160-g85389ef374
Edition: Community
Git Commit Hash: 85389ef3740fcc5058a5660e4382e2f1e80c0f28
Git Branch: master
UTC Build Time: 2026-02-02 03:19:11
GoVersion: go1.25.6
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.