diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 5420de5855391..0cc98f6ef35e6 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -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") diff --git a/executor/ddl.go b/executor/ddl.go index 500d7ea70c001..1398cead232a9 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -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 {