Inconsistent Results for ABS and MAX Aggregation with and without PRIMARY KEY
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
It seems that the query produces different results depending on the presence of a PRIMARY KEY constraint on the c0 column. The behavior of the ABS and MAX aggregation functions changes unexpectedly when the PRIMARY KEY is applied, even though the query is identical in both cases. This suggests there might be an issue related to how TiDB processes aggregations with the PRIMARY KEY.
### 1. Minimal reproduce step (Required)
```sql
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;
-- Create table with PRIMARY KEY constraint
CREATE TABLE t0(c0 CHAR , c1 NUMERIC , c2 TEXT(423) , PRIMARY KEY(c0));
INSERT INTO t0(c0, c2, c1) VALUES (1, 1, 1);
-- Query 1: With PRIMARY KEY constraint
SELECT ABS(MAX(b'10')) AS c1
FROM t0 AS tom0
GROUP BY tom0.c0
ORDER BY c1;
-- Drop table and recreate without PRIMARY KEY constraint
DROP TABLE t0;
CREATE TABLE t0(c0 CHAR , c1 NUMERIC , c2 TEXT(423));
-- Insert data into t0
INSERT INTO t0(c0, c2, c1) VALUES (1, 1, 1);
-- Query 2: Without PRIMARY KEY constraint
SELECT ABS(MAX(b'10')) AS c1
FROM t0 AS tom0
GROUP BY tom0.c0
ORDER BY c1;
```
### 2. What did you expect to see? (Required)
I expected both queries to return the same result, as the logic of the aggregation (i.e., MAX(b'10') and ABS) should not depend on whether the PRIMARY KEY constraint is applied to the column.
### 3. What did you see instead (Required)
```sql
Output for Query 1:
+------+
| c1 |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
Output for Query 2:
+------+
| c1 |
+------+
| 0 |
+------+
1 row in set, 1 warning (0.00 sec)
```
### 4. What is your TiDB version? (Required)
```sql
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v8.5.5
Edition: Community
Git Commit Hash: 1fa258b833ff113883beeba40bc130be7ce66610
Git Branch: HEAD
UTC Build Time: 2026-03-12 08:34:59
GoVersion: go1.25.5
Race Enabled: false
Check Table Before Drop: false
Store: unistore |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.