pingcap / pingcap/tidb

TiDB and MySQL behave differently when `cast` big number to signed

Open
#56,642 1 comment 0 reactions 0 assignees View on GitHub
affects-8.5 may-affects-5.4 may-affects-6.1 may-affects-6.5 may-affects-7.1 may-affects-7.5 may-affects-8.1 severity/major sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

### 1. Minimal reproduce step (Required)
```sql
select
(case when false then 0 else 18446744071569971956 end),
cast(18446744071569971956 as signed),
cast((case when false then 0 else 18446744071569971956 end) as signed)
;
```

### 2. What did you expect to see? (Required)
```
mysql> select
-> (case when false then 0 else 18446744071569971956 end) as c1,
-> cast(18446744071569971956 as signed) as c2,
-> cast((case when false then 0 else 18446744071569971956 end) as signed) as c3
-> ;
+----------------------+-------------+-------------+
| c1 | c2 | c3 |
+----------------------+-------------+-------------+
| 18446744071569971956 | -2139579660 | -2139579660 |
+----------------------+-------------+-------------+
1 row in set (0.00 sec)
```

### 3. What did you see instead (Required)
```
mysql> select
-> (case when false then 0 else 18446744071569971956 end) as c1,
-> cast(18446744071569971956 as signed) as c2,
-> cast((case when false then 0 else 18446744071569971956 end) as signed) as c3
-> ;
+----------------------+-------------+---------------------+
| c1 | c2 | c3 |
+----------------------+-------------+---------------------+
| 18446744071569971956 | -2139579660 | 9223372036854775807 |
+----------------------+-------------+---------------------+
1 row in set, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+-----------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------+
| Warning | 1292 | Truncated incorrect DECIMAL value: '18446744071569971956' |
+---------+------+-----------------------------------------------------------+
1 row in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)
```
Release Version: v8.4.0-alpha-370-gf773b6eeb4
Edition: Community
Git Commit Hash: f773b6eeb4593a3e2c998c265f491a016570a426
Git Branch: HEAD
UTC Build Time: 2024-10-11 02:08:09
GoVersion: go1.23.2
Race Enabled: false
Check Table Before Drop: false
Store: tikv
```

### about us
We are the BASS team from the School of Cyber Science and Technology at Beihang University. Our main focus is on system software security, operating systems, and program analysis research, as well as the development of automated program testing frameworks for detecting software defects. Using our self-developed database vulnerability testing tool, we have identified the above-mentioned vulnerabilities in TiDB that may lead to database logic error.

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.