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

ddl: fix CREATE GLOBAL TEMPORARY TABLE ... LIKE creates a normal table #25644

Merged
merged 10 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
42 changes: 27 additions & 15 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1731,22 +1731,11 @@ func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh
if err != nil {
return nil, errors.Trace(err)
}
switch s.TemporaryKeyword {
case ast.TemporaryGlobal:
tbInfo.TempTableType = model.TempTableGlobal
if !ctx.GetSessionVars().EnableGlobalTemporaryTable {
return nil, errors.New("global temporary table is experimental and it is switched off by tidb_enable_global_temporary_table")
}
// "create global temporary table ... on commit preserve rows"
if !s.OnCommitDelete {
return nil, errors.Trace(errUnsupportedOnCommitPreserve)
}
case ast.TemporaryLocal:
// TODO: set "tbInfo.TempTableType = model.TempTableLocal" after local temporary table is supported.
tbInfo.TempTableType = model.TempTableNone
if err = setTemporaryType(ctx, tbInfo, s); err != nil {
return nil, errors.Trace(err)
}
if s.TemporaryKeyword == ast.TemporaryLocal {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand. Why leave this judgement out of setTemporaryType?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

put it into the method would AppendWarning twice

Copy link
Contributor

Choose a reason for hiding this comment

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

CreateTable calls buildTableInfoWithLike and buildTableInfoWithStmt. So there are 2 ways:

  1. Call setTemporaryType only in CreateTable, which is impossible. Because setTemporaryType must be called in the middle of buildTableInfoWithStmt, unless you remove the dependence of tbInfo.TempTableType in buildTableInfoWithStmt.
  2. Call setTemporaryType both in buildTableInfoWithLike and buildTableInfoWithStmt.

ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("local TEMPORARY TABLE is not supported yet, TEMPORARY will be parsed but ignored"))
case ast.TemporaryNone:
tbInfo.TempTableType = model.TempTableNone
}

if err = setTableAutoRandomBits(ctx, tbInfo, colDefs); err != nil {
Expand Down Expand Up @@ -1815,6 +1804,9 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e
if err != nil {
return errors.Trace(err)
}
if err = setTemporaryType(ctx, tbInfo, s); err != nil {
return errors.Trace(err)
}

if err = checkTableInfoValidWithStmt(ctx, tbInfo, s); err != nil {
return err
Expand All @@ -1828,6 +1820,26 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e
return d.CreateTableWithInfo(ctx, schema.Name, tbInfo, onExist, false /*tryRetainID*/)
}

func setTemporaryType(ctx sessionctx.Context, tbInfo *model.TableInfo, s *ast.CreateTableStmt) error {
switch s.TemporaryKeyword {
case ast.TemporaryGlobal:
tbInfo.TempTableType = model.TempTableGlobal
if !ctx.GetSessionVars().EnableGlobalTemporaryTable {
return errors.New("global temporary table is experimental and it is switched off by tidb_enable_global_temporary_table")
}
// "create global temporary table ... on commit preserve rows"
if !s.OnCommitDelete {
return errors.Trace(errUnsupportedOnCommitPreserve)
}
case ast.TemporaryLocal:
// TODO: set "tbInfo.TempTableType = model.TempTableLocal" after local temporary table is supported.
tbInfo.TempTableType = model.TempTableNone
default:
tbInfo.TempTableType = model.TempTableNone
}
return nil
}

func (d *ddl) CreateTableWithInfo(
ctx sessionctx.Context,
dbName model.CIStr,
Expand Down
17 changes: 17 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) {
c.Assert(err, IsNil)
tableInfo := table.Meta()
c.Assert(len(tableInfo.ForeignKeys), Equals, 0)

// Issue 25613.
tk.MustExec("drop table if exists tb2, tb3")
tk.MustExec("create table tb2(id int);")
tk.MustExec("create global temporary table tb3 like tb2 on commit delete rows;")
Howie59 marked this conversation as resolved.
Show resolved Hide resolved
defer tk.MustExec("drop table if exists tb2, tb3")
tk.MustQuery("show create table tb3;").Check(testkit.Rows("tb3 CREATE GLOBAL TEMPORARY TABLE `tb3` (\n" +
" `id` int(11) DEFAULT NULL\n" +
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"))

tk.MustExec("drop table if exists tb4, tb5")
tk.MustExec("create table tb4(id int);")
tk.MustExec("create table tb5 like tb4")
defer tk.MustExec("drop table if exists tb4, tb5")
tk.MustQuery("show create table tb5;").Check(testkit.Rows("tb5 CREATE TABLE `tb5` (\n" +
" `id` int(11) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

// TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started.
Expand Down