From 7df7198a34ab0fdb95b418ba5185f1312b3a0dbc Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 20 Jun 2021 00:23:31 +0800 Subject: [PATCH 01/18] fix test --- expression/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 41e04af3b0c9e..65d309281453e 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -3861,7 +3861,7 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) { result = tk.MustQuery("select coalesce(NULL, a), coalesce(NULL, b, a), coalesce(c, NULL, a, b), coalesce(d, NULL), coalesce(d, c), coalesce(NULL, NULL, e, 1), coalesce(f), coalesce(1, a, b, c, d, e, f) from t2") // coalesce(col_bit) is not same with MySQL, because it's a bug of MySQL(https://bugs.mysql.com/bug.php?id=103289&thanks=4) - result.Check(testkit.Rows(fmt.Sprintf("1 1.1 2017-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef \x15 1", time.Now().In(tk.Se.GetSessionVars().Location()).Format("2006-01-02")))) + result.Check(testkit.Rows(fmt.Sprintf("1 1.1 2017-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef \x00\x15 1", time.Now().In(tk.Se.GetSessionVars().Location()).Format("2006-01-02")))) // nullif result = tk.MustQuery(`SELECT NULLIF(NULL, 1), NULLIF(1, NULL), NULLIF(1, 1), NULLIF(NULL, NULL);`) From 0d3ff59cbc1c13e5ace4eb0e41ecee548912a600 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 11 Jul 2021 23:53:47 +0800 Subject: [PATCH 02/18] support create temporary table like tbl_xx --- ddl/ddl_api.go | 29 ++++++++++++++++++++++++++--- executor/ddl.go | 17 ++++++++++++++--- infoschema/infoschema.go | 8 -------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index af6cc2fd13ae7..9058f71ee653a 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1682,12 +1682,12 @@ func buildTableInfoWithLike(ctx sessionctx.Context, ident ast.Ident, referTblInf // BuildTableInfoFromAST builds model.TableInfo from a SQL statement. // Note: TableID and PartitionID are left as uninitialized value. func BuildTableInfoFromAST(s *ast.CreateTableStmt) (*model.TableInfo, error) { - return BuildTableInfoWithCheck(mock.NewContext(), s, mysql.DefaultCharset, "") + return buildTableInfoWithCheck(mock.NewContext(), s, mysql.DefaultCharset, "") } // BuildTableInfoWithCheck builds model.TableInfo from a SQL statement. // Note: TableID and PartitionIDs are left as uninitialized value. -func BuildTableInfoWithCheck(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { +func buildTableInfoWithCheck(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { tbInfo, err := buildTableInfoWithStmt(ctx, s, dbCharset, dbCollate) if err != nil { return nil, err @@ -1704,6 +1704,29 @@ func BuildTableInfoWithCheck(ctx sessionctx.Context, s *ast.CreateTableStmt, dbC return tbInfo, nil } +func BuildSessionTemporaryTableInfo(ctx sessionctx.Context, is infoschema.InfoSchema, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { + ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name} + //build tableInfo + var tbInfo *model.TableInfo + var referTbl table.Table + var err error + if s.ReferTable != nil { + referIdent := ast.Ident{Schema: s.ReferTable.Schema, Name: s.ReferTable.Name} + _, ok := is.SchemaByName(referIdent.Schema) + if !ok { + return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(referIdent.Schema, referIdent.Name) + } + referTbl, err = is.TableByName(referIdent.Schema, referIdent.Name) + if err != nil { + return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(referIdent.Schema, referIdent.Name) + } + tbInfo, err = buildTableInfoWithLike(ctx, ident, referTbl.Meta(), s) + } else { + tbInfo, err = buildTableInfoWithCheck(ctx, s, dbCharset, dbCollate) + } + return tbInfo, err +} + // buildTableInfoWithStmt builds model.TableInfo from a SQL statement without validity check func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { colDefs := s.Cols @@ -5723,7 +5746,7 @@ func (d *ddl) RepairTable(ctx sessionctx.Context, table *ast.TableName, createSt } // It is necessary to specify the table.ID and partition.ID manually. - newTableInfo, err := BuildTableInfoWithCheck(ctx, createStmt, oldTableInfo.Charset, oldTableInfo.Collate) + newTableInfo, err := buildTableInfoWithCheck(ctx, createStmt, oldTableInfo.Charset, oldTableInfo.Collate) if err != nil { return errors.Trace(err) } diff --git a/executor/ddl.go b/executor/ddl.go index 1398cead232a9..a2867761f36b6 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -16,6 +16,7 @@ package executor import ( "context" "fmt" + "github.com/pingcap/tidb/table" "strings" "github.com/pingcap/errors" @@ -220,7 +221,7 @@ func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { if !ok { return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(s.Table.Schema.O) } - tbInfo, err := ddl.BuildTableInfoWithCheck(e.ctx, s, dbInfo.Charset, dbInfo.Collate) + tbInfo, err := ddl.BuildSessionTemporaryTableInfo(e.ctx, is, s, dbInfo.Charset, dbInfo.Collate) if err != nil { return err } @@ -245,7 +246,11 @@ func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { // AutoID is allocated in mocked.. alloc := autoid.NewAllocatorFromTempTblInfo(tbInfo) - tbl, err := tables.TableFromMeta([]autoid.Allocator{alloc}, tbInfo) + allocs := make([]autoid.Allocator, 0, 1) + if alloc != nil { + allocs = append(allocs, alloc) + } + tbl, err := tables.TableFromMeta(allocs, tbInfo) if err != nil { return err } @@ -256,7 +261,13 @@ func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() } localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) - err = localTempTables.AddTable(dbInfo, tbl) + var referTbl table.Table + referTbl, err = is.TableByName(s.Table.Schema, s.Table.Name) + if referTbl != nil { + err = infoschema.ErrTableExists.GenWithStackByArgs(referTbl.Meta().Name) + } else { + err = localTempTables.AddTable(dbInfo, tbl) + } if err != nil && s.IfNotExists && infoschema.ErrTableExists.Equal(err) { e.ctx.GetSessionVars().StmtCtx.AppendNote(err) diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index d3db70eaeb00b..a24a67bfafec6 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -449,14 +449,6 @@ func (is *LocalTemporaryTables) AddTable(schema *model.DBInfo, tbl table.Table) schemaTables := is.ensureSchema(schema.Name) tblMeta := tbl.Meta() - if _, ok := schemaTables.tables[tblMeta.Name.L]; ok { - return ErrTableExists.GenWithStackByArgs(tblMeta.Name) - } - - if _, ok := is.idx2table[tblMeta.ID]; ok { - return ErrTableExists.GenWithStackByArgs(tblMeta.Name) - } - schemaTables.tables[tblMeta.Name.L] = tbl is.idx2table[tblMeta.ID] = tbl From 5da8cd9d78ae9502774e0e61e3561f43371e5008 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Mon, 12 Jul 2021 00:40:10 +0800 Subject: [PATCH 03/18] fix --- executor/ddl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/ddl.go b/executor/ddl.go index a2867761f36b6..db86bb729eaee 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -16,7 +16,6 @@ package executor import ( "context" "fmt" - "github.com/pingcap/tidb/table" "strings" "github.com/pingcap/errors" @@ -33,6 +32,7 @@ import ( "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/util/admin" "github.com/pingcap/tidb/util/chunk" From 098dc55c14b5fab223e5dfc81493ce5691c6fb6e Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Mon, 12 Jul 2021 00:50:42 +0800 Subject: [PATCH 04/18] fix --- ddl/ddl_api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 9058f71ee653a..db95560711a5f 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1704,6 +1704,7 @@ func buildTableInfoWithCheck(ctx sessionctx.Context, s *ast.CreateTableStmt, dbC return tbInfo, nil } +// BuildSessionTemporaryTableInfo builds model.TableInfo from a SQL statement. func BuildSessionTemporaryTableInfo(ctx sessionctx.Context, is infoschema.InfoSchema, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name} //build tableInfo From bd4650ca5bdc6692b287eb749d9fa5e6827feeb2 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Mon, 2 Aug 2021 23:37:35 +0800 Subject: [PATCH 05/18] fix test --- ddl/serial_test.go | 22 ++++++++++++++++++++++ executor/ddl.go | 9 +-------- infoschema/infoschema.go | 8 ++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 8c6e094e046a7..f336387430a3f 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -646,6 +646,28 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { _, err = tk.Exec("create global temporary table tb8 like tb7 on commit delete rows;") c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) defer tk.MustExec("drop table if exists tb7, tb8") + + tk.MustExec("set tidb_enable_noop_functions=true") + // Test from->normal, to->local temporary + tk.MustExec("drop table if exists tb11, tb12") + tk.MustExec("create table tb11 (i int primary key, j int)") + tk.MustExec("create temporary table tb12 like tb11") + tk.MustQuery("show create table tb12;").Check(testkit.Rows("tb12 CREATE TEMPORARY TABLE `tb12` (\n" + + "`i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + defer tk.MustExec("drop table if exists tb11, tb12") + // Test from->local temporary, to->local temporary + tk.MustExec("drop table if exists tb13, tb14") + tk.MustExec("create temporary table tb13 (i int primary key, j int)") + _, err = tk.Exec("create temporary table tb14 like tb13;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) + defer tk.MustExec("drop table if exists tb13, tb14") + // Test from->local temporary, to->normal + tk.MustExec("drop table if exists tb15, tb16") + tk.MustExec("create temporary table tb15 (i int primary key, j int)") + _, err = tk.Exec("create table tb16 like tb15;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) + defer tk.MustExec("drop table if exists tb15, tb16") } // TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started. diff --git a/executor/ddl.go b/executor/ddl.go index b4f932ab7bcbf..262df68619ae1 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -33,7 +33,6 @@ import ( "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/sessionctx/variable" - "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/util/admin" @@ -336,13 +335,7 @@ func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() } - var referTbl table.Table - referTbl, err = is.TableByName(s.Table.Schema, s.Table.Name) - if referTbl != nil { - err = infoschema.ErrTableExists.GenWithStackByArgs(referTbl.Meta().Name) - } else { - err = localTempTables.AddTable(dbInfo, tbl) - } + err = localTempTables.AddTable(dbInfo, tbl) if err != nil && s.IfNotExists && infoschema.ErrTableExists.Equal(err) { e.ctx.GetSessionVars().StmtCtx.AppendNote(err) diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index a24a67bfafec6..d3db70eaeb00b 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -449,6 +449,14 @@ func (is *LocalTemporaryTables) AddTable(schema *model.DBInfo, tbl table.Table) schemaTables := is.ensureSchema(schema.Name) tblMeta := tbl.Meta() + if _, ok := schemaTables.tables[tblMeta.Name.L]; ok { + return ErrTableExists.GenWithStackByArgs(tblMeta.Name) + } + + if _, ok := is.idx2table[tblMeta.ID]; ok { + return ErrTableExists.GenWithStackByArgs(tblMeta.Name) + } + schemaTables.tables[tblMeta.Name.L] = tbl is.idx2table[tblMeta.ID] = tbl From 6d17e77e9ed2d9164018ea3643a84ae4fa1e2279 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Tue, 3 Aug 2021 00:39:18 +0800 Subject: [PATCH 06/18] fix test --- ddl/serial_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f336387430a3f..45419e72c0cc6 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -653,7 +653,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { tk.MustExec("create table tb11 (i int primary key, j int)") tk.MustExec("create temporary table tb12 like tb11") tk.MustQuery("show create table tb12;").Check(testkit.Rows("tb12 CREATE TEMPORARY TABLE `tb12` (\n" + - "`i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + + " `i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) defer tk.MustExec("drop table if exists tb11, tb12") // Test from->local temporary, to->local temporary From 16627ef845322603a1626e247904276842aa99e0 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 17:30:42 +0800 Subject: [PATCH 07/18] fix --- ddl/ddl_api.go | 92 ++++++++++++++++++++++++++++++---------------- ddl/serial_test.go | 32 ++++++++++++++++ executor/ddl.go | 4 -- 3 files changed, 92 insertions(+), 36 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 181d6648b235e..bc226e01949e9 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1927,42 +1927,70 @@ func (d *ddl) CreateTableWithInfo( actionType = model.ActionCreateTable } - job := &model.Job{ - SchemaID: schema.ID, - TableID: tbInfo.ID, - SchemaName: schema.Name.L, - Type: actionType, - BinlogInfo: &model.HistoryInfo{}, - Args: args, - } - - err = d.doDDLJob(ctx, job) - if err != nil { - // table exists, but if_not_exists flags is true, so we ignore this error. - if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { - ctx.GetSessionVars().StmtCtx.AppendNote(err) - err = nil - } - } else if actionType == model.ActionCreateTable { - d.preSplitAndScatter(ctx, tbInfo, tbInfo.GetPartitionInfo()) - if tbInfo.AutoIncID > 1 { - // Default tableAutoIncID base is 0. - // If the first ID is expected to greater than 1, we need to do rebase. - newEnd := tbInfo.AutoIncID - 1 - if err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.RowIDAllocType); err != nil { - return errors.Trace(err) + if tbInfo.TempTableType == model.TempTableLocal { + if tbInfo.ID != 0 { + tbInfo.State = model.StatePublic + } + // Store this temporary table to the session. + sessVars := ctx.GetSessionVars() + if sessVars.LocalTemporaryTables == nil { + sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() + } + // AutoID is allocated in mocked.. + alloc := autoid.NewAllocatorFromTempTblInfo(tbInfo) + allocs := make([]autoid.Allocator, 0, 1) + if alloc != nil { + allocs = append(allocs, alloc) + } + tbl, _ := tables.TableFromMeta(allocs, tbInfo) + localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) + err = localTempTables.AddTable(schema, tbl) + if err != nil { + // table exists, but if_not_exists flags is true, so we ignore this error. + if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { + ctx.GetSessionVars().StmtCtx.AppendNote(err) + err = nil } } - if tbInfo.AutoRandID > 1 { - // Default tableAutoRandID base is 0. - // If the first ID is expected to greater than 1, we need to do rebase. - newEnd := tbInfo.AutoRandID - 1 - err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.AutoRandomType) + return errors.Trace(err) + } else { + job := &model.Job{ + SchemaID: schema.ID, + TableID: tbInfo.ID, + SchemaName: schema.Name.L, + Type: actionType, + BinlogInfo: &model.HistoryInfo{}, + Args: args, + } + err = d.doDDLJob(ctx, job) + + if err != nil { + // table exists, but if_not_exists flags is true, so we ignore this error. + if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { + ctx.GetSessionVars().StmtCtx.AppendNote(err) + err = nil + } + } else if actionType == model.ActionCreateTable { + d.preSplitAndScatter(ctx, tbInfo, tbInfo.GetPartitionInfo()) + if tbInfo.AutoIncID > 1 { + // Default tableAutoIncID base is 0. + // If the first ID is expected to greater than 1, we need to do rebase. + newEnd := tbInfo.AutoIncID - 1 + if err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.RowIDAllocType); err != nil { + return errors.Trace(err) + } + } + if tbInfo.AutoRandID > 1 { + // Default tableAutoRandID base is 0. + // If the first ID is expected to greater than 1, we need to do rebase. + newEnd := tbInfo.AutoRandID - 1 + err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.AutoRandomType) + } } - } - err = d.callHookOnChanged(err) - return errors.Trace(err) + err = d.callHookOnChanged(err) + return errors.Trace(err) + } } // preSplitAndScatter performs pre-split and scatter of the table's regions. diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 45419e72c0cc6..48b87ae4030ad 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -40,6 +40,7 @@ import ( "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" + "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/tablecodec" @@ -655,6 +656,9 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { tk.MustQuery("show create table tb12;").Check(testkit.Rows("tb12 CREATE TEMPORARY TABLE `tb12` (\n" + " `i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + tk.MustQuery("create temporary table if not exists tb12 like tb11;") + c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings(), Equals, + stmtctx.SQLWarn{Level: stmtctx.WarnLevelWarning, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}) defer tk.MustExec("drop table if exists tb11, tb12") // Test from->local temporary, to->local temporary tk.MustExec("drop table if exists tb13, tb14") @@ -668,6 +672,34 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { _, err = tk.Exec("create table tb16 like tb15;") c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) defer tk.MustExec("drop table if exists tb15, tb16") + + tk.MustExec("drop table if exists table_pre_split, tmp_pre_split") + tk.MustExec("create table table_pre_split(id int) shard_row_id_bits=2 pre_split_regions=2;") + _, err = tk.Exec("create temporary table tmp_pre_split like table_pre_split") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("pre split regions").Error()) + defer tk.MustExec("drop table if exists table_pre_split, tmp_pre_split") + + tk.MustExec("drop table if exists table_shard_row_id, tmp_shard_row_id") + tk.MustExec("create table table_shard_row_id(id int) shard_row_id_bits=2;") + _, err = tk.Exec("create temporary table tmp_shard_row_id like table_shard_row_id") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) + defer tk.MustExec("drop table if exists table_shard_row_id, tmp_shard_row_id") + + tk.MustExec("drop table if exists partition_table, tmp_partition_table") + tk.MustExec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") + tk.MustGetErrCode("create temporary table tmp_partition_table like partition_table", errno.ErrPartitionNoTemporary) + defer tk.MustExec("drop table if exists partition_table, tmp_partition_table") + + tk.MustExec("drop table if exists foreign_key_table1, foreign_key_table2, foreign_key_tmp;") + tk.MustExec("create table foreign_key_table1 (a int, b int);") + tk.MustExec("create table foreign_key_table2 (c int,d int,foreign key (d) references foreign_key_table1 (b));") + tk.MustExec("create temporary table foreign_key_tmp like foreign_key_table2") + is = tk.Se.(sessionctx.Context).GetInfoSchema().(infoschema.InfoSchema) + table, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("foreign_key_tmp")) + c.Assert(err, IsNil) + tableInfo = table.Meta() + c.Assert(len(tableInfo.ForeignKeys), Equals, 0) + defer tk.MustExec("drop table if exists foreign_key_table1, foreign_key_table2, foreign_key_tmp;") } // TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started. diff --git a/executor/ddl.go b/executor/ddl.go index 262df68619ae1..47c527aa2f591 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -115,10 +115,6 @@ func (e *DDLExec) Next(ctx context.Context, req *chunk.Chunk) (err error) { // Following cases are exceptions var localTempTablesToDrop []*model.TableInfo switch s := e.stmt.(type) { - case *ast.CreateTableStmt: - if s.TemporaryKeyword == ast.TemporaryLocal { - return e.createSessionTemporaryTable(s) - } case *ast.DropTableStmt: if s.IsView { break From e04af9d7b12b4c414003d7f5559f6273020c2ad5 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 17:36:52 +0800 Subject: [PATCH 08/18] fix --- ddl/ddl_api.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index bc226e01949e9..1d8ec869cd927 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1942,7 +1942,10 @@ func (d *ddl) CreateTableWithInfo( if alloc != nil { allocs = append(allocs, alloc) } - tbl, _ := tables.TableFromMeta(allocs, tbInfo) + tbl, err := tables.TableFromMeta(allocs, tbInfo) + if err != nil { + return errors.Trace(err) + } localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) err = localTempTables.AddTable(schema, tbl) if err != nil { From 89df2e7df9759f18c63ca67b12aac910822dd207 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 17:50:56 +0800 Subject: [PATCH 09/18] fix --- ddl/ddl_api.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 1d8ec869cd927..f83cfa266f0b3 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1942,7 +1942,8 @@ func (d *ddl) CreateTableWithInfo( if alloc != nil { allocs = append(allocs, alloc) } - tbl, err := tables.TableFromMeta(allocs, tbInfo) + var tbl table.Table + tbl, err = tables.TableFromMeta(allocs, tbInfo) if err != nil { return errors.Trace(err) } @@ -1955,7 +1956,6 @@ func (d *ddl) CreateTableWithInfo( err = nil } } - return errors.Trace(err) } else { job := &model.Job{ SchemaID: schema.ID, @@ -1992,8 +1992,8 @@ func (d *ddl) CreateTableWithInfo( } err = d.callHookOnChanged(err) - return errors.Trace(err) } + return errors.Trace(err) } // preSplitAndScatter performs pre-split and scatter of the table's regions. From b06b2f44bb54bf25f3f053bb0ba74273282025f4 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 18:01:36 +0800 Subject: [PATCH 10/18] fix bug --- ddl/ddl_api.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index f83cfa266f0b3..b91b94778b52d 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -20,6 +20,7 @@ package ddl import ( "context" "fmt" + "github.com/tikv/client-go/v2/tikv" "math" "strconv" "strings" @@ -1947,6 +1948,16 @@ func (d *ddl) CreateTableWithInfo( if err != nil { return errors.Trace(err) } + // Init MemBuffer in session + if sessVars.TemporaryTableData == nil { + // Create this txn just for getting a MemBuffer. It's a little tricky + bufferTxn, err := ctx.GetStore().BeginWithOption(tikv.DefaultStartTSOption().SetStartTS(0)) + if err != nil { + return err + } + + sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() + } localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) err = localTempTables.AddTable(schema, tbl) if err != nil { From 6dba306dff508537e30e57d933cddada40e53a4b Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 18:10:08 +0800 Subject: [PATCH 11/18] fix --- executor/ddl.go | 71 ------------------------------------------------- 1 file changed, 71 deletions(-) diff --git a/executor/ddl.go b/executor/ddl.go index 47c527aa2f591..2b4760a3427fc 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -30,17 +30,14 @@ import ( "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" - "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/sessionctx/variable" - "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/util/admin" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/gcutil" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/sqlexec" - "github.com/tikv/client-go/v2/tikv" "go.uber.org/zap" ) @@ -273,74 +270,6 @@ func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error { return err } -func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { - is := e.ctx.GetInfoSchema().(infoschema.InfoSchema) - dbInfo, ok := is.SchemaByName(s.Table.Schema) - if !ok { - return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(s.Table.Schema.O) - } - tbInfo, err := ddl.BuildSessionTemporaryTableInfo(e.ctx, is, s, dbInfo.Charset, dbInfo.Collate) - if err != nil { - return err - } - - dom := domain.GetDomain(e.ctx) - // Local temporary table uses a real table ID. - // We could mock a table ID, but the mocked ID might be identical to an existing - // real table, and then we'll get into trouble. - err = kv.RunInNewTxn(context.Background(), dom.Store(), true, func(ctx context.Context, txn kv.Transaction) error { - m := meta.NewMeta(txn) - tblID, err := m.GenGlobalID() - if err != nil { - return errors.Trace(err) - } - tbInfo.ID = tblID - tbInfo.State = model.StatePublic - return nil - }) - if err != nil { - return err - } - - // AutoID is allocated in mocked.. - alloc := autoid.NewAllocatorFromTempTblInfo(tbInfo) - allocs := make([]autoid.Allocator, 0, 1) - if alloc != nil { - allocs = append(allocs, alloc) - } - tbl, err := tables.TableFromMeta(allocs, tbInfo) - if err != nil { - return err - } - - // Store this temporary table to the session. - sessVars := e.ctx.GetSessionVars() - if sessVars.LocalTemporaryTables == nil { - sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() - } - localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) - - // Init MemBuffer in session - if sessVars.TemporaryTableData == nil { - // Create this txn just for getting a MemBuffer. It's a little tricky - bufferTxn, err := e.ctx.GetStore().BeginWithOption(tikv.DefaultStartTSOption().SetStartTS(0)) - if err != nil { - return err - } - - sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() - } - - err = localTempTables.AddTable(dbInfo, tbl) - - if err != nil && s.IfNotExists && infoschema.ErrTableExists.Equal(err) { - e.ctx.GetSessionVars().StmtCtx.AppendNote(err) - return nil - } - - return err -} - func (e *DDLExec) executeCreateView(s *ast.CreateViewStmt) error { ret := &core.PreprocessorReturn{} err := core.Preprocess(e.ctx, s.Select, core.WithPreprocessorReturn(ret)) From 0eb429edb7a72f7e1a635c1752cb8b4844ab6124 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 18:41:36 +0800 Subject: [PATCH 12/18] fix --- ddl/ddl_api.go | 2 +- executor/ddl.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index fec97de08bbe0..63553e2b81bc3 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1959,7 +1959,7 @@ func (d *ddl) CreateTableWithInfo( sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() } localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) - err = localTempTables.AddTable(schema, tbl) + err = localTempTables.AddTable(dbName, tbl) if err != nil { // table exists, but if_not_exists flags is true, so we ignore this error. if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { diff --git a/executor/ddl.go b/executor/ddl.go index 7018f3b1a02ff..c60e2db7456c3 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -41,7 +41,6 @@ import ( "github.com/pingcap/tidb/util/gcutil" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/sqlexec" - "github.com/tikv/client-go/v2/tikv" "go.uber.org/zap" ) From 0e6cf66be7fa056cb7f5aabb28babeb46f35aa32 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 18:50:46 +0800 Subject: [PATCH 13/18] fix --- ddl/ddl_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 63553e2b81bc3..e7c5062659e9c 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -20,7 +20,6 @@ package ddl import ( "context" "fmt" - "github.com/tikv/client-go/v2/tikv" "math" "strconv" "strings" @@ -58,6 +57,7 @@ import ( "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/set" + "github.com/tikv/client-go/v2/tikv" "go.uber.org/zap" ) From 2f878de615331dc91b121f3241b3be267c46d416 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 22:57:21 +0800 Subject: [PATCH 14/18] fix --- ddl/ddl_api.go | 104 ++++++++++++++------------------------------- ddl/serial_test.go | 2 +- executor/ddl.go | 49 +++++++++++++++++++++ 3 files changed, 81 insertions(+), 74 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index e7c5062659e9c..a735720242c51 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -57,7 +57,6 @@ import ( "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/set" - "github.com/tikv/client-go/v2/tikv" "go.uber.org/zap" ) @@ -1690,7 +1689,7 @@ func BuildTableInfoFromAST(s *ast.CreateTableStmt) (*model.TableInfo, error) { return buildTableInfoWithCheck(mock.NewContext(), s, mysql.DefaultCharset, "") } -// BuildTableInfoWithCheck builds model.TableInfo from a SQL statement. +// buildTableInfoWithCheck builds model.TableInfo from a SQL statement. // Note: TableID and PartitionIDs are left as uninitialized value. func buildTableInfoWithCheck(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCharset, dbCollate string) (*model.TableInfo, error) { tbInfo, err := buildTableInfoWithStmt(ctx, s, dbCharset, dbCollate) @@ -1928,82 +1927,41 @@ func (d *ddl) CreateTableWithInfo( actionType = model.ActionCreateTable } - if tbInfo.TempTableType == model.TempTableLocal { - if tbInfo.ID != 0 { - tbInfo.State = model.StatePublic - } - // Store this temporary table to the session. - sessVars := ctx.GetSessionVars() - if sessVars.LocalTemporaryTables == nil { - sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() - } - // AutoID is allocated in mocked.. - alloc := autoid.NewAllocatorFromTempTblInfo(tbInfo) - allocs := make([]autoid.Allocator, 0, 1) - if alloc != nil { - allocs = append(allocs, alloc) - } - var tbl table.Table - tbl, err = tables.TableFromMeta(allocs, tbInfo) - if err != nil { - return errors.Trace(err) - } - // Init MemBuffer in session - if sessVars.TemporaryTableData == nil { - // Create this txn just for getting a MemBuffer. It's a little tricky - bufferTxn, err := ctx.GetStore().BeginWithOption(tikv.DefaultStartTSOption().SetStartTS(0)) - if err != nil { - return err - } + job := &model.Job{ + SchemaID: schema.ID, + TableID: tbInfo.ID, + SchemaName: schema.Name.L, + Type: actionType, + BinlogInfo: &model.HistoryInfo{}, + Args: args, + } - sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() - } - localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) - err = localTempTables.AddTable(dbName, tbl) - if err != nil { - // table exists, but if_not_exists flags is true, so we ignore this error. - if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { - ctx.GetSessionVars().StmtCtx.AppendNote(err) - err = nil + err = d.doDDLJob(ctx, job) + if err != nil { + // table exists, but if_not_exists flags is true, so we ignore this error. + if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { + ctx.GetSessionVars().StmtCtx.AppendNote(err) + err = nil + } + } else if actionType == model.ActionCreateTable { + d.preSplitAndScatter(ctx, tbInfo, tbInfo.GetPartitionInfo()) + if tbInfo.AutoIncID > 1 { + // Default tableAutoIncID base is 0. + // If the first ID is expected to greater than 1, we need to do rebase. + newEnd := tbInfo.AutoIncID - 1 + if err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.RowIDAllocType); err != nil { + return errors.Trace(err) } } - } else { - job := &model.Job{ - SchemaID: schema.ID, - TableID: tbInfo.ID, - SchemaName: schema.Name.L, - Type: actionType, - BinlogInfo: &model.HistoryInfo{}, - Args: args, + if tbInfo.AutoRandID > 1 { + // Default tableAutoRandID base is 0. + // If the first ID is expected to greater than 1, we need to do rebase. + newEnd := tbInfo.AutoRandID - 1 + err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.AutoRandomType) } - err = d.doDDLJob(ctx, job) - - if err != nil { - // table exists, but if_not_exists flags is true, so we ignore this error. - if onExist == OnExistIgnore && infoschema.ErrTableExists.Equal(err) { - ctx.GetSessionVars().StmtCtx.AppendNote(err) - err = nil - } - } else if actionType == model.ActionCreateTable { - d.preSplitAndScatter(ctx, tbInfo, tbInfo.GetPartitionInfo()) - if tbInfo.AutoIncID > 1 { - // Default tableAutoIncID base is 0. - // If the first ID is expected to greater than 1, we need to do rebase. - newEnd := tbInfo.AutoIncID - 1 - if err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.RowIDAllocType); err != nil { - return errors.Trace(err) - } - } - if tbInfo.AutoRandID > 1 { - // Default tableAutoRandID base is 0. - // If the first ID is expected to greater than 1, we need to do rebase. - newEnd := tbInfo.AutoRandID - 1 - err = d.handleAutoIncID(tbInfo, schema.ID, newEnd, autoid.AutoRandomType) - } - } - - err = d.callHookOnChanged(err) } + + err = d.callHookOnChanged(err) return errors.Trace(err) } diff --git a/ddl/serial_test.go b/ddl/serial_test.go index b72d1918d0c7e..f5fc70d83ab38 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -656,7 +656,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { tk.MustQuery("show create table tb12;").Check(testkit.Rows("tb12 CREATE TEMPORARY TABLE `tb12` (\n" + " `i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - tk.MustQuery("create temporary table if not exists tb12 like tb11;") + tk.MustExec("create temporary table if not exists tb12 like tb11;") c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings(), Equals, stmtctx.SQLWarn{Level: stmtctx.WarnLevelWarning, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}) defer tk.MustExec("drop table if exists tb11, tb12") diff --git a/executor/ddl.go b/executor/ddl.go index c60e2db7456c3..33ed85ec98ed4 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -17,6 +17,7 @@ import ( "bytes" "context" "fmt" + "github.com/tikv/client-go/v2/tikv" "strings" "github.com/pingcap/errors" @@ -136,6 +137,10 @@ func (e *DDLExec) Next(ctx context.Context, req *chunk.Chunk) (err error) { // Following cases are exceptions var localTempTablesToDrop []*ast.TableName switch s := e.stmt.(type) { + case *ast.CreateTableStmt: + if s.TemporaryKeyword == ast.TemporaryLocal { + return e.createSessionTemporaryTable(s) + } case *ast.DropTableStmt: if s.IsView { break @@ -328,6 +333,50 @@ func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error { return err } +func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { + is := e.ctx.GetInfoSchema().(infoschema.InfoSchema) + dbInfo, ok := is.SchemaByName(s.Table.Schema) + if !ok { + return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(s.Table.Schema.O) + } + tbInfo, err := ddl.BuildSessionTemporaryTableInfo(e.ctx, is, s, dbInfo.Charset, dbInfo.Collate) + if err != nil { + return err + } + + tbl, err := e.newTemporaryTableFromTableInfo(tbInfo) + if err != nil { + return err + } + + // Store this temporary table to the session. + sessVars := e.ctx.GetSessionVars() + if sessVars.LocalTemporaryTables == nil { + sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() + } + localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) + + // Init MemBuffer in session + if sessVars.TemporaryTableData == nil { + // Create this txn just for getting a MemBuffer. It's a little tricky + bufferTxn, err := e.ctx.GetStore().BeginWithOption(tikv.DefaultStartTSOption().SetStartTS(0)) + if err != nil { + return err + } + + sessVars.TemporaryTableData = bufferTxn.GetMemBuffer() + } + + err = localTempTables.AddTable(dbInfo.Name, tbl) + + if err != nil && s.IfNotExists && infoschema.ErrTableExists.Equal(err) { + e.ctx.GetSessionVars().StmtCtx.AppendNote(err) + return nil + } + + return err +} + func (e *DDLExec) newTemporaryTableFromTableInfo(tbInfo *model.TableInfo) (table.Table, error) { dom := domain.GetDomain(e.ctx) // Local temporary table uses a real table ID. From 7826b89d1c400697b35aa42b2aabb6674067846a Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 23:17:15 +0800 Subject: [PATCH 15/18] fix --- executor/ddl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/ddl.go b/executor/ddl.go index 33ed85ec98ed4..13d5f3c1b6900 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -17,7 +17,6 @@ import ( "bytes" "context" "fmt" - "github.com/tikv/client-go/v2/tikv" "strings" "github.com/pingcap/errors" @@ -42,6 +41,7 @@ import ( "github.com/pingcap/tidb/util/gcutil" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/sqlexec" + "github.com/tikv/client-go/v2/tikv" "go.uber.org/zap" ) From eb21a4d427db25029fd73b12f4d72a0eaf8eba30 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 23:35:29 +0800 Subject: [PATCH 16/18] fix test --- ddl/serial_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f5fc70d83ab38..0e0d790ecb83b 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -658,7 +658,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("create temporary table if not exists tb12 like tb11;") c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings(), Equals, - stmtctx.SQLWarn{Level: stmtctx.WarnLevelWarning, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}) + []stmtctx.SQLWarn{stmtctx.SQLWarn{Level: stmtctx.WarnLevelNote, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}}) defer tk.MustExec("drop table if exists tb11, tb12") // Test from->local temporary, to->local temporary tk.MustExec("drop table if exists tb13, tb14") From eeca5c55dbdbec8ccc949a2e26582aa627b913c9 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 23:41:08 +0800 Subject: [PATCH 17/18] fix test --- ddl/serial_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 0e0d790ecb83b..60b5b27221ccd 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -657,8 +657,8 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { " `i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("create temporary table if not exists tb12 like tb11;") - c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings(), Equals, - []stmtctx.SQLWarn{stmtctx.SQLWarn{Level: stmtctx.WarnLevelNote, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}}) + c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings()[0], Equals, + stmtctx.SQLWarn{Level: stmtctx.WarnLevelNote, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}) defer tk.MustExec("drop table if exists tb11, tb12") // Test from->local temporary, to->local temporary tk.MustExec("drop table if exists tb13, tb14") From 733f7804b183b3ecae831448111f3ba6697cee94 Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Sun, 8 Aug 2021 23:51:05 +0800 Subject: [PATCH 18/18] fix test --- ddl/serial_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 60b5b27221ccd..ff99d04b0afb9 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -40,7 +40,6 @@ import ( "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" - "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/tablecodec" @@ -657,8 +656,8 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) { " `i` int(11) NOT NULL,\n `j` int(11) DEFAULT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("create temporary table if not exists tb12 like tb11;") - c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings()[0], Equals, - stmtctx.SQLWarn{Level: stmtctx.WarnLevelNote, Err: infoschema.ErrTableExists.GenWithStackByArgs("tb12")}) + c.Assert(tk.Se.(sessionctx.Context).GetSessionVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, + infoschema.ErrTableExists.GenWithStackByArgs("tb12").Error()) defer tk.MustExec("drop table if exists tb11, tb12") // Test from->local temporary, to->local temporary tk.MustExec("drop table if exists tb13, tb14")