@@ -18,7 +18,9 @@ package vschemawrapper
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
23
+ "sort"
22
24
"strings"
23
25
24
26
"vitess.io/vitess/go/mysql/collations"
@@ -275,17 +277,6 @@ func (vw *VSchemaWrapper) FindTableOrVindex(tab sqlparser.TableName) (*vindexes.
275
277
return vw .Vcursor .FindTableOrVindex (tab )
276
278
}
277
279
278
- func (vw * VSchemaWrapper ) getfirstKeyspace () (ks * vindexes.Keyspace ) {
279
- var f string
280
- for name , schema := range vw .V .Keyspaces {
281
- if f == "" || f > name {
282
- f = name
283
- ks = schema .Keyspace
284
- }
285
- }
286
- return
287
- }
288
-
289
280
func (vw * VSchemaWrapper ) getActualKeyspace () string {
290
281
if vw .Keyspace == nil {
291
282
return ""
@@ -301,15 +292,32 @@ func (vw *VSchemaWrapper) getActualKeyspace() string {
301
292
}
302
293
303
294
func (vw * VSchemaWrapper ) SelectedKeyspace () (* vindexes.Keyspace , error ) {
304
- return vw .V . Keyspaces [ "main" ]. Keyspace , nil
295
+ return vw .AnyKeyspace ()
305
296
}
306
297
307
298
func (vw * VSchemaWrapper ) AnyKeyspace () (* vindexes.Keyspace , error ) {
308
- return vw .SelectedKeyspace ()
299
+ ks , found := vw .V .Keyspaces ["main" ]
300
+ if found {
301
+ return ks .Keyspace , nil
302
+ }
303
+
304
+ size := len (vw .V .Keyspaces )
305
+ if size == 0 {
306
+ return nil , errors .New ("no keyspace found in vschema" )
307
+ }
308
+
309
+ // Find the first keyspace in the map alphabetically to get deterministic results
310
+ keys := make ([]string , size )
311
+ for key := range vw .V .Keyspaces {
312
+ keys = append (keys , key )
313
+ }
314
+ sort .Strings (keys )
315
+
316
+ return vw .V .Keyspaces [keys [0 ]].Keyspace , nil
309
317
}
310
318
311
319
func (vw * VSchemaWrapper ) FirstSortedKeyspace () (* vindexes.Keyspace , error ) {
312
- return vw .V . Keyspaces [ "main" ]. Keyspace , nil
320
+ return vw .AnyKeyspace ()
313
321
}
314
322
315
323
func (vw * VSchemaWrapper ) TargetString () string {
@@ -344,12 +352,12 @@ func (vw *VSchemaWrapper) IsViewsEnabled() bool {
344
352
345
353
// FindMirrorRule finds the mirror rule for the requested keyspace, table
346
354
// name, and the tablet type in the VSchema.
347
- func (vs * VSchemaWrapper ) FindMirrorRule (tab sqlparser.TableName ) (* vindexes.MirrorRule , error ) {
355
+ func (vw * VSchemaWrapper ) FindMirrorRule (tab sqlparser.TableName ) (* vindexes.MirrorRule , error ) {
348
356
destKeyspace , destTabletType , _ , err := topoproto .ParseDestination (tab .Qualifier .String (), topodatapb .TabletType_PRIMARY )
349
357
if err != nil {
350
358
return nil , err
351
359
}
352
- mirrorRule , err := vs .V .FindMirrorRule (destKeyspace , tab .Name .String (), destTabletType )
360
+ mirrorRule , err := vw .V .FindMirrorRule (destKeyspace , tab .Name .String (), destTabletType )
353
361
if err != nil {
354
362
return nil , err
355
363
}
0 commit comments