Query with Contradictory ON and HAVING Conditions Incorrectly Returns Rows
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Hi, TiDB developers. Thanks for reading my report.
A bug has been identified where a `LEFT JOIN` query with logically contradictory conditions in the `ON` and `HAVING` clauses incorrectly returns rows when combined with a complex `WHERE NOT CASE` expression.
### 1. Minimal reproduce step (Required)
[test.sql](https://github.com/user-attachments/files/23848824/test.sql)
```sql
CREATE TABLE t0(c0 CHAR );
CREATE TABLE t1 LIKE t0;
INSERT INTO t1 VALUES ('');
INSERT INTO t0(c0) VALUES ('_');
SELECT * FROM t1 LEFT JOIN t0 ON t1.c0 LIKE t0.c0 WHERE NOT (CASE true WHEN true THEN t0.c0 WHEN false THEN (t0.c0)REGEXP('_') END) HAVING t1.c0 NOT LIKE t0.c0;
+------+------+
| c0 | c0 |
+------+------+
| | _ |
+------+------+
1 row in set, 1 warning (0.01 sec)
SELECT * FROM t1 LEFT JOIN t0 ON t1.c0 LIKE t0.c0 WHERE true HAVING t1.c0 NOT LIKE t0.c0;
empty set
```
### 2. What did you expect to see? (Required)
```sql
SELECT * FROM t1 LEFT JOIN t0 ON t1.c0 LIKE t0.c0 WHERE NOT (CASE true WHEN true THEN t0.c0 WHEN false THEN (t0.c0)REGEXP('_') END) HAVING t1.c0 NOT LIKE t0.c0;
empty set
```
### 3. What did you see instead (Required)
```sql
SELECT * FROM t1 LEFT JOIN t0 ON t1.c0 LIKE t0.c0 WHERE NOT (CASE true WHEN true THEN t0.c0 WHEN false THEN (t0.c0)REGEXP('_') END) HAVING t1.c0 NOT LIKE t0.c0;
+------+------+
| c0 | c0 |
+------+------+
| | _ |
+------+------+
1 row in set, 1 warning (0.01 sec)
```
### 4. What is your TiDB version? (Required)
| Release Version: v8.5.4
Edition: Community
Git Commit Hash: https://github.com/pingcap/tidb/commit/e4e814fdc0afe9c3a6e5e96f129d83df802ab820
Git Branch: HEAD
UTC Build Time: 2025-11-26 15:53:39
GoVersion: go1.23.12
Race Enabled: false
Check Table Before Drop: false
Store: tikv |
Contributor guide
Assessment
This issue has not been assessed yet.