Skip to content

Commit

Permalink
ddl: allow altering auto_increment to a large number (#19241)
Browse files Browse the repository at this point in the history
* ddl: allow altering auto_increment to a large number

* fix the edge case problem of auto_inc option

* revert last commit

Co-authored-by: Arenatlx <ailinsilence4@gmail.com>
Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 1, 2020
1 parent e62e1fe commit 2ae1cc1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2401,16 +2401,26 @@ func (s *testIntegrationSuite5) TestDropColumnsWithMultiIndex(c *C) {
tk.MustQuery(query).Check(testkit.Rows())
}

func (s *testIntegrationSuite7) TestAutoIncrementAllocator(c *C) {
func (s *testIntegrationSuite7) TestAutoIncrementTableOption(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
// Make sure the integer primary key is the handle(PkIsHandle).
conf.AlterPrimaryKey = false
})
tk.MustExec("drop database if exists test_create_table_option_auto_inc;")
tk.MustExec("create database test_create_table_option_auto_inc;")
tk.MustExec("use test_create_table_option_auto_inc;")
tk.MustExec("drop database if exists test_auto_inc_table_opt;")
tk.MustExec("create database test_auto_inc_table_opt;")
tk.MustExec("use test_auto_inc_table_opt;")

// Empty auto_inc allocator should not cause error.
tk.MustExec("create table t (a bigint primary key) auto_increment = 10;")
tk.MustExec("alter table t auto_increment = 10;")
tk.MustExec("alter table t auto_increment = 12345678901234567890;")

// Rebase the auto_inc allocator to a large integer should work.
tk.MustExec("drop table t;")
tk.MustExec("create table t (a bigint unsigned auto_increment, unique key idx(a));")
tk.MustExec("alter table t auto_increment = 12345678901234567890;")
tk.MustExec("insert into t values ();")
tk.MustQuery("select * from t;").Check(testkit.Rows("12345678901234567890"))
}
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2392,7 +2392,7 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6
// If the user sends SQL `alter table t1 auto_increment = 100` to TiDB-B,
// and TiDB-B finds 100 < 30001 but returns without any handling,
// then TiDB-A may still allocate 99 for auto_increment column. This doesn't make sense for the user.
newBase = mathutil.MaxInt64(newBase, autoID)
newBase = int64(mathutil.MaxUint64(uint64(newBase), uint64(autoID)))
}
job := &model.Job{
SchemaID: schema.ID,
Expand Down

0 comments on commit 2ae1cc1

Please sign in to comment.