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

*: rename TemporaryTableAttachedInfoSchema #37408

Merged
merged 3 commits into from
Aug 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions ddl/db_integration_test.go
Expand Up @@ -3460,7 +3460,7 @@ func TestDropTemporaryTable(t *testing.T) {
require.NoError(t, err)
sessionVars := tk.Session().GetSessionVars()
sessVarsTempTable := sessionVars.LocalTemporaryTables
localTemporaryTable := sessVarsTempTable.(*infoschema.LocalTemporaryTables)
localTemporaryTable := sessVarsTempTable.(*infoschema.SessionTables)
tbl, exist := localTemporaryTable.TableByName(model.NewCIStr("test"), model.NewCIStr("a_local_temp_table_7"))
require.True(t, exist)
tblInfo := tbl.Meta()
Expand Down Expand Up @@ -3719,7 +3719,7 @@ func TestTruncateLocalTemporaryTable(t *testing.T) {
tk.MustExec("create temporary table t1 (id int primary key auto_increment)")

// truncate temporary table will clear session data
localTemporaryTables := tk.Session().GetSessionVars().LocalTemporaryTables.(*infoschema.LocalTemporaryTables)
localTemporaryTables := tk.Session().GetSessionVars().LocalTemporaryTables.(*infoschema.SessionTables)
tb1, exist := localTemporaryTables.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
tbl1Info := tb1.Meta()
tablePrefix := tablecodec.EncodeTablePrefix(tbl1Info.ID)
Expand Down
42 changes: 21 additions & 21 deletions infoschema/infoschema.go
Expand Up @@ -417,25 +417,25 @@ func (is *infoSchema) deletePolicy(name string) {
delete(is.policyMap, name)
}

// LocalTemporaryTables store local temporary tables
type LocalTemporaryTables struct {
// SessionTables store local temporary tables
type SessionTables struct {
// Local temporary tables can be accessed after the db is dropped, so there needs a way to retain the DBInfo.
// schemaTables.dbInfo will only be used when the db is dropped and it may be stale after the db is created again.
// But it's fine because we only need its name.
schemaMap map[string]*schemaTables
idx2table map[int64]table.Table
}

// NewLocalTemporaryTables creates a new NewLocalTemporaryTables object
func NewLocalTemporaryTables() *LocalTemporaryTables {
return &LocalTemporaryTables{
// NewSessionTables creates a new NewSessionTables object
func NewSessionTables() *SessionTables {
return &SessionTables{
schemaMap: make(map[string]*schemaTables),
idx2table: make(map[int64]table.Table),
}
}

// TableByName get table by name
func (is *LocalTemporaryTables) TableByName(schema, table model.CIStr) (table.Table, bool) {
func (is *SessionTables) TableByName(schema, table model.CIStr) (table.Table, bool) {
if tbNames, ok := is.schemaMap[schema.L]; ok {
if t, ok := tbNames.tables[table.L]; ok {
return t, true
Expand All @@ -445,19 +445,19 @@ func (is *LocalTemporaryTables) TableByName(schema, table model.CIStr) (table.Ta
}

// TableExists check if table with the name exists
func (is *LocalTemporaryTables) TableExists(schema, table model.CIStr) (ok bool) {
func (is *SessionTables) TableExists(schema, table model.CIStr) (ok bool) {
_, ok = is.TableByName(schema, table)
return
}

// TableByID get table by table id
func (is *LocalTemporaryTables) TableByID(id int64) (tbl table.Table, ok bool) {
func (is *SessionTables) TableByID(id int64) (tbl table.Table, ok bool) {
tbl, ok = is.idx2table[id]
return
}

// AddTable add a table
func (is *LocalTemporaryTables) AddTable(db *model.DBInfo, tbl table.Table) error {
func (is *SessionTables) AddTable(db *model.DBInfo, tbl table.Table) error {
schemaTables := is.ensureSchema(db)

tblMeta := tbl.Meta()
Expand All @@ -476,7 +476,7 @@ func (is *LocalTemporaryTables) AddTable(db *model.DBInfo, tbl table.Table) erro
}

// RemoveTable remove a table
func (is *LocalTemporaryTables) RemoveTable(schema, table model.CIStr) (exist bool) {
func (is *SessionTables) RemoveTable(schema, table model.CIStr) (exist bool) {
tbls := is.schemaTables(schema)
if tbls == nil {
return false
Expand All @@ -496,12 +496,12 @@ func (is *LocalTemporaryTables) RemoveTable(schema, table model.CIStr) (exist bo
}

// Count gets the count of the temporary tables.
func (is *LocalTemporaryTables) Count() int {
func (is *SessionTables) Count() int {
return len(is.idx2table)
}

// SchemaByTable get a table's schema name
func (is *LocalTemporaryTables) SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool) {
func (is *SessionTables) SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool) {
if tableInfo == nil {
return nil, false
}
Expand All @@ -517,7 +517,7 @@ func (is *LocalTemporaryTables) SchemaByTable(tableInfo *model.TableInfo) (*mode
return nil, false
}

func (is *LocalTemporaryTables) ensureSchema(db *model.DBInfo) *schemaTables {
func (is *SessionTables) ensureSchema(db *model.DBInfo) *schemaTables {
if tbls, ok := is.schemaMap[db.Name.L]; ok {
return tbls
}
Expand All @@ -527,7 +527,7 @@ func (is *LocalTemporaryTables) ensureSchema(db *model.DBInfo) *schemaTables {
return tbls
}

func (is *LocalTemporaryTables) schemaTables(schema model.CIStr) *schemaTables {
func (is *SessionTables) schemaTables(schema model.CIStr) *schemaTables {
if is.schemaMap == nil {
return nil
}
Expand All @@ -539,16 +539,16 @@ func (is *LocalTemporaryTables) schemaTables(schema model.CIStr) *schemaTables {
return nil
}

// TemporaryTableAttachedInfoSchema implements InfoSchema
// SessionExtendedInfoSchema implements InfoSchema
// Local temporary table has a loose relationship with database.
// So when a database is dropped, its temporary tables still exist and can be returned by TableByName/TableByID.
type TemporaryTableAttachedInfoSchema struct {
type SessionExtendedInfoSchema struct {
InfoSchema
LocalTemporaryTables *LocalTemporaryTables
LocalTemporaryTables *SessionTables
}

// TableByName implements InfoSchema.TableByName
func (ts *TemporaryTableAttachedInfoSchema) TableByName(schema, table model.CIStr) (table.Table, error) {
func (ts *SessionExtendedInfoSchema) TableByName(schema, table model.CIStr) (table.Table, error) {
if tbl, ok := ts.LocalTemporaryTables.TableByName(schema, table); ok {
return tbl, nil
}
Expand All @@ -557,7 +557,7 @@ func (ts *TemporaryTableAttachedInfoSchema) TableByName(schema, table model.CISt
}

// TableByID implements InfoSchema.TableByID
func (ts *TemporaryTableAttachedInfoSchema) TableByID(id int64) (table.Table, bool) {
func (ts *SessionExtendedInfoSchema) TableByID(id int64) (table.Table, bool) {
if tbl, ok := ts.LocalTemporaryTables.TableByID(id); ok {
return tbl, true
}
Expand All @@ -566,7 +566,7 @@ func (ts *TemporaryTableAttachedInfoSchema) TableByID(id int64) (table.Table, bo
}

// SchemaByTable implements InfoSchema.SchemaByTable, it returns a stale DBInfo even if it's dropped.
func (ts *TemporaryTableAttachedInfoSchema) SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool) {
func (ts *SessionExtendedInfoSchema) SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool) {
if tableInfo == nil {
return nil, false
}
Expand All @@ -579,6 +579,6 @@ func (ts *TemporaryTableAttachedInfoSchema) SchemaByTable(tableInfo *model.Table
}

// HasTemporaryTable returns whether information schema has temporary table
func (ts *TemporaryTableAttachedInfoSchema) HasTemporaryTable() bool {
func (ts *SessionExtendedInfoSchema) HasTemporaryTable() bool {
return ts.LocalTemporaryTables.Count() > 0 || ts.InfoSchema.HasTemporaryTable()
}
14 changes: 7 additions & 7 deletions infoschema/infoschema_test.go
Expand Up @@ -591,7 +591,7 @@ func TestLocalTemporaryTables(t *testing.T) {
return tbl
}

assertTableByName := func(sc *infoschema.LocalTemporaryTables, schemaName, tableName string, schema *model.DBInfo, tb table.Table) {
assertTableByName := func(sc *infoschema.SessionTables, schemaName, tableName string, schema *model.DBInfo, tb table.Table) {
got, ok := sc.TableByName(model.NewCIStr(schemaName), model.NewCIStr(tableName))
if tb == nil {
require.Nil(t, schema)
Expand All @@ -604,12 +604,12 @@ func TestLocalTemporaryTables(t *testing.T) {
}
}

assertTableExists := func(sc *infoschema.LocalTemporaryTables, schemaName, tableName string, exists bool) {
assertTableExists := func(sc *infoschema.SessionTables, schemaName, tableName string, exists bool) {
got := sc.TableExists(model.NewCIStr(schemaName), model.NewCIStr(tableName))
require.Equal(t, exists, got)
}

assertTableByID := func(sc *infoschema.LocalTemporaryTables, tbID int64, schema *model.DBInfo, tb table.Table) {
assertTableByID := func(sc *infoschema.SessionTables, tbID int64, schema *model.DBInfo, tb table.Table) {
got, ok := sc.TableByID(tbID)
if tb == nil {
require.Nil(t, schema)
Expand All @@ -622,7 +622,7 @@ func TestLocalTemporaryTables(t *testing.T) {
}
}

assertSchemaByTable := func(sc *infoschema.LocalTemporaryTables, db *model.DBInfo, tb *model.TableInfo) {
assertSchemaByTable := func(sc *infoschema.SessionTables, db *model.DBInfo, tb *model.TableInfo) {
got, ok := sc.SchemaByTable(tb)
if db == nil {
require.Nil(t, got)
Expand All @@ -634,7 +634,7 @@ func TestLocalTemporaryTables(t *testing.T) {
}
}

sc := infoschema.NewLocalTemporaryTables()
sc := infoschema.NewSessionTables()
db1 := createNewSchemaInfo("db1")
tb11 := createNewTable(db1.ID, "tb1", model.TempTableLocal)
tb12 := createNewTable(db1.ID, "Tb2", model.TempTableLocal)
Expand Down Expand Up @@ -737,14 +737,14 @@ func TestLocalTemporaryTables(t *testing.T) {
assertSchemaByTable(sc, nil, tb22.Meta())
assertSchemaByTable(sc, nil, nil)

// test TemporaryTableAttachedInfoSchema
// test SessionExtendedInfoSchema
dbTest := createNewSchemaInfo("test")
tmpTbTestA := createNewTable(dbTest.ID, "tba", model.TempTableLocal)
normalTbTestA := createNewTable(dbTest.ID, "tba", model.TempTableNone)
normalTbTestB := createNewTable(dbTest.ID, "tbb", model.TempTableNone)
normalTbTestC := createNewTable(db1.ID, "tbc", model.TempTableNone)

is := &infoschema.TemporaryTableAttachedInfoSchema{
is := &infoschema.SessionExtendedInfoSchema{
InfoSchema: infoschema.MockInfoSchema([]*model.TableInfo{normalTbTestA.Meta(), normalTbTestB.Meta()}),
LocalTemporaryTables: sc,
}
Expand Down
8 changes: 4 additions & 4 deletions session/session.go
Expand Up @@ -796,13 +796,13 @@ func (s *session) commitTxnWithTemporaryData(ctx context.Context, txn kv.Transac
sessionData := sessVars.TemporaryTableData
var (
stage kv.StagingHandle
localTempTables *infoschema.LocalTemporaryTables
localTempTables *infoschema.SessionTables
)

if sessVars.LocalTemporaryTables != nil {
localTempTables = sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables)
localTempTables = sessVars.LocalTemporaryTables.(*infoschema.SessionTables)
} else {
localTempTables = new(infoschema.LocalTemporaryTables)
localTempTables = new(infoschema.SessionTables)
}

defer func() {
Expand Down Expand Up @@ -3473,7 +3473,7 @@ func (s *session) EncodeSessionStates(ctx context.Context, sctx sessionctx.Conte
// Data in local temporary tables is hard to encode, so we do not support it.
// Check temporary tables here to avoid circle dependency.
if s.sessionVars.LocalTemporaryTables != nil {
localTempTables := s.sessionVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables)
localTempTables := s.sessionVars.LocalTemporaryTables.(*infoschema.SessionTables)
if localTempTables.Count() > 0 {
return sessionstates.ErrCannotMigrateSession.GenWithStackByArgs("session has local temporary tables")
}
Expand Down
4 changes: 2 additions & 2 deletions sessiontxn/failpoint.go
Expand Up @@ -75,9 +75,9 @@ func AssertTxnManagerInfoSchema(sctx sessionctx.Context, is interface{}) {
}

if localTables := sctx.GetSessionVars().LocalTemporaryTables; localTables != nil {
got, ok := GetTxnManager(sctx).GetTxnInfoSchema().(*infoschema.TemporaryTableAttachedInfoSchema)
got, ok := GetTxnManager(sctx).GetTxnInfoSchema().(*infoschema.SessionExtendedInfoSchema)
if !ok {
panic("Expected to be a TemporaryTableAttachedInfoSchema")
panic("Expected to be a SessionExtendedInfoSchema")
}

if got.LocalTemporaryTables != localTables {
Expand Down
4 changes: 2 additions & 2 deletions sessiontxn/isolation/optimistic_test.go
Expand Up @@ -322,7 +322,7 @@ func TestTidbSnapshotVarInOptimisticTxn(t *testing.T) {
checkUseSnapshot := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, snapshotISVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.Equal(t, snapshotTS, readTS)
Expand All @@ -334,7 +334,7 @@ func TestTidbSnapshotVarInOptimisticTxn(t *testing.T) {
checkUseTxn := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, isVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.NotEqual(t, snapshotTS, readTS)
Expand Down
4 changes: 2 additions & 2 deletions sessiontxn/isolation/readcommitted_test.go
Expand Up @@ -391,7 +391,7 @@ func TestTidbSnapshotVarInRC(t *testing.T) {
checkUseSnapshot := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, snapshotISVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.Equal(t, snapshotTS, readTS)
Expand All @@ -403,7 +403,7 @@ func TestTidbSnapshotVarInRC(t *testing.T) {
checkUseTxn := func(useTxnTs bool) {
is := provider.GetTxnInfoSchema()
require.Equal(t, isVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.NotEqual(t, snapshotTS, readTS)
Expand Down
4 changes: 2 additions & 2 deletions sessiontxn/isolation/repeatable_read_test.go
Expand Up @@ -286,7 +286,7 @@ func TestTidbSnapshotVarInPessimisticRepeatableRead(t *testing.T) {
checkUseSnapshot := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, snapshotISVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.Equal(t, snapshotTS, readTS)
Expand All @@ -298,7 +298,7 @@ func TestTidbSnapshotVarInPessimisticRepeatableRead(t *testing.T) {
checkUseTxn := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, isVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.NotEqual(t, snapshotTS, readTS)
Expand Down
4 changes: 2 additions & 2 deletions sessiontxn/isolation/serializable_test.go
Expand Up @@ -206,7 +206,7 @@ func TestTidbSnapshotVarInSerialize(t *testing.T) {
checkUseSnapshot := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, snapshotISVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.Equal(t, snapshotTS, readTS)
Expand All @@ -218,7 +218,7 @@ func TestTidbSnapshotVarInSerialize(t *testing.T) {
checkUseTxn := func() {
is := provider.GetTxnInfoSchema()
require.Equal(t, isVersion, is.SchemaMetaVersion())
require.IsType(t, &infoschema.TemporaryTableAttachedInfoSchema{}, is)
require.IsType(t, &infoschema.SessionExtendedInfoSchema{}, is)
readTS, err := provider.GetStmtReadTS()
require.NoError(t, err)
require.NotEqual(t, snapshotTS, readTS)
Expand Down
6 changes: 3 additions & 3 deletions sessiontxn/txn_manager_test.go
Expand Up @@ -182,7 +182,7 @@ func TestEnterNewTxn(t *testing.T) {
require.Equal(t, stalenessTS, txn.StartTS())
is := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema()
require.Equal(t, is1.SchemaMetaVersion(), is.SchemaMetaVersion())
_, ok := is.(*infoschema.TemporaryTableAttachedInfoSchema)
_, ok := is.(*infoschema.SessionExtendedInfoSchema)
require.True(t, ok)

sessVars := sctx.GetSessionVars()
Expand Down Expand Up @@ -567,10 +567,10 @@ func checkNonActiveStalenessTxn(t *testing.T, sctx sessionctx.Context, ts uint64
}

func checkInfoSchemaVersion(t *testing.T, sctx sessionctx.Context, ver int64) {
is1, ok := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema().(*infoschema.TemporaryTableAttachedInfoSchema)
is1, ok := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema().(*infoschema.SessionExtendedInfoSchema)
require.True(t, ok)

is2 := sctx.GetSessionVars().TxnCtx.InfoSchema.(*infoschema.TemporaryTableAttachedInfoSchema)
is2 := sctx.GetSessionVars().TxnCtx.InfoSchema.(*infoschema.SessionExtendedInfoSchema)
require.True(t, ok)

require.Equal(t, ver, is1.SchemaMetaVersion())
Expand Down