@@ -33,7 +33,10 @@ import (
33
33
"github.com/pingcap/tidb/pkg/planner/core/base"
34
34
"github.com/pingcap/tidb/pkg/types"
35
35
"github.com/pingcap/tidb/pkg/util/collate"
36
+ "github.com/pingcap/tidb/pkg/util/intest"
37
+ "github.com/pingcap/tidb/pkg/util/logutil"
36
38
"github.com/pingcap/tidb/pkg/util/set"
39
+ "go.uber.org/zap"
37
40
"golang.org/x/exp/maps"
38
41
)
39
42
@@ -110,24 +113,37 @@ func (e *InfoSchemaBaseExtractor) GetBase() *InfoSchemaBaseExtractor {
110
113
return e
111
114
}
112
115
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.
114
118
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 {
118
121
ret := is .AllSchemaNames ()
119
122
slices .SortFunc (ret , func (a , b pmodel.CIStr ) int {
120
123
return strings .Compare (a .L , b .L )
121
124
})
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
123
139
}
124
140
ret := schemas [:0 ]
125
141
for _ , s := range schemas {
126
142
if n , ok := is .SchemaByName (s ); ok {
127
143
ret = append (ret , n .Name )
128
144
}
129
145
}
130
- return filterSchemaObjectByRegexp (e , ec .schema , ret , extractStrCIStr )
146
+ return filterSchemaObjectByRegexp (e , ec .schema , ret , extractStrCIStr ), false
131
147
}
132
148
133
149
// ListSchemasAndTables lists related tables and their corresponding schemas from predicate.
@@ -137,7 +153,6 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
137
153
is infoschema.InfoSchema ,
138
154
) ([]pmodel.CIStr , []* model.TableInfo , error ) {
139
155
ec := e .extractableColumns
140
- schemas := e .ListSchemas (is )
141
156
var tableNames []pmodel.CIStr
142
157
if ec .table != "" {
143
158
tableNames = e .getSchemaObjectNames (ec .table )
@@ -150,7 +165,7 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
150
165
findTablesByID (is , tableIDs , tableNames , tableMap )
151
166
tableSlice := maps .Values (tableMap )
152
167
tableSlice = filterSchemaObjectByRegexp (e , ec .table , tableSlice , extractStrTableInfo )
153
- return findSchemasForTables (ctx , is , schemas , tableSlice )
168
+ return findSchemasForTables (e , is , tableSlice )
154
169
}
155
170
}
156
171
if ec .partitionID != "" {
@@ -160,13 +175,15 @@ func (e *InfoSchemaBaseExtractor) ListSchemasAndTables(
160
175
findTablesByPartID (is , partIDs , tableNames , tableMap )
161
176
tableSlice := maps .Values (tableMap )
162
177
tableSlice = filterSchemaObjectByRegexp (e , ec .table , tableSlice , extractStrTableInfo )
163
- return findSchemasForTables (ctx , is , schemas , tableSlice )
178
+ return findSchemasForTables (e , is , tableSlice )
164
179
}
165
180
}
166
181
if len (tableNames ) > 0 {
167
182
tableNames = filterSchemaObjectByRegexp (e , ec .table , tableNames , extractStrCIStr )
183
+ schemas := e .ListSchemas (is )
168
184
return findTableAndSchemaByName (ctx , is , schemas , tableNames )
169
185
}
186
+ schemas := e .ListSchemas (is )
170
187
return listTablesForEachSchema (ctx , e , is , schemas )
171
188
}
172
189
@@ -723,23 +740,28 @@ func listTablesForEachSchema(
723
740
// returns a schema slice and a table slice that has the same length.
724
741
// Note that input arg "tableSlice" will be changed in place.
725
742
func findSchemasForTables (
726
- ctx context. Context ,
743
+ e * InfoSchemaBaseExtractor ,
727
744
is infoschema.InfoSchema ,
728
- schemas []pmodel.CIStr ,
729
745
tableSlice []* model.TableInfo ,
730
746
) ([]pmodel.CIStr , []* model.TableInfo , error ) {
747
+ schemas , unspecified := e .listPredicateSchemas (is )
731
748
schemaSlice := make ([]pmodel.CIStr , 0 , len (tableSlice ))
732
749
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
+
733
762
found := false
734
763
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 {
743
765
schemaSlice = append (schemaSlice , s )
744
766
found = true
745
767
break
0 commit comments