pingcap / pingcap/tidb

Unexpected Error in HAVING Clause with Contradictory IS NULL/IS NOT NULL Logic on LEFT JOIN

Open
#67,851 4 comments 0 reactions 0 assignees View on GitHub
affects-7.1 affects-7.5 affects-8.1 affects-8.5 contribution severity/major sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Hi, I found a bug in TiDB 8.5.6. Executing a `LEFT JOIN` query where the `HAVING` clause contains a specific condition `x IS NOT NULL AND NOT (x IS NOT NULL) AND x IS NULL` causes TiDB to throw an internal panic (ERROR 1105) instead of returning an empty result set.

### 1. Minimal reproduce step (Required)

```sql
CREATE TABLE t0(c0 CHAR);
CREATE TABLE t1(c0 CHAR, PRIMARY KEY(c0));
INSERT INTO t1 VALUES ('1'),('2');
SELECT * FROM t0 LEFT JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND t1.c0 IS NULL; -- Empty set (0.01 sec)√
SELECT * FROM t0 LEFT JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND NOT (t1.c0 IS NOT NULL); --Empty set (0.01 sec)√
SELECT * FROM t0 INNER JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND NOT (t1.c0 IS NOT NULL) AND t1.c0 IS NULL; --Empty set (0.01 sec)√
SELECT * FROM t0 LEFT JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND NOT (t1.c0 IS NOT NULL) AND t1.c0 IS NULL; -- expect: Empty set
ERROR 1105 (HY000): interface conversion: expression.Expression is *expression.Constant, not *expression.ScalarFunction
```

### 2. What did you expect to see? (Required)
```sql
SELECT * FROM t0 LEFT JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND NOT (t1.c0 IS NOT NULL) AND t1.c0 IS NULL;
Empty set (0.01 sec)
```

### 3. What did you see instead (Required)
```sql
SELECT * FROM t0 LEFT JOIN t1 ON TRUE HAVING t1.c0 IS NOT NULL AND NOT (t1.c0 IS NOT NULL) AND t1.c0 IS NULL;
ERROR 1105 (HY000): interface conversion: expression.Expression is *expression.Constant, not *expression.ScalarFunction
```
### 4. What is your TiDB version? (Required)

| Release Version: v8.5.6
Edition: Community
Git Commit Hash: ae18096e023780bb56bfce33698abec0d4640d0a
Git Branch: HEAD
UTC Build Time: 2026-04-14 07:15:36
GoVersion: go1.25.8
Race Enabled: false
Check Table Before Drop: false
Store: tikv |

Contributor guide

Open the contributing guide

Research direction

The issue names no source file or test. Start by running the minimal LEFT JOIN and HAVING reproduction against TiDB, then trace the expression handling for contradictory IS NULL conditions and the reported Constant-versus-ScalarFunction panic. Done means the query returns an empty set without ERROR 1105 and regression coverage is added.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.