pingcap / pingcap/tidb

TiDB incorrectly returns results due to predicate evaluation order affecting LIKE operator's behavior

Open
#64,822 3 comments 0 reactions 0 assignees View on GitHub
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, TiDB developers:
TiDB exhibits inconsistent query results when the order of predicates in the WHERE clause changes, even though logically equivalent expressions should produce identical output. The discrepancy may suggest an optimization or evaluation issue where short-circuit logic or type conversion behaves differently depending on predicate arrangement.

### 1. Minimal reproduce step (Required)
The following two queries are equivalent, but their result are different.
```sql
CREATE TABLE t0(c0 CHAR, c1 BLOB);
CREATE TABLE t1 LIKE t0;
INSERT INTO t0 VALUES ('_', '1');
INSERT INTO t1 VALUES ('ᡖ', '0');

SELECT * FROM t1 JOIN t0 WHERE t1.c0 LIKE t0.c0 AND t1.c0 NOT LIKE t0.c0 AND NOT(CASE true WHEN false THEN t1.c1 ELSE t1.c0 END);
Empty set, 1 warning (0.00 sec)

-- After changing the order in the WHERE clause, we get a different result
SELECT * FROM t1 JOIN t0 WHERE t1.c0 LIKE t0.c0 AND NOT(CASE true WHEN false THEN t1.c1 ELSE t1.c0 END) AND t1.c0 NOT LIKE t0.c0;
+------+------+------+------+
| c0 | c1 | c0 | c1 |
+------+------+------+------+
| ᡖ | 0 | _ | 1 |
+------+------+------+------+
1 row in set, 1 warning (0.00 sec)
```

### 2. What did you expect to see? (Required)
```sql
SELECT * FROM t1 JOIN t0 WHERE t1.c0 LIKE t0.c0 AND NOT(CASE true WHEN false THEN t1.c1 ELSE t1.c0 END) AND t1.c0 NOT LIKE t0.c0;
Empty set, 1 warning (0.00 sec)
```

### 3. What did you see instead (Required)
```sql
SELECT * FROM t1 JOIN t0 WHERE t1.c0 LIKE t0.c0 AND NOT(CASE true WHEN false THEN t1.c1 ELSE t1.c0 END) AND t1.c0 NOT LIKE t0.c0;
+------+------+------+------+
| c0 | c1 | c0 | c1 |
+------+------+------+------+
| ᡖ | 0 | _ | 1 |
+------+------+------+------+
1 row in set, 1 warning (0.00 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

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.