Skip to content

Commit

Permalink
ddl: support all engine for temporary tables (pingcap#28815)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Oct 18, 2021
1 parent ab0d231 commit 46b87ac
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
20 changes: 15 additions & 5 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2896,8 +2896,21 @@ func (s *testIntegrationSuite3) TestCreateTemporaryTable(c *C) {

// Not support yet.
tk.MustGetErrCode("create global temporary table t (id int) on commit preserve rows", errno.ErrUnsupportedDDLOperation)
// Engine type can only be 'memory' or empty for now.
tk.MustGetErrCode("create global temporary table t (id int) engine = 'innodb' on commit delete rows", errno.ErrUnsupportedDDLOperation)

// Engine type can be anyone, see https://github.com/pingcap/tidb/issues/28541.
tk.MustExec("drop table if exists tengine")
tk.MustExec("create global temporary table tengine (id int) engine = 'innodb' on commit delete rows")
tk.MustExec("drop table if exists tengine")
tk.MustExec("create global temporary table tengine (id int) engine = 'memory' on commit delete rows")
tk.MustExec("drop table if exists tengine")
tk.MustExec("create global temporary table tengine (id int) engine = 'myisam' on commit delete rows")
tk.MustExec("drop table if exists tengine")
tk.MustExec("create temporary table tengine (id int) engine = 'innodb'")
tk.MustExec("drop table if exists tengine")
tk.MustExec("create temporary table tengine (id int) engine = 'memory'")
tk.MustExec("drop table if exists tengine")
tk.MustExec("create temporary table tengine (id int) engine = 'myisam'")
tk.MustExec("drop table if exists tengine")

// Create local temporary table.
tk.MustExec("create database tmp_db")
Expand Down Expand Up @@ -2939,9 +2952,6 @@ func (s *testIntegrationSuite3) TestCreateTemporaryTable(c *C) {
c.Assert(infoschema.ErrTableExists.Equal(err), IsTrue)
tk.MustExec("create temporary table if not exists b_local_temp_table (id int)")

// Engine type can only be 'memory' or empty for now.
tk.MustGetErrCode("create temporary table te (id int) engine = 'innodb'", errno.ErrUnsupportedDDLOperation)

// Stale read see the local temporary table but can't read on it.
tk.MustExec("START TRANSACTION READ ONLY AS OF TIMESTAMP NOW(3)")
tk.MustGetErrMsg("select * from overlap", "can not stale read temporary table")
Expand Down
6 changes: 0 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2447,12 +2447,6 @@ func handleTableOptions(options []*ast.TableOption, tbInfo *model.TableInfo) err
tbInfo.PreSplitRegions = op.UintValue
case ast.TableOptionCharset, ast.TableOptionCollate:
// We don't handle charset and collate here since they're handled in `getCharsetAndCollateInTableOption`.
case ast.TableOptionEngine:
if tbInfo.TempTableType != model.TempTableNone {
if op.StrValue != "" && !strings.EqualFold(op.StrValue, "memory") {
return errors.Trace(errUnsupportedEngineTemporary)
}
}
case ast.TableOptionPlacementPolicy:
tbInfo.PlacementPolicyRef = &model.PolicyRefInfo{
Name: model.NewCIStr(op.StrValue),
Expand Down
1 change: 0 additions & 1 deletion ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ var (
ErrOptOnTemporaryTable = dbterror.ClassDDL.NewStd(mysql.ErrOptOnTemporaryTable)

errUnsupportedOnCommitPreserve = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("TiDB doesn't support ON COMMIT PRESERVE ROWS for now", nil))
errUnsupportedEngineTemporary = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("TiDB doesn't support this kind of engine for temporary table", nil))
errUnsupportedClusteredSecondaryKey = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("CLUSTERED/NONCLUSTERED keyword is only supported for primary key", nil))

// ErrUnsupportedLocalTempTableDDL returns when ddl operation unsupported for local temporary table
Expand Down
4 changes: 2 additions & 2 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) {
defer tk.MustExec("drop table if exists tb3, tb4")
tk.MustQuery("show create table tb4;").Check(testkit.Rows("tb4 CREATE GLOBAL TEMPORARY TABLE `tb4` (\n" +
" `id` int(11) DEFAULT NULL\n" +
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"))
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"))

// Test from->global temporary, to->normal.
tk.MustExec("drop table if exists tb5, tb6")
Expand All @@ -653,7 +653,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) {
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"))
") ENGINE=InnoDB 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("test.tb12").Error())
Expand Down
9 changes: 1 addition & 8 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,14 +1006,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T

buf.WriteString("\n")

switch tableInfo.TempTableType {
case model.TempTableNone:
buf.WriteString(") ENGINE=InnoDB")
default:
// For now the only supported engine for temporary table is memory.
buf.WriteString(") ENGINE=memory")
}

buf.WriteString(") ENGINE=InnoDB")
// We need to explicitly set the default charset and collation
// to make it work on MySQL server which has default collate utf8_general_ci.
if len(tblCollate) == 0 || tblCollate == "binary" {
Expand Down
12 changes: 6 additions & 6 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ func (s *testSuite5) TestShowCreateStmtIgnoreLocalTemporaryTables(c *C) {
""+
"v1 CREATE TEMPORARY TABLE `v1` (\n"+
" `a` int(11) DEFAULT NULL\n"+
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec("drop view v1")
err := tk.ExecToErr("show create view v1")
Expand Down Expand Up @@ -1436,13 +1436,13 @@ func (s *testSuite5) TestShowTemporaryTable(c *C) {
// For issue https://github.com/pingcap/tidb/issues/24752
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE GLOBAL TEMPORARY TABLE `t1` (\n" +
" `id` int(11) DEFAULT NULL\n" +
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"))
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"))
// No panic, fix issue https://github.com/pingcap/tidb/issues/24788
expect := "CREATE GLOBAL TEMPORARY TABLE `t3` (\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 ON COMMIT DELETE ROWS"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"
tk.MustQuery("show create table t3").Check(testkit.Rows("t3 " + expect))

// Verify that the `show create table` result can be used to build the table.
Expand All @@ -1462,15 +1462,15 @@ func (s *testSuite5) TestShowTemporaryTable(c *C) {
" `pad` varbinary(255) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" KEY `b` (`b`)\n" +
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"
tk.MustQuery("show create table t5").Check(testkit.Rows("t5 " + expect))

tk.MustExec("create temporary table t6 (i int primary key, j int)")
expect = "CREATE TEMPORARY TABLE `t6` (\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"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
tk.MustQuery("show create table t6").Check(testkit.Rows("t6 " + expect))
tk.MustExec("create temporary table t7 (i int primary key auto_increment, j int)")
defer func() {
Expand All @@ -1484,6 +1484,6 @@ func (s *testSuite5) TestShowTemporaryTable(c *C) {
" `i` int(11) NOT NULL AUTO_INCREMENT,\n" +
" `j` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n" +
") ENGINE=memory DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2"
tk.MustQuery("show create table t7").Check(testkit.Rows("t7 " + expect))
}

0 comments on commit 46b87ac

Please sign in to comment.