Skip to content

Commit a248b96

Browse files
authored
planner: use TableInfo.DBID to locate schema (#57785) (#57870)
close #57779, close #57783
1 parent afb68c6 commit a248b96

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

pkg/planner/core/memtable_infoschema_extractor.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ import (
3333
"github.com/pingcap/tidb/pkg/planner/core/base"
3434
"github.com/pingcap/tidb/pkg/types"
3535
"github.com/pingcap/tidb/pkg/util/collate"
36+
"github.com/pingcap/tidb/pkg/util/intest"
37+
"github.com/pingcap/tidb/pkg/util/logutil"
3638
"github.com/pingcap/tidb/pkg/util/set"
39+
"go.uber.org/zap"
3740
"golang.org/x/exp/maps"
3841
)
3942

@@ -110,24 +113,37 @@ func (e *InfoSchemaBaseExtractor) GetBase() *InfoSchemaBaseExtractor {
110113
return e
111114
}
112115

113-
// ListSchemas lists related schemas from predicate.
116+
// ListSchemas lists all schemas from predicate. If no schema is specified, it lists
117+
// all schemas in the storage.
114118
func (e *InfoSchemaBaseExtractor) ListSchemas(is infoschema.InfoSchema) []pmodel.CIStr {
115-
ec := e.extractableColumns
116-
schemas := e.getSchemaObjectNames(ec.schema)
117-
if len(schemas) == 0 {
119+
extractedSchemas, unspecified := e.listPredicateSchemas(is)
120+
if unspecified {
118121
ret := is.AllSchemaNames()
119122
slices.SortFunc(ret, func(a, b pmodel.CIStr) int {
120123
return strings.Compare(a.L, b.L)
121124
})
122-
return filterSchemaObjectByRegexp(e, ec.schema, ret, extractStrCIStr)
125+
return filterSchemaObjectByRegexp(e, e.extractableColumns.schema, ret, extractStrCIStr)
126+
}
127+
return extractedSchemas
128+
}
129+
130+
// listPredicateSchemas lists all schemas specified in predicates.
131+
// If no schema is specified in predicates, return `unspecified` as true.
132+
func (e *InfoSchemaBaseExtractor) listPredicateSchemas(
133+
is infoschema.InfoSchema,
134+
) (schemas []pmodel.CIStr, unspecified bool) {
135+
ec := e.extractableColumns
136+
schemas = e.getSchemaObjectNames(ec.schema)
137+
if len(schemas) == 0 {
138+
return nil, true
123139
}
124140
ret := schemas[:0]
125141
for _, s := range schemas {
126142
if n, ok := is.SchemaByName(s); ok {
127143
ret = append(ret, n.Name)
128144
}
129145
}
130-
return filterSchemaObjectByRegexp(e, ec.schema, ret, extractStrCIStr)
146+
return filterSchemaObjectByRegexp(e, ec.schema, ret, extractStrCIStr), false
131147
}
132148

133149
// ListSchemasAndTables lists related tables and their corresponding schemas from predicate.
@@ -137,7 +153,6 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
137153
is infoschema.InfoSchema,
138154
) ([]pmodel.CIStr, []*model.TableInfo, error) {
139155
ec := e.extractableColumns
140-
schemas := e.ListSchemas(is)
141156
var tableNames []pmodel.CIStr
142157
if ec.table != "" {
143158
tableNames = e.getSchemaObjectNames(ec.table)
@@ -150,7 +165,7 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
150165
findTablesByID(is, tableIDs, tableNames, tableMap)
151166
tableSlice := maps.Values(tableMap)
152167
tableSlice = filterSchemaObjectByRegexp(e, ec.table, tableSlice, extractStrTableInfo)
153-
return findSchemasForTables(ctx, is, schemas, tableSlice)
168+
return findSchemasForTables(e, is, tableSlice)
154169
}
155170
}
156171
if ec.partitionID != "" {
@@ -160,13 +175,15 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
160175
findTablesByPartID(is, partIDs, tableNames, tableMap)
161176
tableSlice := maps.Values(tableMap)
162177
tableSlice = filterSchemaObjectByRegexp(e, ec.table, tableSlice, extractStrTableInfo)
163-
return findSchemasForTables(ctx, is, schemas, tableSlice)
178+
return findSchemasForTables(e, is, tableSlice)
164179
}
165180
}
166181
if len(tableNames) > 0 {
167182
tableNames = filterSchemaObjectByRegexp(e, ec.table, tableNames, extractStrCIStr)
183+
schemas := e.ListSchemas(is)
168184
return findTableAndSchemaByName(ctx, is, schemas, tableNames)
169185
}
186+
schemas := e.ListSchemas(is)
170187
return listTablesForEachSchema(ctx, e, is, schemas)
171188
}
172189

@@ -723,23 +740,28 @@ func listTablesForEachSchema(
723740
// returns a schema slice and a table slice that has the same length.
724741
// Note that input arg "tableSlice" will be changed in place.
725742
func findSchemasForTables(
726-
ctx context.Context,
743+
e *InfoSchemaBaseExtractor,
727744
is infoschema.InfoSchema,
728-
schemas []pmodel.CIStr,
729745
tableSlice []*model.TableInfo,
730746
) ([]pmodel.CIStr, []*model.TableInfo, error) {
747+
schemas, unspecified := e.listPredicateSchemas(is)
731748
schemaSlice := make([]pmodel.CIStr, 0, len(tableSlice))
732749
for i, tbl := range tableSlice {
750+
dbInfo, ok := is.SchemaByID(tbl.DBID)
751+
intest.Assert(ok)
752+
if !ok {
753+
logutil.BgLogger().Warn("schema not found for table info",
754+
zap.Int64("tableID", tbl.ID), zap.Int64("dbID", tbl.DBID))
755+
continue
756+
}
757+
if unspecified { // all schemas should be included.
758+
schemaSlice = append(schemaSlice, dbInfo.Name)
759+
continue
760+
}
761+
733762
found := false
734763
for _, s := range schemas {
735-
isTbl, err := is.TableByName(ctx, s, tbl.Name)
736-
if err != nil {
737-
if terror.ErrorEqual(err, infoschema.ErrTableNotExists) {
738-
continue
739-
}
740-
return nil, nil, errors.Trace(err)
741-
}
742-
if isTbl.Meta().ID == tbl.ID {
764+
if s.L == dbInfo.Name.L {
743765
schemaSlice = append(schemaSlice, s)
744766
found = true
745767
break

0 commit comments

Comments
 (0)