From 011074cc13187d6885cee4d9ce62bc46651492ff Mon Sep 17 00:00:00 2001 From: djshow832 Date: Thu, 23 Jun 2022 18:58:37 +0800 Subject: [PATCH] table: fix cannot find the temp table when it's created in a transaction (#35663) close pingcap/tidb#35644 --- ddl/db_integration_test.go | 20 ++++++++++++++++++++ infoschema/infoschema.go | 5 +++++ table/temptable/ddl_test.go | 2 +- table/temptable/infoschema.go | 12 ++---------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index fff7e16f8a33c..23c10805724be 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -3814,6 +3814,26 @@ func TestIssue29282(t *testing.T) { } } +// See https://github.com/pingcap/tidb/issues/35644 +func TestCreateTempTableInTxn(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("begin") + tk.MustExec("create temporary table t1(id int)") + tk.MustQuery("select * from t1") + tk.MustExec("commit") + + tk1 := testkit.NewTestKit(t, store) + tk1.MustExec("use test") + tk1.MustExec("create table tt(id int)") + tk1.MustExec("begin") + tk1.MustExec("create temporary table t1(id int)") + tk1.MustExec("insert into tt select * from t1") + tk1.MustExec("drop table tt") +} + // See https://github.com/pingcap/tidb/issues/29327 func TestEnumDefaultValue(t *testing.T) { store, clean := testkit.CreateMockStore(t) diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index c96fa6d59dc83..3200166d21af0 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -487,6 +487,11 @@ func (is *LocalTemporaryTables) RemoveTable(schema, table model.CIStr) (exist bo return true } +// Count gets the count of the temporary tables. +func (is *LocalTemporaryTables) Count() int { + return len(is.idx2table) +} + // SchemaByTable get a table's schema name func (is *LocalTemporaryTables) SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool) { if tableInfo == nil { diff --git a/table/temptable/ddl_test.go b/table/temptable/ddl_test.go index 1418a35ad83d1..e08343c1177a6 100644 --- a/table/temptable/ddl_test.go +++ b/table/temptable/ddl_test.go @@ -164,7 +164,7 @@ func TestTruncateLocalTemporaryTable(t *testing.T) { // truncate when empty err := ddl.TruncateLocalTemporaryTable(model.NewCIStr("db1"), model.NewCIStr("t1")) require.True(t, infoschema.ErrTableNotExists.Equal(err)) - require.Nil(t, sessVars.LocalTemporaryTables) + require.Equal(t, 0, sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables).Count()) require.Nil(t, sessVars.TemporaryTableData) // add one table diff --git a/table/temptable/infoschema.go b/table/temptable/infoschema.go index 7947027ba0314..764bee0dbbce0 100644 --- a/table/temptable/infoschema.go +++ b/table/temptable/infoschema.go @@ -26,10 +26,6 @@ func AttachLocalTemporaryTableInfoSchema(sctx sessionctx.Context, is infoschema. } localTemporaryTables := getLocalTemporaryTables(sctx) - if localTemporaryTables == nil { - return is - } - return &infoschema.TemporaryTableAttachedInfoSchema{ InfoSchema: is, LocalTemporaryTables: localTemporaryTables, @@ -46,12 +42,8 @@ func DetachLocalTemporaryTableInfoSchema(is infoschema.InfoSchema) infoschema.In } func getLocalTemporaryTables(sctx sessionctx.Context) *infoschema.LocalTemporaryTables { - localTemporaryTables := sctx.GetSessionVars().LocalTemporaryTables - if localTemporaryTables == nil { - return nil - } - - return localTemporaryTables.(*infoschema.LocalTemporaryTables) + // Do not return nil so that new created tables can always be visited through the returned object. + return ensureLocalTemporaryTables(sctx) } func ensureLocalTemporaryTables(sctx sessionctx.Context) *infoschema.LocalTemporaryTables {