Unexpected "index out of range" Error with COALESCE and MIN Aggregation
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
It seems that the issue may be related to how COALESCE interacts with the MIN aggregation function. The query should not return an "index out of range" error. This could be a bug in how TiDB processes the combination of COALESCE and aggregation functions such as MIN.
### 1. Minimal reproduce step (Required)
```sql
DROP TABLE IF EXISTS table1;
CREATE TABLE IF NOT EXISTS table1 (active BIT);
INSERT INTO table1 (active) VALUES (0);
-- sql 1:
SELECT MIN(COALESCE(tom0.active)) AS c0
FROM table1 AS tom0
GROUP BY tom0.active;
-- sql 2:
SELECT MIN(tom0.active) AS c0
FROM table1 AS tom0
GROUP BY tom0.active;
-- sql 3:
SELECT (COALESCE(tom0.active)) AS c0
FROM table1 AS tom0
GROUP BY tom0.active;
```
### 2. What did you expect to see? (Required)
I expected to see the correct result of the aggregation, which should return the minimum value of the active column or 0 as specified by the COALESCE function.
### 3. What did you see instead (Required)
```sql
sql1 output:
ERROR 1105 (HY000): runtime error: index out of range [7] with length 0
sql2 output:
+------------+
| c0 |
+------------+
| 0x00 |
+------------+
1 row in set (0.00 sec)
sql3 output:
+------------+
| c0 |
+------------+
| 0x00 |
+------------+
1 row in set (0.00 sec)
```
The query that uses MIN(COALESCE(...)) returns an unexpected error:
ERROR 1105 (HY000): runtime error: index out of range [7] with length 0
### 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.