Skip to content

Commit

Permalink
*: Support create local temporary table if not exist (#25983)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Jul 7, 2021
1 parent 383fb9a commit c043d66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,12 @@ func (s *testIntegrationSuite3) TestCreateTemporaryTable(c *C) {
tk.MustExec("rollback")
tk.MustQuery("select * from check_data").Check(testkit.Rows())

// Check create temporary table for if not exists
tk.MustExec("create temporary table b_local_temp_table (id int)")
_, err = tk.Exec("create temporary table b_local_temp_table (id int)")
c.Assert(infoschema.ErrTableExists.Equal(err), IsTrue)
tk.MustExec("create temporary table if not exists b_local_temp_table (id int)")

// 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
9 changes: 8 additions & 1 deletion executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,14 @@ func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error {
sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables()
}
localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables)
return localTempTables.AddTable(dbInfo, tbl)
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 {
Expand Down

0 comments on commit c043d66

Please sign in to comment.