pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
37.05k stars 5.83k forks source link

Able to set AUTO_INCREMENT to a value larger than maximum value of int #56034

Open fubinzh opened 1 month ago

fubinzh commented 1 month ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

  1. create a table with auto increment, and set AUTO_INCREMENT=2147483648, while max value for int is 2147483647 (https://docs.pingcap.com/tidb/stable/data-type-numeric#integer-type)
  2. insert into the value using auto increment
    
    mysql> create table t1 (a int auto_increment) AUTO_INCREMENT=2147483648;
    Query OK, 0 rows affected (0.07 sec)

mysql> insert into t1 values (); ERROR 1690 (22003): constant 2147483648 overflows int



### 2. What did you expect to see? (Required)
Create table should fail or user should be notified/warned in this case.

### 3. What did you see instead (Required)
Create table is OK, but later insert fails.

### 4. What is your TiDB version? (Required)
v8.4.0-alpha
Edition: Community
Git Commit Hash: f9c4773ba34854cec93dec3c5cae8a625925f9e9
Git Branch: heads/refs/tags/v8.4.0-alpha
adi-kmt commented 3 weeks ago

Hey @lance6716 @jebter, is this issue related to this comment? And this case?

lance6716 commented 3 weeks ago

The first URL is ALTER TABLE statement which I think is unrelated. The second URL is TableOptionAutoIdCache, but this issue is AUTO_INCREMENT (TableOptionAutoIncrement) which is some lines above it.

adi-kmt commented 3 weeks ago

So is it as simple as testing the condition and current db?

lance6716 commented 3 weeks ago

Yes I guess adding a testing here https://github.com/pingcap/tidb/blob/fd6d5c1cd0f6de9b2f000b4ba997cc79f79ae26f/pkg/ddl/create_table.go#L789-L790 is enough. But you need to find the correct way to do the check. Maybe refer to ConvertIntToInt function. And the lowerBound/upperBound should be decided by column type.

adi-kmt commented 3 weeks ago

So can I take this up?

lance6716 commented 3 weeks ago

/assign adi-kmt