TiDB throws overflow error for CAST in WHERE clause depending on condition order, leading to inconsistent behavior
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
When a `CAST(t0.c0 AS DECIMAL)` expression is used in a `WHERE` clause together with other conditions, TiDB may either return an overflow error or successfully execute the query, depending on the order of the conditions.
### 1. Minimal reproduce step (Required)
```sql
CREATE TABLE t0(c0 DOUBLE);
INSERT INTO t0(c0) VALUES (1E81); -- "INSERT INTO t0(c0) VALUES (1E80)" would trigger the following ERROR message.
SELECT * FROM t0 WHERE t0.c0 > 1 AND CAST(t0.c0 AS DECIMAL) AND t0.c0 <= 1;
ERROR 1105 (HY000): [components/tidb_query_datatype/src/codec/mysql/decimal.rs:1916]: parsing 1000000000000000000000000000000000000000000000000000000000000000000000000000000000 will overflow
SELECT * FROM t0 WHERE t0.c0 > 1 AND t0.c0 <= 1 AND CAST(t0.c0 AS DECIMAL);
Empty set (0.00 sec)
```
### 2. What did you expect to see? (Required)
```sql
SELECT * FROM t0 WHERE t0.c0 > 1 AND CAST(t0.c0 AS DECIMAL) AND t0.c0 <= 1;
Empty set (0.00 sec)
```
### 3. What did you see instead (Required)
```sql
SELECT * FROM t0 WHERE t0.c0 > 1 AND CAST(t0.c0 AS DECIMAL) AND t0.c0 <= 1;
ERROR 1105 (HY000): [components/tidb_query_datatype/src/codec/mysql/decimal.rs:1916]: parsing 1000000000000000000000000000000000000000000000000000000000000000000000000000000000 will overflow
```
### 4. What is your TiDB version? (Required)
| Release Version: v8.5.5
Edition: Community
Git Commit Hash: 1fa258b833ff113883beeba40bc130be7ce66610
Git Branch: HEAD
UTC Build Time: 2026-01-14 22:20:57
GoVersion: go1.25.5
Race Enabled: false
Check Table Before Drop: false
Store: tikv |
Contributor guide
Assessment
This issue has not been assessed yet.