pingcap / pingcap/tidb

Unexpected "index out of range" Error with NULLIF and CASE WHEN in INTERSECT

Open
#67,008 2 comments 0 reactions 0 assignees View on GitHub
contribution may-affects-7.1 may-affects-7.5 may-affects-8.1 may-affects-8.5 severity/major sig/planner type/bug
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 NULLIF interacts with the CASE WHEN statement and the INTERSECT operator. The query should not return an "index out of range" error. This could be a bug in how TiDB processes the combination of NULLIF and CASE WHEN in the context of INTERSECT.

### 1. Minimal reproduce step (Required)

```sql

DROP TABLE IF EXISTS table1;
CREATE TABLE IF NOT EXISTS table1 (active BIT, updated_at DATETIME);

INSERT INTO table1 (active, updated_at) VALUES (1, '2023-12-01 10:00:00');

-- sql 1:
SELECT CASE WHEN NULLIF(tom3.updated_at, 1) THEN tom3.active ELSE tom3.active END AS c9
FROM table1 AS tom3
INTERSECT
SELECT 1 AS c11;

-- sql 2:
SELECT tom3.active AS c9
FROM table1 AS tom3
INTERSECT
SELECT 1 AS c11;

-- sql 3:
SELECT NULLIF(tom3.updated_at, 1) AS c9
FROM table1 AS tom3
INTERSECT
SELECT 1 AS c11;
```

### 2. What did you expect to see? (Required)
I expected the queries to execute properly:

Query 1: Should return the result of the CASE WHEN expression with the intersection with the number 1.

Query 2: Should return the active value 0x01 from table1 since it intersects with 1.

Query 3: Should return an empty set because NULLIF(tom3.updated_at, 1) will not match the 1 in the intersect
### 3. What did you see instead (Required)
```sql

Query 1:

ERROR 1105 (HY000): runtime error: index out of range [7] with length 0

Query 2:

+------------+
| c9 |
+------------+
| 0x01 |
+------------+
1 row in set (0.00 sec)

Query 3:

Empty set (0.01 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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.