pingcap / pingcap/tidb

Silent overflow when computing `(0) - BIGINT_MIN`, while others correctly throw out-of-range errors

Open
#67,211 4 comments 0 reactions 0 assignees View on GitHub
contribution may-affects-7.1 may-affects-7.5 may-affects-8.1 may-affects-8.5 severity/major 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!

### 1. Minimal reproduce step (Required)
```SQL
mysql> CREATE TABLE t1(a BIGINT);
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t1 VALUES (-9223372036854775808);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT a, (0) - a FROM t1;
+----------------------+----------------------+
| a | (0) - a |
+----------------------+----------------------+
| -9223372036854775808 | -9223372036854775808 |
+----------------------+----------------------+
1 row in set (0.00 sec)

mysql> SELECT a, (1) - a FROM t1;
ERROR 1690 (22003): BIGINT value is out of range in '(1 - test.t1.a)'
mysql> SELECT a, (-1) - a FROM t1;
+----------------------+---------------------+
| a | (-1) - a |
+----------------------+---------------------+
| -9223372036854775808 | 9223372036854775807 |
+----------------------+---------------------+
1 row in set (0.01 sec)
```

### 2. What did you expect to see? (Required)
`(0) - a` had better throw an error just like `(1) - a` does.
```SQL
mysql> select @@sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)

mysql> SELECT a, (0) - a FROM t1;
ERROR 1690 (22003): BIGINT value is out of range in '(0 - `test`.`t1`.`a`)'
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.45 |
+-----------+
1 row in set (0.01 sec)
```

### 3. What did you see instead (Required)
```SQL
mysql> SELECT a, (0) - a FROM t1;
+----------------------+----------------------+
| a | (0) - a |
+----------------------+----------------------+
| -9223372036854775808 | -9223372036854775808 |
+----------------------+----------------------+
1 row in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1421-g3b531d4e3d
Edition: Community
Git Commit Hash: 3b531d4e3d99cb988bebccb8f63a96afff0ec6cd
Git Branch: master
UTC Build Time: 2026-03-23 02:13:05
GoVersion: go1.25.8
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

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.