Skip to content

Commit

Permalink
infoschema: add the SchemaNameByTableID API (#56156)
Browse files Browse the repository at this point in the history
ref #55906
  • Loading branch information
Rustin170506 authored Sep 25, 2024
1 parent 918b58d commit 8c12f12
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pkg/infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ func (is *infoSchema) TableByID(_ stdctx.Context, id int64) (val table.Table, ok
return slice[idx], true
}

func (is *infoSchema) SchemaNameByTableID(tableID int64) (schemaName pmodel.CIStr, ok bool) {
tbl, ok := is.TableByID(stdctx.Background(), tableID)
if !ok {
return
}
db, ok := is.SchemaByID(tbl.Meta().DBID)
if !ok {
return
}

return db.Name, true
}

// TableInfoByID implements InfoSchema.TableInfoByID
func (is *infoSchema) TableInfoByID(id int64) (*model.TableInfo, bool) {
tbl, ok := is.TableByID(stdctx.Background(), id)
Expand Down
35 changes: 35 additions & 0 deletions pkg/infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,41 @@ func TestBasic(t *testing.T) {
tbls, err := is.SchemaTableInfos(context.Background(), schema.Name)
require.NoError(t, err)
require.Equal(t, 1, len(tbls))

// Test SchemaNameByTableID
tests := []struct {
name string
tableID int64
wantSchema pmodel.CIStr
wantOK bool
}{
{
name: "valid table ID",
tableID: tbID,
wantSchema: dbName,
wantOK: true,
},
{
name: "non-existent table ID",
tableID: tbID + 1,
wantSchema: pmodel.CIStr{},
wantOK: false,
},
{
name: "invalid table ID (negative)",
tableID: -1,
wantSchema: pmodel.CIStr{},
wantOK: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSchema, gotOK := is.SchemaNameByTableID(tt.tableID)
require.Equal(t, tt.wantOK, gotOK)
require.Equal(t, tt.wantSchema, gotSchema)
})
}
}

func TestMockInfoSchema(t *testing.T) {
Expand Down
26 changes: 24 additions & 2 deletions pkg/infoschema/infoschema_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ func (is *infoschemaV2) CloneAndUpdateTS(startTS uint64) *infoschemaV2 {
return &tmp
}

func (is *infoschemaV2) searchTableItemByID(tableID int64) (tableItem, bool) {
eq := func(a, b *tableItem) bool { return a.tableID == b.tableID }
return search(
is.byID,
is.infoSchema.schemaMetaVersion,
tableItem{tableID: tableID, schemaVersion: math.MaxInt64},
eq,
)
}

// TableByID implements the InfoSchema interface.
// As opposed to TableByName, TableByID will not refill cache when schema cache miss,
// unless the caller changes the behavior by passing a context use WithRefillOption.
Expand All @@ -604,8 +614,7 @@ func (is *infoschemaV2) TableByID(ctx context.Context, id int64) (val table.Tabl
return tbl, true
}

eq := func(a, b *tableItem) bool { return a.tableID == b.tableID }
itm, ok := search(is.byID, is.infoSchema.schemaMetaVersion, tableItem{tableID: id, schemaVersion: math.MaxInt64}, eq)
itm, ok := is.searchTableItemByID(id)
if !ok {
return nil, false
}
Expand Down Expand Up @@ -646,6 +655,19 @@ func (is *infoschemaV2) TableByID(ctx context.Context, id int64) (val table.Tabl
return ret, true
}

func (is *infoschemaV2) SchemaNameByTableID(tableID int64) (schemaName pmodel.CIStr, ok bool) {
if !tableIDIsValid(tableID) {
return
}

itm, ok := is.searchTableItemByID(tableID)
if !ok {
return
}

return itm.dbName, true
}

// TableItem is exported from tableItem.
type TableItem struct {
DBName pmodel.CIStr
Expand Down
37 changes: 37 additions & 0 deletions pkg/infoschema/infoschema_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,43 @@ func TestV2Basic(t *testing.T) {
require.Equal(t, 0, len(tblInfos))

require.Equal(t, int64(2), is.SchemaMetaVersion())

// Test SchemaNameByTableID
schemaNameByTableIDTests := []struct {
name string
tableID int64
wantSchema pmodel.CIStr
wantOK bool
}{
{
name: "valid table ID",
tableID: tblInfo.ID,
wantSchema: schemaName,
wantOK: true,
},
{
name: "non-existent table ID",
tableID: tblInfo.ID + 1,
wantSchema: pmodel.CIStr{},
wantOK: false,
},
{
name: "invalid table ID (negative)",
tableID: -1,
wantSchema: pmodel.CIStr{},
wantOK: false,
},
}

for _, tt := range schemaNameByTableIDTests {
t.Run(tt.name, func(t *testing.T) {
gotSchema, gotOK := is.SchemaNameByTableID(tt.tableID)

require.Equal(t, tt.wantOK, gotOK)
require.Equal(t, tt.wantSchema, gotSchema)
})
}

// TODO: support FindTableByPartitionID.
}

Expand Down
1 change: 1 addition & 0 deletions pkg/infoschema/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type InfoSchema interface {
context.MetaOnlyInfoSchema
TableByName(ctx stdctx.Context, schema, table pmodel.CIStr) (table.Table, error)
TableByID(ctx stdctx.Context, id int64) (table.Table, bool)
SchemaNameByTableID(tableID int64) (pmodel.CIStr, bool)
FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition)
ListTablesWithSpecialAttribute(filter specialAttributeFilter) []tableInfoResult
base() *infoSchema
Expand Down

0 comments on commit 8c12f12

Please sign in to comment.