-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vschema.go
1404 lines (1290 loc) · 40.3 KB
/
vschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vindexes
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"time"
"vitess.io/vitess/go/json2"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqlescape"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)
// TabletTypeSuffix maps the tablet type to its suffix string.
var TabletTypeSuffix = map[topodatapb.TabletType]string{
0: "@unknown",
1: "@primary",
2: "@replica",
3: "@rdonly",
4: "@spare",
5: "@experimental",
6: "@backup",
7: "@restore",
8: "@drained",
}
// The following constants represent table types.
const (
TypeTable = ""
TypeSequence = "sequence"
TypeReference = "reference"
)
// VSchema represents the denormalized version of SrvVSchema,
// used for building routing plans.
type VSchema struct {
RoutingRules map[string]*RoutingRule `json:"routing_rules"`
// globalTables contains the name of all tables in all keyspaces. If the
// table is uniquely named, the value will be the qualified Table object
// with the keyspace where this table exists. If multiple keyspaces have a
// table with the same name, the value will be a `nil`.
globalTables map[string]*Table
uniqueVindexes map[string]Vindex
Keyspaces map[string]*KeyspaceSchema `json:"keyspaces"`
ShardRoutingRules map[string]string `json:"shard_routing_rules"`
// created is the time when the VSchema object was created. Used to detect if a cached
// copy of the vschema is stale.
created time.Time
}
// RoutingRule represents one routing rule.
type RoutingRule struct {
Tables []*Table
Error error
}
// MarshalJSON returns a JSON representation of Column.
func (rr *RoutingRule) MarshalJSON() ([]byte, error) {
if rr.Error != nil {
return json.Marshal(rr.Error.Error())
}
tables := make([]string, 0, len(rr.Tables))
for _, t := range rr.Tables {
tables = append(tables, t.String())
}
return json.Marshal(tables)
}
// Table represents a table in VSchema.
type Table struct {
Type string `json:"type,omitempty"`
Name sqlparser.IdentifierCS `json:"name"`
Keyspace *Keyspace `json:"-"`
ColumnVindexes []*ColumnVindex `json:"column_vindexes,omitempty"`
Ordered []*ColumnVindex `json:"ordered,omitempty"`
Owned []*ColumnVindex `json:"owned,omitempty"`
AutoIncrement *AutoIncrement `json:"auto_increment,omitempty"`
Columns []Column `json:"columns,omitempty"`
Pinned []byte `json:"pinned,omitempty"`
ColumnListAuthoritative bool `json:"column_list_authoritative,omitempty"`
// ReferencedBy is an inverse mapping of tables in other keyspaces that
// reference this table via Source.
//
// This is useful in route-planning for quickly selecting the optimal route
// when JOIN-ing a reference table to a sharded table.
ReferencedBy map[string]*Table `json:"-"`
// Source is a keyspace-qualified table name that points to the source of a
// reference table. Only applicable for tables with Type set to "reference".
Source *Source `json:"source,omitempty"`
ChildForeignKeys []ChildFKInfo `json:"child_foreign_keys,omitempty"`
ParentForeignKeys []ParentFKInfo `json:"parent_foreign_keys,omitempty"`
// index can be columns or expression.
// For Primary key, functional indexes are not allowed, therefore it will only be columns.
// MySQL error message: ERROR 3756 (HY000): The primary key cannot be a functional index
PrimaryKey sqlparser.Columns `json:"primary_key,omitempty"`
UniqueKeys []sqlparser.Exprs `json:"unique_keys,omitempty"`
}
// GetTableName gets the sqlparser.TableName for the vindex Table.
func (t *Table) GetTableName() sqlparser.TableName {
return sqlparser.NewTableNameWithQualifier(t.Name.String(), t.Keyspace.Name)
}
// Keyspace contains the keyspcae info for each Table.
type Keyspace struct {
Name string
Sharded bool
}
// ColumnVindex contains the index info for each index of a table.
type ColumnVindex struct {
Columns []sqlparser.IdentifierCI `json:"columns"`
Type string `json:"type"`
Name string `json:"name"`
Owned bool `json:"owned,omitempty"`
Vindex Vindex `json:"vindex"`
isUnique bool
cost int
partial bool
backfill bool
}
// TableInfo contains column and foreign key info for a table.
type TableInfo struct {
Columns []Column
ForeignKeys []*sqlparser.ForeignKeyDefinition
Indexes []*sqlparser.IndexDefinition
}
// IsUnique is used to tell whether the ColumnVindex
// will return a unique shard value or not when queried with
// the given column list
func (c *ColumnVindex) IsUnique() bool {
return c.isUnique
}
// Cost represents the cost associated with using the
// ColumnVindex
func (c *ColumnVindex) Cost() int {
return c.cost
}
// IsPartialVindex is used to let planner and engine know that this is a composite vindex missing one or more columns
func (c *ColumnVindex) IsPartialVindex() bool {
return c.partial
}
// IsBackfilling returns true if the vindex is in the process of backfilling the rows.
func (c *ColumnVindex) IsBackfilling() bool {
return c.backfill
}
// Column describes a column.
type Column struct {
Name sqlparser.IdentifierCI `json:"name"`
Type querypb.Type `json:"type"`
CollationName string `json:"collation_name"`
Default sqlparser.Expr `json:"default,omitempty"`
// Invisible marks this as a column that will not be automatically included in `*` projections
Invisible bool `json:"invisible,omitempty"`
Size int32 `json:"size,omitempty"`
Scale int32 `json:"scale,omitempty"`
Nullable bool `json:"nullable,omitempty"`
// Values contains the list of values for enum and set types.
Values []string `json:"values,omitempty"`
}
// MarshalJSON returns a JSON representation of Column.
func (col *Column) MarshalJSON() ([]byte, error) {
cj := struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Invisible bool `json:"invisible,omitempty"`
Default string `json:"default,omitempty"`
}{
Name: col.Name.String(),
Type: querypb.Type_name[int32(col.Type)],
}
if col.Invisible {
cj.Invisible = true
}
if col.Default != nil {
cj.Default = sqlparser.String(col.Default)
}
return json.Marshal(cj)
}
func (col *Column) ToEvalengineType(collationEnv *collations.Environment) evalengine.Type {
var collation collations.ID
if sqltypes.IsText(col.Type) {
collation, _ = collationEnv.LookupID(col.CollationName)
} else {
collation = collations.CollationForType(col.Type, collationEnv.DefaultConnectionCharset())
}
return evalengine.NewTypeEx(col.Type, collation, col.Nullable, col.Size, col.Scale)
}
// KeyspaceSchema contains the schema(table) for a keyspace.
type KeyspaceSchema struct {
Keyspace *Keyspace
ForeignKeyMode vschemapb.Keyspace_ForeignKeyMode
Tables map[string]*Table
Vindexes map[string]Vindex
Views map[string]sqlparser.SelectStatement
Error error
}
type ksJSON struct {
Sharded bool `json:"sharded,omitempty"`
ForeignKeyMode string `json:"foreignKeyMode,omitempty"`
Tables map[string]*Table `json:"tables,omitempty"`
Vindexes map[string]Vindex `json:"vindexes,omitempty"`
Views map[string]string `json:"views,omitempty"`
Error string `json:"error,omitempty"`
}
// findTable looks for the table with the requested tablename in the keyspace.
//
// If no table matching the requested tablename is found, and:
//
// - constructUnshardedIfNotFound is not requested, than no table is returned.
// - constructUnshardedIfNotFound is requested, and the keyspace is
// unsharded, then a *Table representing that table is returned.
func (ks *KeyspaceSchema) findTable(
tablename string,
constructUnshardedIfNotFound bool,
) *Table {
table := ks.Tables[tablename]
if table != nil {
return table
}
if constructUnshardedIfNotFound && !ks.Keyspace.Sharded {
return &Table{Name: sqlparser.NewIdentifierCS(tablename), Keyspace: ks.Keyspace}
}
return nil
}
// MarshalJSON returns a JSON representation of KeyspaceSchema.
func (ks *KeyspaceSchema) MarshalJSON() ([]byte, error) {
ksJ := ksJSON{
Sharded: ks.Keyspace.Sharded,
Tables: ks.Tables,
ForeignKeyMode: ks.ForeignKeyMode.String(),
Vindexes: ks.Vindexes,
}
if ks.Error != nil {
ksJ.Error = ks.Error.Error()
}
if len(ks.Views) > 0 {
ksJ.Views = make(map[string]string, len(ks.Views))
}
for view, def := range ks.Views {
ksJ.Views[view] = sqlparser.String(def)
}
return json.Marshal(ksJ)
}
// AutoIncrement contains the auto-inc information for a table.
type AutoIncrement struct {
Column sqlparser.IdentifierCI `json:"column"`
Sequence *Table `json:"sequence"`
}
type Source struct {
sqlparser.TableName
}
func (source *Source) String() string {
buf := sqlparser.NewTrackedBuffer(nil)
source.Format(buf)
return buf.String()
}
// BuildVSchema builds a VSchema from a SrvVSchema.
func BuildVSchema(source *vschemapb.SrvVSchema, parser *sqlparser.Parser) (vschema *VSchema) {
vschema = &VSchema{
RoutingRules: make(map[string]*RoutingRule),
globalTables: make(map[string]*Table),
uniqueVindexes: make(map[string]Vindex),
Keyspaces: make(map[string]*KeyspaceSchema),
created: time.Now(),
}
buildKeyspaces(source, vschema, parser)
// buildGlobalTables before buildReferences so that buildReferences can
// resolve sources which reference global tables.
buildGlobalTables(source, vschema)
buildReferences(source, vschema)
buildRoutingRule(source, vschema, parser)
buildShardRoutingRule(source, vschema)
// Resolve auto-increments after routing rules are built since sequence tables also obey routing rules.
resolveAutoIncrement(source, vschema, parser)
return vschema
}
// BuildKeyspaceSchema builds the vschema portion for one keyspace.
// The build ignores sequence references because those dependencies can
// go cross-keyspace.
func BuildKeyspaceSchema(input *vschemapb.Keyspace, keyspace string, parser *sqlparser.Parser) (*KeyspaceSchema, error) {
if input == nil {
input = &vschemapb.Keyspace{}
}
formal := &vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
keyspace: input,
},
}
vschema := &VSchema{
globalTables: make(map[string]*Table),
uniqueVindexes: make(map[string]Vindex),
Keyspaces: make(map[string]*KeyspaceSchema),
}
buildKeyspaces(formal, vschema, parser)
err := vschema.Keyspaces[keyspace].Error
return vschema.Keyspaces[keyspace], err
}
// BuildKeyspace ensures that the keyspace vschema is valid.
// External references (like sequence) are not validated.
func BuildKeyspace(input *vschemapb.Keyspace, parser *sqlparser.Parser) (*KeyspaceSchema, error) {
return BuildKeyspaceSchema(input, "", parser)
}
func buildKeyspaces(source *vschemapb.SrvVSchema, vschema *VSchema, parser *sqlparser.Parser) {
for ksname, ks := range source.Keyspaces {
ksvschema := &KeyspaceSchema{
Keyspace: &Keyspace{
Name: ksname,
Sharded: ks.Sharded,
},
ForeignKeyMode: replaceUnspecifiedForeignKeyMode(ks.ForeignKeyMode),
Tables: make(map[string]*Table),
Vindexes: make(map[string]Vindex),
}
vschema.Keyspaces[ksname] = ksvschema
ksvschema.Error = buildTables(ks, vschema, ksvschema, parser)
}
}
// replaceUnspecifiedForeignKeyMode replaces the default value of the foreign key mode enum with the default we want to keep.
func replaceUnspecifiedForeignKeyMode(fkMode vschemapb.Keyspace_ForeignKeyMode) vschemapb.Keyspace_ForeignKeyMode {
if fkMode == vschemapb.Keyspace_unspecified {
return vschemapb.Keyspace_unmanaged
}
return fkMode
}
func (vschema *VSchema) AddView(ksname, viewName, query string, parser *sqlparser.Parser) error {
ks, ok := vschema.Keyspaces[ksname]
if !ok {
return fmt.Errorf("keyspace %s not found in vschema", ksname)
}
ast, err := parser.Parse(query)
if err != nil {
return err
}
selectStmt, ok := ast.(sqlparser.SelectStatement)
if !ok {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "expected SELECT or UNION query, got %T", ast)
}
if ks.Views == nil {
ks.Views = make(map[string]sqlparser.SelectStatement)
}
ks.Views[viewName] = selectStmt
t := &Table{
Type: "View",
Name: sqlparser.NewIdentifierCS(viewName),
Keyspace: ks.Keyspace,
ColumnListAuthoritative: true,
}
vschema.addTableName(t)
return nil
}
func buildGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) {
for ksname, ks := range source.Keyspaces {
ksvschema := vschema.Keyspaces[ksname]
// If the keyspace requires explicit routing, don't include any of
// its tables in global tables.
if ks.RequireExplicitRouting {
continue
}
buildKeyspaceGlobalTables(vschema, ksvschema)
}
}
func buildKeyspaceGlobalTables(vschema *VSchema, ksvschema *KeyspaceSchema) {
for tname, t := range ksvschema.Tables {
if gt, ok := vschema.globalTables[tname]; ok {
// There is already an entry table stored in global tables
// with this name.
if gt == nil {
// Table name is already marked ambiguous, nothing to do.
continue
} else {
// Special handling for reference tables which specify their source.
if t.Type == TypeReference && t.Source != nil {
// If the reference table points to the already stored
// global table, there is no ambiguity.
if t.Source.Qualifier.IsEmpty() || t.Source.Qualifier.String() == gt.Keyspace.Name {
continue
}
}
// Otherwise, mark this table name ambiguous.
vschema.globalTables[tname] = nil
}
} else {
// Reference tables which define a source with the same name may be
// globally routable through their source, as long as the source
// doesn't require explicit routing.
if t.Type == TypeReference && t.Source != nil && t.Source.Name.String() == t.Name.String() {
continue
}
vschema.globalTables[tname] = t
}
}
}
func buildReferences(source *vschemapb.SrvVSchema, vschema *VSchema) {
for ksname := range source.Keyspaces {
ksvschema := vschema.Keyspaces[ksname]
if err := buildKeyspaceReferences(vschema, ksvschema); err != nil && ksvschema.Error == nil {
ksvschema.Error = err
}
}
}
func buildKeyspaceReferences(vschema *VSchema, ksvschema *KeyspaceSchema) error {
keyspace := ksvschema.Keyspace
for tname, t := range ksvschema.Tables {
source := t.Source
if t.Type != TypeReference || source == nil {
continue
}
sourceKsname := source.Qualifier.String()
sourceTname := source.Name.String()
// Prohibit self-references.
if sourceKsname == keyspace.Name {
return vterrors.Errorf(
vtrpcpb.Code_UNIMPLEMENTED,
"source %q may not reference a table in the same keyspace as table: %s",
source,
tname,
)
}
// Verify the reference source can be resolved.
sourceT, err := vschema.findTable(
sourceKsname,
sourceTname,
false, /* constructTableIfNotFound */
)
// Rephrase errors to be more helpful in the context of VSchema linting.
if err != nil {
if vterrors.Code(err) != vtrpcpb.Code_NOT_FOUND || vterrors.ErrState(err) != vterrors.BadDb {
return err
}
return vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"source %q references a non-existent keyspace %q",
source,
sourceKsname,
)
}
if sourceT == nil {
return vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"source %q references a table %q that is not present in the VSchema of keyspace %q",
source,
sourceTname,
sourceKsname,
)
}
// Validate source table types.
if !(sourceT.Type == "" || sourceT.Type == TypeReference) {
return vterrors.Errorf(
vtrpcpb.Code_UNIMPLEMENTED,
"source %q may not reference a table of type %q: %s",
source,
sourceT.Type,
tname,
)
}
// Update inverse reference table map.
if ot := sourceT.getReferenceInKeyspace(keyspace.Name); ot != nil {
names := []string{ot.Name.String(), tname}
sort.Strings(names)
return vterrors.Errorf(
vtrpcpb.Code_UNIMPLEMENTED,
"source %q may not be referenced more than once per keyspace: %s, %s",
source,
names[0],
names[1],
)
}
sourceT.addReferenceInKeyspace(keyspace.Name, t)
// Forbid reference chains. This is not necessarily a technical
// limitation. If people want this, in theory it should be possible as
// long as reference chains are not circular.
if sourceT.Source != nil {
chain := fmt.Sprintf("%s => %s => %s", tname, sourceT, sourceT.Source)
return vterrors.Errorf(
vtrpcpb.Code_UNIMPLEMENTED,
"reference chaining is not allowed %s: %s",
chain,
tname,
)
}
}
return nil
}
func buildTables(ks *vschemapb.Keyspace, vschema *VSchema, ksvschema *KeyspaceSchema, parser *sqlparser.Parser) error {
keyspace := ksvschema.Keyspace
for vname, vindexInfo := range ks.Vindexes {
vindex, err := CreateVindex(vindexInfo.Type, vname, vindexInfo.Params)
if err != nil {
return err
}
// If the keyspace requires explicit routing, don't include its indexes
// in global routing.
if !ks.RequireExplicitRouting {
if _, ok := vschema.uniqueVindexes[vname]; ok {
vschema.uniqueVindexes[vname] = nil
} else {
vschema.uniqueVindexes[vname] = vindex
}
}
ksvschema.Vindexes[vname] = vindex
}
for tname, table := range ks.Tables {
t := &Table{
Name: sqlparser.NewIdentifierCS(tname),
Keyspace: keyspace,
ColumnListAuthoritative: table.ColumnListAuthoritative,
}
switch table.Type {
case "":
t.Type = table.Type
case TypeReference:
if table.Source != "" {
tableName, err := parseTable(table.Source)
if err != nil {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"invalid source %q for reference table: %s; %v",
table.Source,
tname,
err,
)
}
t.Source = &Source{TableName: tableName}
}
t.Type = table.Type
case TypeSequence:
if keyspace.Sharded && table.Pinned == "" {
return vterrors.Errorf(
vtrpcpb.Code_FAILED_PRECONDITION,
"sequence table has to be in an unsharded keyspace or must be pinned: %s",
tname,
)
}
t.Type = table.Type
default:
return vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"unidentified table type %s",
table.Type,
)
}
if table.Pinned != "" {
decoded, err := hex.DecodeString(table.Pinned)
if err != nil {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"could not decode the keyspace id for pin: %v",
err,
)
}
t.Pinned = decoded
}
// If keyspace is sharded, then any table that's not a reference or pinned must have vindexes.
if keyspace.Sharded && t.Type != TypeReference && table.Pinned == "" && len(table.ColumnVindexes) == 0 {
return vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"missing primary col vindex for table: %s",
tname,
)
}
// Initialize Columns.
colNames := make(map[string]bool)
for _, col := range table.Columns {
name := sqlparser.NewIdentifierCI(col.Name)
if colNames[name.Lowered()] {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"duplicate column name '%v' for table: %s",
name,
tname,
)
}
var colDefault sqlparser.Expr
if col.Default != "" {
var err error
colDefault, err = parser.ParseExpr(col.Default)
if err != nil {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT,
"could not parse the '%s' column's default expression '%s' for table '%s'", col.Name, col.Default, tname)
}
}
nullable := true
if col.Nullable != nil {
nullable = *col.Nullable
}
colNames[name.Lowered()] = true
t.Columns = append(t.Columns, Column{
Name: name,
Type: col.Type,
CollationName: col.CollationName,
Default: colDefault,
Invisible: col.Invisible,
Size: col.Size,
Scale: col.Scale,
Nullable: nullable,
Values: col.Values,
})
}
// Initialize ColumnVindexes.
for i, ind := range table.ColumnVindexes {
vindexInfo, ok := ks.Vindexes[ind.Name]
if !ok {
return vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"vindex %s not found for table %s",
ind.Name,
tname,
)
}
vindex := ksvschema.Vindexes[ind.Name]
owned := false
if _, ok := vindex.(Lookup); ok && vindexInfo.Owner == tname {
owned = true
}
var columns []sqlparser.IdentifierCI
if ind.Column != "" {
if len(ind.Columns) > 0 {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"can't use column and columns at the same time in vindex (%s) and table (%s)",
ind.Name,
tname,
)
}
columns = []sqlparser.IdentifierCI{sqlparser.NewIdentifierCI(ind.Column)}
} else {
if len(ind.Columns) == 0 {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"must specify at least one column for vindex (%s) and table (%s)",
ind.Name,
tname,
)
}
for _, indCol := range ind.Columns {
columns = append(columns, sqlparser.NewIdentifierCI(indCol))
}
}
backfill := false
if lkpBackfill, ok := vindex.(LookupBackfill); ok {
backfill = lkpBackfill.IsBackfilling()
}
columnVindex := &ColumnVindex{
Columns: columns,
Type: vindexInfo.Type,
Name: ind.Name,
Owned: owned,
Vindex: vindex,
isUnique: vindex.IsUnique(),
cost: vindex.Cost(),
backfill: backfill,
}
if i == 0 {
// Perform Primary vindex check.
if !columnVindex.Vindex.IsUnique() {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"primary vindex %s is not Unique for table %s",
ind.Name,
tname,
)
}
if owned {
return vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"primary vindex %s cannot be owned for table %s",
ind.Name,
tname,
)
}
}
t.ColumnVindexes = append(t.ColumnVindexes, columnVindex)
if owned {
if setter, ok := vindex.(WantOwnerInfo); ok {
if err := setter.SetOwnerInfo(keyspace.Name, tname, columns); err != nil {
return err
}
}
t.Owned = append(t.Owned, columnVindex)
}
mcv, isMultiColumn := vindex.(MultiColumn)
if !isMultiColumn {
continue
}
if i != 0 {
return vterrors.Errorf(
vtrpcpb.Code_UNIMPLEMENTED,
"multi-column vindex %s should be a primary vindex for table %s",
ind.Name,
tname,
)
}
if !mcv.PartialVindex() {
// Partial column selection not allowed.
// Do not create subset column vindex.
continue
}
cost := vindex.Cost()
for i := len(columns) - 1; i > 0; i-- {
columnSubset := columns[:i]
cost++
columnVindex = &ColumnVindex{
Columns: columnSubset,
Type: vindexInfo.Type,
Name: ind.Name,
Owned: owned,
Vindex: vindex,
cost: cost,
partial: true,
backfill: backfill,
}
t.ColumnVindexes = append(t.ColumnVindexes, columnVindex)
}
}
t.Ordered = colVindexSorted(t.ColumnVindexes)
// Add the table to the map entries.
ksvschema.Tables[tname] = t
}
return nil
}
func (vschema *VSchema) addTableName(t *Table) {
tname := t.Name.String()
if _, ok := vschema.globalTables[tname]; ok {
vschema.globalTables[tname] = nil
} else {
vschema.globalTables[tname] = t
}
}
func resolveAutoIncrement(source *vschemapb.SrvVSchema, vschema *VSchema, parser *sqlparser.Parser) {
for ksname, ks := range source.Keyspaces {
ksvschema := vschema.Keyspaces[ksname]
for tname, table := range ks.Tables {
t := ksvschema.Tables[tname]
if t == nil || table.AutoIncrement == nil {
continue
}
seqks, seqtab, err := parser.ParseTable(table.AutoIncrement.Sequence)
var seq *Table
if err == nil {
// Ensure that sequence tables also obey routing rules.
seq, err = vschema.FindRoutedTable(seqks, seqtab, topodatapb.TabletType_PRIMARY)
if seq == nil && err == nil {
err = vterrors.Errorf(vtrpcpb.Code_NOT_FOUND, "table %s not found", seqtab)
}
}
if err != nil {
// Better to remove the table than to leave it partially initialized.
delete(ksvschema.Tables, tname)
delete(vschema.globalTables, tname)
ksvschema.Error = vterrors.Errorf(
vtrpcpb.Code_NOT_FOUND,
"cannot resolve sequence %s: %s",
table.AutoIncrement.Sequence,
err.Error(),
)
continue
}
t.AutoIncrement = &AutoIncrement{
Column: sqlparser.NewIdentifierCI(table.AutoIncrement.Column),
Sequence: seq,
}
}
}
}
// expects table name of the form <keyspace>.<tablename>
func escapeQualifiedTable(qualifiedTableName string) (string, error) {
keyspace, tableName, err := extractTableParts(qualifiedTableName, false /* allowUnqualified */)
if err != nil {
return "", err
}
// unescape() first in case an already escaped string was passed
keyspace, err = sqlescape.EnsureEscaped(keyspace)
if err != nil {
return "", err
}
tableName, err = sqlescape.EnsureEscaped(tableName)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s", keyspace, tableName), nil
}
func extractTableParts(tableName string, allowUnqualified bool) (string, string, error) {
errMsgFormat := "invalid table name: %s, it must be of the "
if allowUnqualified {
errMsgFormat = errMsgFormat + "unqualified form <table_name> or the "
}
errMsgFormat = errMsgFormat + "qualified form <keyspace_name>.<table_name> (dots are not allowed in either name)"
// It's possible to have a database or table name with a dot in it, but
// that's not otherwise supported within vitess today
arr := strings.Split(tableName, ".")
switch len(arr) {
case 1:
if allowUnqualified {
return "", arr[0], nil
}
case 2:
return arr[0], arr[1], nil
}
// Using fmt.Errorf instead of vterrors here because this error is always wrapped in vterrors.
return "", "", fmt.Errorf(errMsgFormat, tableName)
}
func parseTable(tableName string) (sqlparser.TableName, error) {
keyspace, tableName, err := extractTableParts(tableName, true /* allowUnqualified */)
if err != nil {
return sqlparser.TableName{}, err
}
return sqlparser.TableName{
Qualifier: sqlparser.NewIdentifierCS(keyspace),
Name: sqlparser.NewIdentifierCS(tableName),
}, nil
}
func buildRoutingRule(source *vschemapb.SrvVSchema, vschema *VSchema, parser *sqlparser.Parser) {
var err error
if source.RoutingRules == nil {
return
}
outer:
for _, rule := range source.RoutingRules.Rules {
rr := &RoutingRule{}
if len(rule.ToTables) > 1 {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"table %v has more than one target: %v",
rule.FromTable,
rule.ToTables,
),
}
continue
}
for _, toTable := range rule.ToTables {
if _, ok := vschema.RoutingRules[rule.FromTable]; ok {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: vterrors.Errorf(
vtrpcpb.Code_ALREADY_EXISTS,
"duplicate rule for entry %s",
rule.FromTable,
),
}
continue outer
}
// we need to backtick the keyspace and table name before calling ParseTable
toTable, err = escapeQualifiedTable(toTable)
if err != nil {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
err.Error(),
),
}
continue outer
}
toKeyspace, toTableName, err := parser.ParseTable(toTable)
if err != nil {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: err,
}
continue outer
}
if toKeyspace == "" {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: vterrors.Errorf(
vtrpcpb.Code_INVALID_ARGUMENT,
"table %s must be qualified",
toTable,
),
}
continue outer
}
t, err := vschema.FindTable(toKeyspace, toTableName)
if err != nil {
vschema.RoutingRules[rule.FromTable] = &RoutingRule{
Error: err,
}
continue outer
}
rr.Tables = append(rr.Tables, t)
}
vschema.RoutingRules[rule.FromTable] = rr
}
}
func buildShardRoutingRule(source *vschemapb.SrvVSchema, vschema *VSchema) {
if source.ShardRoutingRules == nil || len(source.ShardRoutingRules.Rules) == 0 {
return
}
vschema.ShardRoutingRules = make(map[string]string)
for _, rule := range source.ShardRoutingRules.Rules {
vschema.ShardRoutingRules[getShardRoutingRulesKey(rule.FromKeyspace, rule.Shard)] = rule.ToKeyspace
}
}
// FindTable returns a pointer to the Table. If a keyspace is specified, only tables
// from that keyspace are searched. If the specified keyspace is unsharded
// and no tables matched, it's considered valid: FindTable will construct a table
// of that name and return it. If no keyspace is specified, then a table is returned
// only if its name is unique across all keyspaces. If there is only one
// keyspace in the vschema, and it's unsharded, then all table requests are considered
// valid and belonging to that keyspace.
// FindTable bypasses routing rules and returns at most one table.
func (vschema *VSchema) FindTable(keyspace, tablename string) (*Table, error) {
table, err := vschema.findTable(
keyspace,
tablename,
true, /* constructUnshardedIfNotFound */
)
if table != nil || err != nil {
return table, err