diff --git a/ddl/reorg.go b/ddl/reorg.go index 60c158bb7dc32..d03b73e133eb5 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -289,8 +289,8 @@ func getColumnsTypes(columns []*model.ColumnInfo) []*types.FieldType { } // buildDescTableScan builds a desc table scan upon tblInfo. -func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, columns []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { - sctx := newContext(d.store) +func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, columns []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { + sctx := newContext(dc.store) dagPB, err := buildDescTableScanDAG(sctx, tbl, columns, limit) if err != nil { return nil, errors.Trace(err) @@ -320,7 +320,7 @@ func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl tab } // GetTableMaxRowID gets the last row id of the table partition. -func (d *ddlCtx) GetTableMaxRowID(startTS uint64, tbl table.PhysicalTable) (maxRowID int64, emptyTable bool, err error) { +func (dc *ddlCtx) GetTableMaxRowID(startTS uint64, tbl table.PhysicalTable) (maxRowID int64, emptyTable bool, err error) { maxRowID = int64(math.MaxInt64) var columns []*model.ColumnInfo if tbl.Meta().PKIsHandle { @@ -336,7 +336,7 @@ func (d *ddlCtx) GetTableMaxRowID(startTS uint64, tbl table.PhysicalTable) (maxR ctx := context.Background() // build a desc scan of tblInfo, which limit is 1, we can use it to retrieve the last handle of the table. - result, err := d.buildDescTableScan(ctx, startTS, tbl, columns, 1) + result, err := dc.buildDescTableScan(ctx, startTS, tbl, columns, 1) if err != nil { return maxRowID, false, errors.Trace(err) } diff --git a/executor/ddl_test.go b/executor/ddl_test.go index eea0626467500..2a11116f417c4 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -808,6 +808,14 @@ type testAutoRandomSuite struct { *baseTestSuite } +func (s *testAutoRandomSuite) SetUpTest(c *C) { + testutil.ConfigTestUtils.SetupAutoRandomTestConfig() +} + +func (s *testAutoRandomSuite) TearDownTest(c *C) { + testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() +} + func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -822,9 +830,6 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { return allHds } - testutil.ConfigTestUtils.SetupAutoRandomTestConfig() - defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() - tk.MustExec("create table t (a bigint primary key auto_random(15), b int)") for i := 0; i < 100; i++ { tk.MustExec("insert into t(b) values (?)", i) @@ -931,9 +936,6 @@ func (s *testAutoRandomSuite) TestAutoRandomTableOption(c *C) { tk.MustExec("use test") // test table option is auto-random - testutil.ConfigTestUtils.SetupAutoRandomTestConfig() - defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() - tk.MustExec("drop table if exists auto_random_table_option") tk.MustExec("create table auto_random_table_option (a bigint auto_random(5) key) auto_random_base = 1000") t, err := domain.GetDomain(tk.Se).InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("auto_random_table_option")) @@ -1003,9 +1005,6 @@ func (s *testAutoRandomSuite) TestFilterDifferentAllocators(c *C) { tk.MustExec("drop table if exists t") tk.MustExec("drop table if exists t1") - testutil.ConfigTestUtils.SetupAutoRandomTestConfig() - defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() - tk.MustExec("create table t(a bigint auto_random(5) key, b int auto_increment unique)") tk.MustExec("insert into t values()") tk.MustQuery("select b from t").Check(testkit.Rows("1")) diff --git a/executor/show.go b/executor/show.go index 0e050babbb6ac..dea0ead1f0e42 100644 --- a/executor/show.go +++ b/executor/show.go @@ -719,7 +719,7 @@ func getDefaultCollate(charsetName string) string { } // ConstructResultOfShowCreateTable constructs the result for show create table. -func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.TableInfo, allocator autoid.Allocator, buf *bytes.Buffer) (err error) { +func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.TableInfo, allocators autoid.Allocators, buf *bytes.Buffer) (err error) { if tableInfo.IsView() { fetchShowCreateTable4View(ctx, tableInfo, buf) return nil @@ -902,11 +902,13 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T fmt.Fprintf(buf, " COMPRESSION='%s'", tableInfo.Compression) } - if hasAutoIncID { - autoIncID, err := allocator.NextGlobalAutoID(tableInfo.ID) + incrementAllocator := allocators.Get(autoid.RowIDAllocType) + if hasAutoIncID && incrementAllocator != nil { + autoIncID, err := incrementAllocator.NextGlobalAutoID(tableInfo.ID) if err != nil { return errors.Trace(err) } + // It's compatible with MySQL. if autoIncID > 1 { fmt.Fprintf(buf, " AUTO_INCREMENT=%d", autoIncID) @@ -917,8 +919,16 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T fmt.Fprintf(buf, " /*T![auto_id_cache] AUTO_ID_CACHE=%d */", tableInfo.AutoIdCache) } - if tableInfo.AutoRandID != 0 { - fmt.Fprintf(buf, " /*T![auto_rand_base] AUTO_RANDOM_BASE=%d */", tableInfo.AutoRandID) + randomAllocator := allocators.Get(autoid.AutoRandomType) + if randomAllocator != nil { + autoRandID, err := randomAllocator.NextGlobalAutoID(tableInfo.ID) + if err != nil { + return errors.Trace(err) + } + + if autoRandID > 1 { + fmt.Fprintf(buf, " /*T![auto_rand_base] AUTO_RANDOM_BASE=%d */", autoRandID) + } } if tableInfo.ShardRowIDBits > 0 { @@ -1012,10 +1022,9 @@ func (e *ShowExec) fetchShowCreateTable() error { } tableInfo := tb.Meta() - allocator := tb.Allocators(e.ctx).Get(autoid.RowIDAllocType) var buf bytes.Buffer // TODO: let the result more like MySQL. - if err = ConstructResultOfShowCreateTable(e.ctx, tableInfo, allocator, &buf); err != nil { + if err = ConstructResultOfShowCreateTable(e.ctx, tableInfo, tb.Allocators(e.ctx), &buf); err != nil { return err } if tableInfo.IsView() { diff --git a/executor/show_test.go b/executor/show_test.go index dde5a7793370c..cf1c6617a67db 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -19,6 +19,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/errors" + "github.com/pingcap/failpoint" "github.com/pingcap/parser/auth" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" @@ -682,9 +683,6 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - testutil.ConfigTestUtils.SetupAutoRandomTestConfig() - defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() - // Basic show create table. tk.MustExec("create table auto_random_tbl1 (a bigint primary key auto_random(3), b varchar(255))") tk.MustQuery("show create table `auto_random_tbl1`").Check(testutil.RowsWithSep("|", @@ -783,6 +781,39 @@ func (s *testAutoRandomSuite) TestAutoIdCache(c *C) { )) } +func (s *testAutoRandomSuite) TestAutoRandomBase(c *C) { + c.Assert(failpoint.Enable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange", `return(true)`), IsNil) + defer func() { + c.Assert(failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"), IsNil) + }() + + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a bigint primary key auto_random(5), b int unique key auto_increment) auto_random_base = 100, auto_increment = 100") + tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|", + ""+ + "t CREATE TABLE `t` (\n"+ + " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ + " `b` int(11) NOT NULL AUTO_INCREMENT,\n"+ + " PRIMARY KEY (`a`),\n"+ + " UNIQUE KEY `b` (`b`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=100 /*T![auto_rand_base] AUTO_RANDOM_BASE=100 */", + )) + + tk.MustExec("insert into t(`a`) values (1000)") + tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|", + ""+ + "t CREATE TABLE `t` (\n"+ + " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ + " `b` int(11) NOT NULL AUTO_INCREMENT,\n"+ + " PRIMARY KEY (`a`),\n"+ + " UNIQUE KEY `b` (`b`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=5100 /*T![auto_rand_base] AUTO_RANDOM_BASE=6001 */", + )) +} + func (s *testSuite5) TestShowEscape(c *C) { tk := testkit.NewTestKit(c, s.store)