Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: fix cannot find the temp table when it's created in a transaction #35663

Merged
merged 3 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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