tidb icon indicating copy to clipboard operation
tidb copied to clipboard

DISTINCT incorrectly merges different GROUPING() results in a WITH ROLLUP query

Open guijinxin opened this issue 1 month ago • 3 comments

Bug Report

1. Minimal reproduce step (Required)

Run the following statements in a fresh, empty database with default session settings:

CREATE TABLE t (a INT, b INT);
INSERT INTO t VALUES (1, 2);

SELECT GROUPING(a) AS ga, GROUPING(b) AS gb
FROM t
GROUP BY a, b WITH ROLLUP;

SELECT DISTINCT GROUPING(a) AS ga, GROUPING(b) AS gb
FROM t
GROUP BY a, b WITH ROLLUP;

The issue reproduces using the native mysql client, independently of the test harness. Cascades is disabled.

2. What did you expect to see? (Required)

Both SELECT statements should return the same three distinct rows, in any order:

ga  gb
0   0
0   1
1   1

These represent the detail group, the subtotal for a, and the grand total, respectively. Since all three (ga, gb) pairs are different, adding DISTINCT should not remove any row.

3. What did you see instead (Required)

The query without DISTINCT returns the expected three rows. The query with DISTINCT returns only two rows. For example, the native client returned:

ga  gb
0   0
1   1

In other executions, it returned (0, 1) and (1, 1) instead. Which of (0, 0) and (0, 1) survives can vary, but one of these distinct rows is incorrectly removed.

EXPLAIN FORMAT='brief' for the DISTINCT query shows that the outer aggregation groups by the same column twice, although the projection produces separate columns for the two GROUPING expressions:

HashAgg
  group by:Column#13, Column#13,
  funcs:firstrow(Column#13)->Column#7,
        firstrow(Column#14)->Column#8
└─Projection
    grouping(gid)->Column#13, grouping(gid)->Column#14
  └─HashAgg
      group by:Column#4, Column#5, gid,
      funcs:firstrow(gid)->gid
    └─Expand
      ...

This is consistent with DISTINCT using only the first GROUPING result to distinguish rows, causing (0, 0) and (0, 1) to be merged. The exact source-level root cause has not been confirmed.

Additional observations:

  • Reversing the order of GROUPING(a) and GROUPING(b) still returns only two rows.

  • SELECT DISTINCT GROUPING(a,b) FROM t GROUP BY a,b WITH ROLLUP correctly returns three masks: 0, 1, and 3.

  • Casting the second GROUPING result to CHAR makes the two-column DISTINCT query return all three rows:

    SELECT DISTINCT GROUPING(a) AS ga,
                    CAST(GROUPING(b) AS CHAR) AS gb
    FROM t
    GROUP BY a, b WITH ROLLUP;
    

4. What is your TiDB version? (Required)

Output of SELECT tidb_version():

Release Version: v8.5.8
Edition: Community
Git Commit Hash: 8b857efa20363d50a8fa2ea7dd9809a85a61b115
Git Branch: HEAD
UTC Build Time: 2026-08-27 07:20:33
GoVersion: go1.25.12
Race Enabled: false
Check Table Before Drop: false
Store: unistore

guijinxin avatar Oct 25 '26 04:10 guijinxin

/sig planner /severity major

kennedy8312 avatar Oct 25 '26 08:10 kennedy8312

tidb-triage-bot AI rationale

  • severity/major: DISTINCT returns incorrect results for separate GROUPING expressions with WITH ROLLUP, but casting the second result to CHAR provides a workaround.
  • sig/planner: The DISTINCT aggregation plan groups by the same projected column twice despite separate GROUPING expressions, indicating incorrect planner expression handling.

kennedy8312 avatar Oct 25 '26 08:10 kennedy8312

Thanks for investigating and opening the fix!

guijinxin avatar Nov 20 '26 08:11 guijinxin