Skip to content

Commit

Permalink
table: fix cannot find the temp table when it's created in a transact…
Browse files Browse the repository at this point in the history
…ion (#35663)

close #35644
  • Loading branch information
djshow832 authored Jun 23, 2022
1 parent 028bdcf commit 011074c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
20 changes: 20 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion table/temptable/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions table/temptable/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 011074c

Please sign in to comment.