From 6800748915d6d206e33c5cb26e121ba56e4f0d0c Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Tue, 6 Jul 2021 13:25:21 +0800 Subject: [PATCH] *: Support create local temporary table if not exist --- ddl/db_integration_test.go | 6 ++++++ executor/ddl.go | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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 {