Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7df7198
fix test
zhaoxugang Jun 19, 2021
1dc4efb
Merge remote-tracking branch 'origin/master'
zhaoxugang Jun 21, 2021
248c61e
Merge branch 'master-upstream' into for_issue_25890
zhaoxugang Jul 10, 2021
0d3ff59
support create temporary table like tbl_xx
zhaoxugang Jul 11, 2021
5da8cd9
fix
zhaoxugang Jul 11, 2021
098dc55
fix
zhaoxugang Jul 11, 2021
363f5a6
Merge branch 'master-upstream'
zhaoxugang Jul 24, 2021
f40ef3a
Merge branch 'master-upstream'
zhaoxugang Aug 1, 2021
9a296b0
Merge remote-tracking branch 'origin/for_issue_25890' into for_issue_…
zhaoxugang Aug 1, 2021
bd4650c
fix test
zhaoxugang Aug 2, 2021
8144f54
Merge branch 'master-upstream'
zhaoxugang Aug 2, 2021
cdf5782
Merge branch 'master' into for_issue_25890
zhaoxugang Aug 2, 2021
6d17e77
fix test
zhaoxugang Aug 2, 2021
16627ef
fix
zhaoxugang Aug 8, 2021
e04af9d
fix
zhaoxugang Aug 8, 2021
89df2e7
fix
zhaoxugang Aug 8, 2021
b06b2f4
fix bug
zhaoxugang Aug 8, 2021
6dba306
fix
zhaoxugang Aug 8, 2021
7ac0b5b
Merge branch 'master' into for_issue_25890
zhaoxugang Aug 8, 2021
b33137f
Merge branch 'master-upstream'
zhaoxugang Aug 8, 2021
f557348
Merge branch 'master' into for_issue_25890
zhaoxugang Aug 8, 2021
0eb429e
fix
zhaoxugang Aug 8, 2021
52cfcdc
Merge remote-tracking branch 'origin/for_issue_25890' into for_issue_…
zhaoxugang Aug 8, 2021
0e6cf66
fix
zhaoxugang Aug 8, 2021
2f878de
fix
zhaoxugang Aug 8, 2021
7826b89
fix
zhaoxugang Aug 8, 2021
eb21a4d
fix test
zhaoxugang Aug 8, 2021
eeca5c5
fix test
zhaoxugang Aug 8, 2021
733f780
fix test
zhaoxugang Aug 8, 2021
f3aae12
Merge branch 'master' into for_issue_25890
zhaoxugang Aug 8, 2021
d4cbb92
Merge branch 'master' into for_issue_25890
ti-chi-bot Aug 12, 2021
3d7f981
Merge branch 'master' into for_issue_25890
ti-chi-bot Aug 12, 2021
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
32 changes: 28 additions & 4 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1686,12 +1686,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.
// 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
Expand All @@ -1708,6 +1708,30 @@ 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) {
Comment thread
zhaoxugang marked this conversation as resolved.
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
Expand Down Expand Up @@ -5765,7 +5789,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)
}
Expand Down
53 changes: 53 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,59 @@ 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"))
tk.MustExec("create temporary table if not exists tb12 like tb11;")
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")
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")
Comment thread
lcwangchao marked this conversation as resolved.

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.
Expand Down
2 changes: 1 addition & 1 deletion executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,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
}
Expand Down