From 2ae1cc175f426b24e73c4c9ccf00b95de026eb16 Mon Sep 17 00:00:00 2001 From: tangenta Date: Tue, 1 Sep 2020 21:38:31 +0800 Subject: [PATCH] ddl: allow altering auto_increment to a large number (#19241) * 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 Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com> --- ddl/db_integration_test.go | 18 ++++++++++++++---- ddl/ddl_api.go | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index d36e49c4d49e5..33f80185c0886 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -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")) } diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index be9ace8ee9edb..a143347db6f72 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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,