From baf32d20859805379bc8088cb07d3ab32310af39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E6=89=8B=E6=8E=89=E5=8C=85=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E5=B8=88?= Date: Wed, 1 Jul 2020 10:45:26 +0800 Subject: [PATCH 1/6] cherry pick #18217 to release-4.0 Signed-off-by: ti-srebot --- ddl/reorg.go | 24 ++++++++++++++++++++++++ executor/ddl_test.go | 18 ++++++++++++------ executor/show.go | 23 ++++++++++++++++------- executor/show_test.go | 38 +++++++++++++++++++++++++++++++++++--- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index 60c158bb7dc32..1d89f1fa2fb24 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -289,9 +289,16 @@ func getColumnsTypes(columns []*model.ColumnInfo) []*types.FieldType { } // buildDescTableScan builds a desc table scan upon tblInfo. +<<<<<<< HEAD func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, columns []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { sctx := newContext(d.store) dagPB, err := buildDescTableScanDAG(sctx, tbl, columns, limit) +======= +func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, + handleCols []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { + sctx := newContext(dc.store) + dagPB, err := buildDescTableScanDAG(sctx, tbl, handleCols, limit) +>>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) if err != nil { return nil, errors.Trace(err) } @@ -319,11 +326,20 @@ func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl tab return result, nil } +<<<<<<< HEAD // 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) { maxRowID = int64(math.MaxInt64) var columns []*model.ColumnInfo if tbl.Meta().PKIsHandle { +======= +// GetTableMaxHandle gets the max handle of a PhysicalTable. +func (dc *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (maxHandle kv.Handle, emptyTable bool, err error) { + var handleCols []*model.ColumnInfo + tblInfo := tbl.Meta() + switch { + case tblInfo.PKIsHandle: +>>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) for _, col := range tbl.Meta().Columns { if mysql.HasPriKeyFlag(col.Flag) { columns = []*model.ColumnInfo{col} @@ -336,7 +352,11 @@ 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. +<<<<<<< HEAD result, err := d.buildDescTableScan(ctx, startTS, tbl, columns, 1) +======= + result, err := dc.buildDescTableScan(ctx, startTS, tbl, handleCols, 1) +>>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) if err != nil { return maxRowID, false, errors.Trace(err) } @@ -352,6 +372,10 @@ func (d *ddlCtx) GetTableMaxRowID(startTS uint64, tbl table.PhysicalTable) (maxR // empty table return maxRowID, true, nil } +<<<<<<< HEAD +======= + sessCtx := newContext(dc.store) +>>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) row := chk.GetRow(0) maxRowID = row.GetInt64(0) return maxRowID, false, nil diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 175656f2333af..ae1d9e52d14fb 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,8 +830,12 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { return allHds } +<<<<<<< HEAD testutil.ConfigTestUtils.SetupAutoRandomTestConfig() defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() +======= + tk.MustExec("set @@allow_auto_random_explicit_insert = true") +>>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) tk.MustExec("create table t (a bigint primary key auto_random(15), b int)") for i := 0; i < 100; i++ { @@ -913,9 +925,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")) @@ -985,9 +994,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..07b15b54566f6 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,40 @@ 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("set @@allow_auto_random_explicit_insert = true") + 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) From ad2b200c71801b8e121c1339e7820a30d800d798 Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Wed, 1 Jul 2020 22:15:44 +0800 Subject: [PATCH 2/6] executor: resolve conflicts Signed-off-by: Rustin-Liu --- ddl/reorg.go | 29 ++++------------------------- executor/ddl_test.go | 5 ----- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index 1d89f1fa2fb24..5f30d3e22d69c 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -289,19 +289,14 @@ func getColumnsTypes(columns []*model.ColumnInfo) []*types.FieldType { } // buildDescTableScan builds a desc table scan upon tblInfo. -<<<<<<< HEAD -func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, columns []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { - sctx := newContext(d.store) - dagPB, err := buildDescTableScanDAG(sctx, tbl, columns, limit) -======= func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, handleCols []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { sctx := newContext(dc.store) dagPB, err := buildDescTableScanDAG(sctx, tbl, handleCols, limit) ->>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) if err != nil { return nil, errors.Trace(err) } + ranges := ranger.FullIntRange(false) var builder distsql.RequestBuilder builder.SetTableRanges(tbl.GetPhysicalID(), ranges, nil). @@ -326,20 +321,11 @@ func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl ta return result, nil } -<<<<<<< HEAD // 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 { -======= -// GetTableMaxHandle gets the max handle of a PhysicalTable. -func (dc *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (maxHandle kv.Handle, emptyTable bool, err error) { - var handleCols []*model.ColumnInfo - tblInfo := tbl.Meta() - switch { - case tblInfo.PKIsHandle: ->>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) for _, col := range tbl.Meta().Columns { if mysql.HasPriKeyFlag(col.Flag) { columns = []*model.ColumnInfo{col} @@ -352,11 +338,7 @@ func (dc *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (ma 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. -<<<<<<< HEAD - result, err := d.buildDescTableScan(ctx, startTS, tbl, columns, 1) -======= - result, err := dc.buildDescTableScan(ctx, startTS, tbl, handleCols, 1) ->>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) + result, err := dc.buildDescTableScan(ctx, startTS, tbl, columns, 1) if err != nil { return maxRowID, false, errors.Trace(err) } @@ -372,10 +354,7 @@ func (dc *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (ma // empty table return maxRowID, true, nil } -<<<<<<< HEAD -======= - sessCtx := newContext(dc.store) ->>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) + row := chk.GetRow(0) maxRowID = row.GetInt64(0) return maxRowID, false, nil diff --git a/executor/ddl_test.go b/executor/ddl_test.go index ae1d9e52d14fb..34490446c0eec 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -830,12 +830,7 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { return allHds } -<<<<<<< HEAD - testutil.ConfigTestUtils.SetupAutoRandomTestConfig() - defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig() -======= tk.MustExec("set @@allow_auto_random_explicit_insert = true") ->>>>>>> aa1d2d2... executor,ddl: update auto_random_base in 'show create table' after insertion (#18217) tk.MustExec("create table t (a bigint primary key auto_random(15), b int)") for i := 0; i < 100; i++ { From b543bd587caecdaa0c2119c6155dfe600f9beb63 Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Wed, 1 Jul 2020 22:17:24 +0800 Subject: [PATCH 3/6] executor: resolve conflicts Signed-off-by: Rustin-Liu --- ddl/reorg.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index 5f30d3e22d69c..b62391de14cb9 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -40,7 +40,6 @@ import ( "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/ranger" "github.com/pingcap/tidb/util/sqlexec" - "github.com/pingcap/tipb/go-tipb" "go.uber.org/zap" ) From 78944f9ae1faee018756fdc7ad86c5c992c2d899 Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Wed, 1 Jul 2020 23:07:45 +0800 Subject: [PATCH 4/6] executor: resolve conflicts Signed-off-by: Rustin-Liu --- ddl/reorg.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index b62391de14cb9..7214eab41736e 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -288,14 +288,12 @@ func getColumnsTypes(columns []*model.ColumnInfo) []*types.FieldType { } // buildDescTableScan builds a desc table scan upon tblInfo. -func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable, - handleCols []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) { +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, handleCols, limit) + dagPB, err := buildDescTableScanDAG(sctx, tbl, columns, limit) if err != nil { return nil, errors.Trace(err) } - ranges := ranger.FullIntRange(false) var builder distsql.RequestBuilder builder.SetTableRanges(tbl.GetPhysicalID(), ranges, nil). @@ -353,7 +351,6 @@ func (dc *ddlCtx) GetTableMaxRowID(startTS uint64, tbl table.PhysicalTable) (max // empty table return maxRowID, true, nil } - row := chk.GetRow(0) maxRowID = row.GetInt64(0) return maxRowID, false, nil From 378417c7a4071ae40115a8f6214d14d08cdb2eb9 Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Wed, 1 Jul 2020 23:10:56 +0800 Subject: [PATCH 5/6] executor: resolve conflicts Signed-off-by: Rustin-Liu --- ddl/reorg.go | 1 + executor/ddl_test.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index 7214eab41736e..d03b73e133eb5 100755 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -40,6 +40,7 @@ import ( "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/ranger" "github.com/pingcap/tidb/util/sqlexec" + "github.com/pingcap/tipb/go-tipb" "go.uber.org/zap" ) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 34490446c0eec..d701d97ef5acd 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -830,8 +830,6 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { return allHds } - tk.MustExec("set @@allow_auto_random_explicit_insert = true") - 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) From d51cade7a64cab063d9cf72f0605eb53f2ea43af Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Wed, 1 Jul 2020 23:19:37 +0800 Subject: [PATCH 6/6] executor: remove allow_auto_random_explicit_insert = true Signed-off-by: Rustin-Liu --- executor/show_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/executor/show_test.go b/executor/show_test.go index 07b15b54566f6..cf1c6617a67db 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -788,7 +788,6 @@ func (s *testAutoRandomSuite) TestAutoRandomBase(c *C) { }() tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@allow_auto_random_explicit_insert = true") tk.MustExec("use test") tk.MustExec("drop table if exists t")