Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor,ddl: update auto_random_base in 'show create table' after insertion #18217

Merged
merged 45 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
975cb41
executor,ddl: update auto_random_base in 'show create table' after in…
Rustin170506 Jun 26, 2020
57a4871
executor: fix logic
Rustin170506 Jun 26, 2020
be8b55e
executor: remove var
Rustin170506 Jun 26, 2020
117c600
executor: refine logic
Rustin170506 Jun 26, 2020
d0b6606
executor: refine logic
Rustin170506 Jun 26, 2020
8a230be
executor: fix sql
Rustin170506 Jun 26, 2020
62cb14a
executor: fix sql
Rustin170506 Jun 26, 2020
ce2fe25
executor: fix sql
Rustin170506 Jun 26, 2020
7c2bf61
executor: fix assert
Rustin170506 Jun 26, 2020
20fe777
executor: fix assert
Rustin170506 Jun 26, 2020
8405829
executor: add more cases
Rustin170506 Jun 26, 2020
167cf8d
executor: use different Allocator
Rustin170506 Jun 28, 2020
1c35b26
executor: use different Allocator
Rustin170506 Jun 28, 2020
6c3769c
executor: use different Allocator
Rustin170506 Jun 28, 2020
13127ae
executor: use different Allocator
Rustin170506 Jun 28, 2020
e957c45
executor: use different Allocator
Rustin170506 Jun 28, 2020
a60b9ce
executor: fix test
Rustin170506 Jun 28, 2020
1519e56
executor: refine logic
Rustin170506 Jun 28, 2020
8f24cff
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 28, 2020
a061b04
executor: refine logic
Rustin170506 Jun 29, 2020
c7421c7
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 29, 2020
79b6a2d
executor: fix type
Rustin170506 Jun 29, 2020
51f8084
executor: add blank line
Rustin170506 Jun 29, 2020
f10b42f
executor: add test config
Rustin170506 Jun 29, 2020
a5d5ae1
Merge branch 'master' into rustin-patch-auto
AilinKid Jun 29, 2020
461e98c
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 29, 2020
a72f5bd
executor: add test setup and Teardown
Rustin170506 Jun 29, 2020
582f249
executor: use test setup and Teardown
Rustin170506 Jun 29, 2020
b830703
executor: remove useless code
Rustin170506 Jun 29, 2020
5e56a80
executor: add failed point mock
Rustin170506 Jun 29, 2020
b156053
executor: remove failed point mock
Rustin170506 Jun 29, 2020
5869c8f
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 29, 2020
606e3cf
executor: fix fmt
Rustin170506 Jun 29, 2020
d12e6c3
executor: fix fmt
Rustin170506 Jun 29, 2020
7f07d69
executor: add failed point mock
Rustin170506 Jun 29, 2020
11cd87e
executor: fix fmt
Rustin170506 Jun 29, 2020
8cf78e1
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 29, 2020
77cb504
executor: using fixtures
Rustin170506 Jun 29, 2020
b5094f3
executor: using fixtures
Rustin170506 Jun 29, 2020
dd5b381
Merge branch 'master' into rustin-patch-auto
AilinKid Jun 30, 2020
5c2a831
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 30, 2020
c8aee99
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 30, 2020
b3cd4e6
Merge branch 'master' into rustin-patch-auto
AilinKid Jun 30, 2020
a1d89c8
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 30, 2020
b1d0dc8
Merge branch 'master' into rustin-patch-auto
Rustin170506 Jun 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ddl/reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ 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,
func (dc *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl table.PhysicalTable,
handleCols []*model.ColumnInfo, limit uint64) (distsql.SelectResult, error) {
sctx := newContext(d.store)
sctx := newContext(dc.store)
dagPB, err := buildDescTableScanDAG(sctx, tbl, handleCols, limit)
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -337,7 +337,7 @@ func (d *ddlCtx) buildDescTableScan(ctx context.Context, startTS uint64, tbl tab
}

// GetTableMaxHandle gets the max handle of a PhysicalTable.
func (d *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (maxHandle kv.Handle, emptyTable bool, err error) {
func (dc *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (maxHandle kv.Handle, emptyTable bool, err error) {
var handleCols []*model.ColumnInfo
tblInfo := tbl.Meta()
switch {
Expand All @@ -360,7 +360,7 @@ func (d *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (max

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, handleCols, 1)
result, err := dc.buildDescTableScan(ctx, startTS, tbl, handleCols, 1)
if err != nil {
return nil, false, errors.Trace(err)
}
Expand All @@ -376,7 +376,7 @@ func (d *ddlCtx) GetTableMaxHandle(startTS uint64, tbl table.PhysicalTable) (max
// empty table
return nil, true, nil
}
sessCtx := newContext(d.store)
sessCtx := newContext(dc.store)
row := chk.GetRow(0)
if tblInfo.IsCommonHandle {
maxHandle, err = buildHandleFromChunkRow(sessCtx.GetSessionVars().StmtCtx, row, handleCols)
Expand Down
16 changes: 8 additions & 8 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,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)

Expand All @@ -848,8 +856,6 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) {
return allHds
}

testutil.ConfigTestUtils.SetupAutoRandomTestConfig()
defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig()
tk.MustExec("set @@allow_auto_random_explicit_insert = true")

tk.MustExec("create table t (a bigint primary key auto_random(15), b int)")
Expand Down Expand Up @@ -971,9 +977,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"))
Expand Down Expand Up @@ -1042,9 +1045,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"))
Expand Down
23 changes: 16 additions & 7 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -903,11 +903,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)
Expand All @@ -918,8 +920,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 {
Expand Down Expand Up @@ -1013,10 +1023,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() {
Expand Down
38 changes: 35 additions & 3 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -707,9 +708,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("|",
Expand Down Expand Up @@ -808,6 +806,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep it stable, it is better to initialize the test (like other tests in testAutoRandomSuite):

testutil.ConfigTestUtils.SetupAutoRandomTestConfig()
defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig()

(If you would like to help improve, these patterns can be put into SetupTest() and TeardownTest().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Maybe I can send another PR to improve it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better in the same PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored.

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)

Expand Down