Skip to content

Commit bd17acd

Browse files
authored
infoschema: introduce MetaOnlyInfoSchema to provide meta only information schema (#52070)
close #52072
1 parent 311eef9 commit bd17acd

File tree

22 files changed

+231
-44
lines changed

22 files changed

+231
-44
lines changed

br/pkg/lightning/backend/kv/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func (se *Session) Value(key fmt.Stringer) any {
428428
func (*Session) StmtAddDirtyTableOP(_ int, _ int64, _ kv.Handle) {}
429429

430430
// GetInfoSchema implements the sessionctx.Context interface.
431-
func (*Session) GetInfoSchema() infoschema.InfoSchemaMetaVersion {
431+
func (*Session) GetInfoSchema() infoschema.MetaOnlyInfoSchema {
432432
return nil
433433
}
434434

pkg/ddl/schematracker/info_store.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,32 +152,33 @@ func (i *InfoStore) AllTableNamesOfSchema(schema model.CIStr) ([]string, error)
152152

153153
// InfoStoreAdaptor convert InfoStore to InfoSchema, it only implements a part of InfoSchema interface to be
154154
// used by DDL interface.
155-
// nolint:unused
156155
type InfoStoreAdaptor struct {
157156
infoschema.InfoSchema
158157
inner *InfoStore
159158
}
160159

161160
// SchemaByName implements the InfoSchema interface.
162-
// nolint:unused
163161
func (i InfoStoreAdaptor) SchemaByName(schema model.CIStr) (*model.DBInfo, bool) {
164162
dbInfo := i.inner.SchemaByName(schema)
165163
return dbInfo, dbInfo != nil
166164
}
167165

168166
// TableExists implements the InfoSchema interface.
169-
// nolint:unused
170167
func (i InfoStoreAdaptor) TableExists(schema, table model.CIStr) bool {
171168
tableInfo, _ := i.inner.TableByName(schema, table)
172169
return tableInfo != nil
173170
}
174171

175172
// TableByName implements the InfoSchema interface.
176-
// nolint:unused
177173
func (i InfoStoreAdaptor) TableByName(schema, table model.CIStr) (t table.Table, err error) {
178174
tableInfo, err := i.inner.TableByName(schema, table)
179175
if err != nil {
180176
return nil, err
181177
}
182178
return tables.MockTableFromMeta(tableInfo), nil
183179
}
180+
181+
// TableInfoByName implements the InfoSchema interface.
182+
func (i InfoStoreAdaptor) TableInfoByName(schema, table model.CIStr) (*model.TableInfo, error) {
183+
return i.inner.TableByName(schema, table)
184+
}

pkg/expression/builtin_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ func (c *tidbDecodeKeyFunctionClass) getFunction(ctx BuildContext, args []Expres
909909
}
910910

911911
// DecodeKeyFromString is used to decode key by expressions
912-
var DecodeKeyFromString func(types.Context, infoschema.InfoSchemaMetaVersion, string) string
912+
var DecodeKeyFromString func(types.Context, infoschema.MetaOnlyInfoSchema, string) string
913913

914914
type builtinTiDBDecodeKeySig struct {
915915
baseBuiltinFunc

pkg/expression/builtin_info_vec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func (b *builtinTiDBDecodeKeySig) vecEvalString(ctx EvalContext, input *chunk.Ch
408408

409409
decode := DecodeKeyFromString
410410
if decode == nil {
411-
decode = func(_ types.Context, _ infoschema.InfoSchemaMetaVersion, s string) string {
411+
decode = func(_ types.Context, _ infoschema.MetaOnlyInfoSchema, s string) string {
412412
return s
413413
}
414414
}

pkg/expression/contextimpl/sessionctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func currentUserProp(sctx sessionctx.Context) exprctx.OptionalEvalPropProvider {
223223
}
224224

225225
func infoSchemaProp(sctx sessionctx.Context) contextopt.InfoSchemaPropProvider {
226-
return func(isDomain bool) infoschema.InfoSchemaMetaVersion {
226+
return func(isDomain bool) infoschema.MetaOnlyInfoSchema {
227227
if isDomain {
228228
return sctx.GetDomainInfoSchema()
229229
}

pkg/expression/contextopt/infoschema.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
// InfoSchemaPropProvider is the function to provide information schema.
23-
type InfoSchemaPropProvider func(isDomain bool) infoschema.InfoSchemaMetaVersion
23+
type InfoSchemaPropProvider func(isDomain bool) infoschema.MetaOnlyInfoSchema
2424

2525
// Desc returns the description for the property key.
2626
func (InfoSchemaPropProvider) Desc() *context.OptionalEvalPropDesc {
@@ -36,7 +36,7 @@ func (InfoSchemaPropReader) RequiredOptionalEvalProps() context.OptionalEvalProp
3636
}
3737

3838
// GetSessionInfoSchema returns session information schema.
39-
func (InfoSchemaPropReader) GetSessionInfoSchema(ctx context.EvalContext) (infoschema.InfoSchemaMetaVersion, error) {
39+
func (InfoSchemaPropReader) GetSessionInfoSchema(ctx context.EvalContext) (infoschema.MetaOnlyInfoSchema, error) {
4040
p, err := getPropProvider[InfoSchemaPropProvider](ctx, context.OptPropInfoSchema)
4141
if err != nil {
4242
return nil, err
@@ -45,7 +45,7 @@ func (InfoSchemaPropReader) GetSessionInfoSchema(ctx context.EvalContext) (infos
4545
}
4646

4747
// GetDomainInfoSchema return domain information schema.
48-
func (InfoSchemaPropReader) GetDomainInfoSchema(ctx context.EvalContext) (infoschema.InfoSchemaMetaVersion, error) {
48+
func (InfoSchemaPropReader) GetDomainInfoSchema(ctx context.EvalContext) (infoschema.MetaOnlyInfoSchema, error) {
4949
p, err := getPropProvider[InfoSchemaPropProvider](ctx, context.OptPropInfoSchema)
5050
if err != nil {
5151
return nil, err

pkg/expression/contextopt/optional_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func TestOptionalEvalPropProviders(t *testing.T) {
105105
}
106106
case context.OptPropInfoSchema:
107107
type mockIsType struct {
108-
infoschema.InfoSchemaMetaVersion
108+
infoschema.MetaOnlyInfoSchema
109109
}
110110
var is1, is2 mockIsType
111-
p = InfoSchemaPropProvider(func(isDomain bool) infoschema.InfoSchemaMetaVersion {
111+
p = InfoSchemaPropProvider(func(isDomain bool) infoschema.MetaOnlyInfoSchema {
112112
if isDomain {
113113
return &is1
114114
}

pkg/infoschema/context/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go_library(
55
srcs = ["infoschema.go"],
66
importpath = "github.com/pingcap/tidb/pkg/infoschema/context",
77
visibility = ["//visibility:public"],
8+
deps = ["//pkg/parser/model"],
89
)

pkg/infoschema/context/infoschema.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414

1515
package context
1616

17-
// InfoSchemaMetaVersion is a workaround. Due to circular dependency,
18-
// can not return the complete interface. But SchemaMetaVersion is widely used for logging.
19-
// So we give a convenience for that.
20-
// FIXME: remove this interface
21-
type InfoSchemaMetaVersion interface {
17+
import "github.com/pingcap/tidb/pkg/parser/model"
18+
19+
// MetaOnlyInfoSchema is a workaround.
20+
// Due to circular dependency cannot return the complete interface.
21+
// But MetaOnlyInfoSchema is widely used for scenes that require meta only, so we give a convenience for that.
22+
type MetaOnlyInfoSchema interface {
2223
SchemaMetaVersion() int64
24+
SchemaByName(schema model.CIStr) (*model.DBInfo, bool)
25+
SchemaExists(schema model.CIStr) bool
26+
TableInfoByName(schema, table model.CIStr) (*model.TableInfo, error)
27+
TableInfoByID(id int64) (*model.TableInfo, bool)
28+
FindTableInfoByPartitionID(partitionID int64) (*model.TableInfo, *model.DBInfo, *model.PartitionDefinition)
29+
TableExists(schema, table model.CIStr) bool
30+
SchemaByID(id int64) (*model.DBInfo, bool)
31+
AllSchemas() []*model.DBInfo
32+
AllSchemaNames() []model.CIStr
33+
SchemaTableInfos(schema model.CIStr) []*model.TableInfo
2334
}

pkg/infoschema/infoschema.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ func (is *infoSchema) TableByName(schema, table model.CIStr) (t table.Table, err
177177
return nil, ErrTableNotExists.GenWithStackByArgs(schema, table)
178178
}
179179

180+
// TableInfoByName implements InfoSchema.TableInfoByName
181+
func (is *infoSchema) TableInfoByName(schema, table model.CIStr) (*model.TableInfo, error) {
182+
tbl, err := is.TableByName(schema, table)
183+
return getTableInfo(tbl), err
184+
}
185+
180186
// TableIsView indicates whether the schema.table is a view.
181187
func TableIsView(is InfoSchema, schema, table model.CIStr) bool {
182188
tbl, err := is.TableByName(schema, table)
@@ -240,6 +246,25 @@ func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) {
240246
return slice[idx], true
241247
}
242248

249+
// TableInfoByID implements InfoSchema.TableInfoByID
250+
func (is *infoSchema) TableInfoByID(id int64) (*model.TableInfo, bool) {
251+
tbl, ok := is.TableByID(id)
252+
return getTableInfo(tbl), ok
253+
}
254+
255+
// FindTableInfoByPartitionID implements InfoSchema.FindTableInfoByPartitionID
256+
func (is *infoSchema) FindTableInfoByPartitionID(
257+
partitionID int64,
258+
) (*model.TableInfo, *model.DBInfo, *model.PartitionDefinition) {
259+
tbl, db, partDef := is.FindTableByPartitionID(partitionID)
260+
return getTableInfo(tbl), db, partDef
261+
}
262+
263+
// SchemaTableInfos implements InfoSchema.FindTableInfoByPartitionID
264+
func (is *infoSchema) SchemaTableInfos(schema model.CIStr) []*model.TableInfo {
265+
return getTableInfoList(is.SchemaTables(schema))
266+
}
267+
243268
// allocByID returns the Allocators of a table.
244269
func allocByID(is InfoSchema, id int64) (autoid.Allocators, bool) {
245270
tbl, ok := is.TableByID(id)
@@ -351,10 +376,10 @@ func init() {
351376
Tables: infoSchemaTables,
352377
}
353378
RegisterVirtualTable(infoSchemaDB, createInfoSchemaTable)
354-
util.GetSequenceByName = func(is context.InfoSchemaMetaVersion, schema, sequence model.CIStr) (util.SequenceTable, error) {
379+
util.GetSequenceByName = func(is context.MetaOnlyInfoSchema, schema, sequence model.CIStr) (util.SequenceTable, error) {
355380
return GetSequenceByName(is.(InfoSchema), schema, sequence)
356381
}
357-
mock.MockInfoschema = func(tbList []*model.TableInfo) context.InfoSchemaMetaVersion {
382+
mock.MockInfoschema = func(tbList []*model.TableInfo) context.MetaOnlyInfoSchema {
358383
return MockInfoSchema(tbList)
359384
}
360385
}
@@ -666,6 +691,26 @@ func (ts *SessionExtendedInfoSchema) TableByName(schema, table model.CIStr) (tab
666691
return ts.InfoSchema.TableByName(schema, table)
667692
}
668693

694+
// TableInfoByName implements InfoSchema.TableInfoByName
695+
func (ts *SessionExtendedInfoSchema) TableInfoByName(schema, table model.CIStr) (*model.TableInfo, error) {
696+
tbl, err := ts.TableByName(schema, table)
697+
return getTableInfo(tbl), err
698+
}
699+
700+
// TableInfoByID implements InfoSchema.TableInfoByID
701+
func (ts *SessionExtendedInfoSchema) TableInfoByID(id int64) (*model.TableInfo, bool) {
702+
tbl, ok := ts.TableByID(id)
703+
return getTableInfo(tbl), ok
704+
}
705+
706+
// FindTableInfoByPartitionID implements InfoSchema.FindTableInfoByPartitionID
707+
func (ts *SessionExtendedInfoSchema) FindTableInfoByPartitionID(
708+
partitionID int64,
709+
) (*model.TableInfo, *model.DBInfo, *model.PartitionDefinition) {
710+
tbl, db, partDef := ts.FindTableByPartitionID(partitionID)
711+
return getTableInfo(tbl), db, partDef
712+
}
713+
669714
// TableByID implements InfoSchema.TableByID
670715
func (ts *SessionExtendedInfoSchema) TableByID(id int64) (table.Table, bool) {
671716
if ts.LocalTemporaryTables != nil {
@@ -739,3 +784,22 @@ func FindTableByTblOrPartID(is InfoSchema, id int64) (table.Table, *model.Partit
739784
tbl, _, partDef := is.FindTableByPartitionID(id)
740785
return tbl, partDef
741786
}
787+
788+
func getTableInfo(tbl table.Table) *model.TableInfo {
789+
if tbl == nil {
790+
return nil
791+
}
792+
return tbl.Meta()
793+
}
794+
795+
func getTableInfoList(tables []table.Table) []*model.TableInfo {
796+
if tables == nil {
797+
return nil
798+
}
799+
800+
infoLost := make([]*model.TableInfo, 0, len(tables))
801+
for _, tbl := range tables {
802+
infoLost = append(infoLost, tbl.Meta())
803+
}
804+
return infoLost
805+
}

0 commit comments

Comments
 (0)