pingcap / pingcap/tidb

Inconsistent results between TiFlash and TiDB for simple BETWEEN predicate with CASE WHEN

Open
#63,771 0 comments 0 reactions 0 assignees View on GitHub
contribution sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!
I'm not sure if this is a duplicate of issue https://github.com/pingcap/tidb/issues/61449
### 1. Minimal reproduce step (Required)
```
DROP TABLE IF EXISTS tb0;
CREATE TABLE tb0 (c0 CHAR(162) NOT NULL, c1 CHAR(177), c2 BIGINT NOT NULL, c3 MEDIUMINT UNSIGNED NOT NULL);
INSERT INTO tb0 (c0, c1, c2, c3) VALUES ('y', '9', 3321678284464598783, 6829312);
ALTER TABLE tb0 SET TIFLASH REPLICA 1;

-- Query on TiKV: Returns an empty set. This is the CORRECT behavior.
SELECT /*+ read_from_storage(tikv[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783));

-- Query on TiFlash: Returns 1 row. This is the INCORRECT behavior.
SELECT /*+ read_from_storage(tiflash[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783));

-- Don't use CASE WHEN, Result: Empty set (Correct)
SELECT /*+ read_from_storage(tiflash[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN t0.c2 AND (3321678284464598783));

-- TiFlash evaluates the predicate expression correctly to 0 (false) in the SELECT list.
select (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783)) from tb0 as t0;
```

### 2. What did you expect to see? (Required)
Tiflash return empty
### 3. What did you see instead (Required)
```
mysql> SELECT /*+ read_from_storage(tiflash[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783));
+---------------------+
| col_0 |
+---------------------+
| 3321678284464598783 |
+---------------------+
1 row in set (0.007 sec)

mysql> SELECT /*+ read_from_storage(tikv[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783));
Empty set (0.001 sec)

mysql> SELECT /*+ read_from_storage(tiflash[t0]) */ t0.c2 AS col_0 FROM tb0 AS t0 WHERE (t0.c3 BETWEEN t0.c2 AND (3321678284464598783));
Empty set (0.010 sec)

mysql> select (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783)) from tb0 as t0;
+--------------------------------------------------------------------------------------------------+
| (t0.c3 BETWEEN (CASE WHEN (t0.c1 != false) THEN t0.c2 ELSE t0.c3 END) AND (3321678284464598783)) |
+--------------------------------------------------------------------------------------------------+
| 0 |
+--------------------------------------------------------------------------------------------------+
1 row in set (0.002 sec)
```
### 4. What is your TiDB version? (Required)
```
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-250-ga6d415f24a
Edition: Community
Git Commit Hash: a6d415f24a7d182ebca3d189c457d9044537028f
Git Branch: HEAD
UTC Build Time: 2025-08-03 10:01:12
GoVersion: go1.23.11
Race Enabled: false
Check Table Before Drop: false
Store: tikv
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

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.