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,table: fix 'show create table' for the temporary table #24857

Merged
merged 8 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,13 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
}

sqlMode := ctx.GetSessionVars().SQLMode
fmt.Fprintf(buf, "CREATE TABLE %s (\n", stringutil.Escape(tableInfo.Name.O, sqlMode))
tableName := stringutil.Escape(tableInfo.Name.O, sqlMode)
switch tableInfo.TempTableType {
case model.TempTableGlobal:
fmt.Fprintf(buf, "CREATE /* GLOBAL TEMPORARY */ TABLE %s (\n", tableName)
default:
fmt.Fprintf(buf, "CREATE TABLE %s (\n", tableName)
}
var pkCol *model.ColumnInfo
var hasAutoIncID bool
needAddComma := false
Expand Down Expand Up @@ -1009,6 +1015,11 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if len(tableInfo.Comment) > 0 {
fmt.Fprintf(buf, " COMMENT='%s'", format.OutputFormat(tableInfo.Comment))
}

if tableInfo.TempTableType == model.TempTableGlobal {
fmt.Fprintf(buf, " /* ON COMMIT DELETE ROWS */")
}

// add partition info here.
appendPartitionInfo(tableInfo.Partition, buf)
return nil
Expand Down
22 changes: 22 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,3 +1303,25 @@ func (s *testSuite5) TestShowPerformanceSchema(c *C) {
testkit.Rows("events_statements_summary_by_digest 0 SCHEMA_NAME 1 SCHEMA_NAME A 0 <nil> <nil> YES BTREE YES NULL NO",
"events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 <nil> <nil> YES BTREE YES NULL NO"))
}

func (s *testSuite5) TestShowTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create global temporary table t1 (id int) on commit delete rows")
tk.MustExec("create global temporary table t3 (i int primary key, j int) on commit delete rows")
// 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=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=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.
createTable := strings.ReplaceAll(expect, "t3", "t4")
tk.MustExec(createTable)
}
7 changes: 5 additions & 2 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,11 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators {
} else if ctx.GetSessionVars().IDAllocator == nil {
// Use an independent allocator for global temporary tables.
if t.meta.TempTableType == model.TempTableGlobal {
alloc := ctx.GetSessionVars().GetTemporaryTable(t.meta).GetAutoIDAllocator()
return autoid.Allocators{alloc}
if alloc := ctx.GetSessionVars().GetTemporaryTable(t.meta).GetAutoIDAllocator(); alloc != nil {
return autoid.Allocators{alloc}
}
// If the session is not in a txn, for example, in "show create table", use the original allocator.
// Otherwise the would be a nil pointer dereference,
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
}
return t.allocs
}
Expand Down