-
Notifications
You must be signed in to change notification settings - Fork 4
/
insert.go
632 lines (570 loc) · 22.1 KB
/
insert.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
/*
Copyright 2018 Google Inc.
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 engine
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"gopkg.in/src-d/go-vitess.v1/jsonutil"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/key"
"gopkg.in/src-d/go-vitess.v1/vt/sqlannotation"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
"gopkg.in/src-d/go-vitess.v1/vt/srvtopo"
"gopkg.in/src-d/go-vitess.v1/vt/vterrors"
"gopkg.in/src-d/go-vitess.v1/vt/vtgate/vindexes"
querypb "gopkg.in/src-d/go-vitess.v1/vt/proto/query"
vtrpcpb "gopkg.in/src-d/go-vitess.v1/vt/proto/vtrpc"
)
var _ Primitive = (*Insert)(nil)
// Insert represents the instructions to perform an insert operation.
type Insert struct {
// Opcode is the execution opcode.
Opcode InsertOpcode
// Keyspace specifies the keyspace to send the query to.
Keyspace *vindexes.Keyspace
// Query specifies the query to be executed.
// For InsertSharded plans, this value is unused,
// and Prefix, Mid and Suffix are used instead.
Query string
// VindexValues specifies values for all the vindex columns.
// This is a three-dimensonal data structure:
// Insert.Values[i] represents the values to be inserted for the i'th colvindex (i < len(Insert.Table.ColumnVindexes))
// Insert.Values[i].Values[j] represents values for the j'th column of the given colVindex (j < len(colVindex[i].Columns)
// Insert.Values[i].Values[j].Values[k] represents the value pulled from row k for that column: (k < len(ins.rows))
VindexValues []sqltypes.PlanValue
// Table sepcifies the table for the insert.
Table *vindexes.Table
// Generate is only set for inserts where a sequence must be generated.
Generate *Generate
// Prefix, Mid and Suffix are for sharded insert plans.
Prefix string
Mid []string
Suffix string
// Option to override the standard behavior and allow a multi-shard insert
// to use single round trip autocommit.
//
// This is a clear violation of the SQL semantics since it means the statement
// is not atomic in the presence of PK conflicts on one shard and not another.
// However some application use cases would prefer that the statement partially
// succeed in order to get the performance benefits of autocommit.
MultiShardAutocommit bool
// QueryTimeout contains the optional timeout (in milliseconds) to apply to this query
QueryTimeout int
}
// NewQueryInsert creates an Insert with a query string.
func NewQueryInsert(opcode InsertOpcode, keyspace *vindexes.Keyspace, query string) *Insert {
return &Insert{
Opcode: opcode,
Keyspace: keyspace,
Query: query,
}
}
// NewSimpleInsert creates an Insert for a Table.
func NewSimpleInsert(opcode InsertOpcode, table *vindexes.Table, keyspace *vindexes.Keyspace) *Insert {
return &Insert{
Opcode: opcode,
Table: table,
Keyspace: keyspace,
}
}
// NewInsert creates a new Insert.
func NewInsert(opcode InsertOpcode, keyspace *vindexes.Keyspace, vindexValues []sqltypes.PlanValue, table *vindexes.Table, prefix string, mid []string, suffix string) *Insert {
return &Insert{
Opcode: opcode,
Keyspace: keyspace,
VindexValues: vindexValues,
Table: table,
Prefix: prefix,
Mid: mid,
Suffix: suffix,
}
}
// MarshalJSON serializes the Insert into a JSON representation.
// It's used for testing and diagnostics.
func (ins *Insert) MarshalJSON() ([]byte, error) {
var tname string
if ins.Table != nil {
tname = ins.Table.Name.String()
}
marshalInsert := struct {
Opcode InsertOpcode
Keyspace *vindexes.Keyspace `json:",omitempty"`
Query string `json:",omitempty"`
Values []sqltypes.PlanValue `json:",omitempty"`
Table string `json:",omitempty"`
Generate *Generate `json:",omitempty"`
Prefix string `json:",omitempty"`
Mid []string `json:",omitempty"`
Suffix string `json:",omitempty"`
MultiShardAutocommit bool `json:",omitempty"`
QueryTimeout int `json:",omitempty"`
}{
Opcode: ins.Opcode,
Keyspace: ins.Keyspace,
Query: ins.Query,
Values: ins.VindexValues,
Table: tname,
Generate: ins.Generate,
Prefix: ins.Prefix,
Mid: ins.Mid,
Suffix: ins.Suffix,
MultiShardAutocommit: ins.MultiShardAutocommit,
QueryTimeout: ins.QueryTimeout,
}
return jsonutil.MarshalNoEscape(marshalInsert)
}
// Generate represents the instruction to generate
// a value from a sequence.
type Generate struct {
Keyspace *vindexes.Keyspace
Query string
// Values are the supplied values for the column, which
// will be stored as a list within the PlanValue. New
// values will be generated based on how many were not
// supplied (NULL).
Values sqltypes.PlanValue
}
// InsertOpcode is a number representing the opcode
// for the Insert primitve.
type InsertOpcode int
const (
// InsertUnsharded is for routing an insert statement
// to an unsharded keyspace.
InsertUnsharded = InsertOpcode(iota)
// InsertSharded is for routing an insert statement
// to individual shards. Requires: A list of Values, one
// for each ColVindex. If the table has an Autoinc column,
// A Generate subplan must be created.
InsertSharded
// InsertShardedIgnore is for INSERT IGNORE and
// INSERT...ON DUPLICATE KEY constructs.
InsertShardedIgnore
)
var insName = map[InsertOpcode]string{
InsertUnsharded: "InsertUnsharded",
InsertSharded: "InsertSharded",
InsertShardedIgnore: "InsertShardedIgnore",
}
// MarshalJSON serializes the InsertOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code InsertOpcode) MarshalJSON() ([]byte, error) {
return json.Marshal(insName[code])
}
// RouteType returns a description of the query routing type used by the primitive
func (ins *Insert) RouteType() string {
return insName[ins.Opcode]
}
// Execute performs a non-streaming exec.
func (ins *Insert) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
if ins.QueryTimeout != 0 {
cancel := vcursor.SetContextTimeout(time.Duration(ins.QueryTimeout) * time.Millisecond)
defer cancel()
}
switch ins.Opcode {
case InsertUnsharded:
return ins.execInsertUnsharded(vcursor, bindVars)
case InsertSharded, InsertShardedIgnore:
return ins.execInsertSharded(vcursor, bindVars)
default:
// Unreachable.
return nil, fmt.Errorf("unsupported query route: %v", ins)
}
}
// StreamExecute performs a streaming exec.
func (ins *Insert) StreamExecute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
return fmt.Errorf("query %q cannot be used for streaming", ins.Query)
}
// GetFields fetches the field info.
func (ins *Insert) GetFields(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: unreachable code for %q", ins.Query)
}
func (ins *Insert) execInsertUnsharded(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
insertID, err := ins.processGenerate(vcursor, bindVars)
if err != nil {
return nil, vterrors.Wrap(err, "execInsertUnsharded")
}
rss, _, err := vcursor.ResolveDestinations(ins.Keyspace.Name, nil, []key.Destination{key.DestinationAllShards{}})
if err != nil {
return nil, vterrors.Wrap(err, "execInsertUnsharded")
}
if len(rss) != 1 {
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "Keyspace does not have exactly one shard: %v", rss)
}
result, err := execShard(vcursor, ins.Query, bindVars, rss[0], true, true /* canAutocommit */)
if err != nil {
return nil, vterrors.Wrap(err, "execInsertUnsharded")
}
// If processGenerate generated new values, it supercedes
// any ids that MySQL might have generated. If both generated
// values, we don't return an error because this behavior
// is required to support migration.
if insertID != 0 {
result.InsertID = uint64(insertID)
}
return result, nil
}
func (ins *Insert) execInsertSharded(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
insertID, err := ins.processGenerate(vcursor, bindVars)
if err != nil {
return nil, vterrors.Wrap(err, "execInsertSharded")
}
rss, queries, err := ins.getInsertShardedRoute(vcursor, bindVars)
if err != nil {
return nil, vterrors.Wrap(err, "execInsertSharded")
}
autocommit := (len(rss) == 1 || ins.MultiShardAutocommit) && vcursor.AutocommitApproval()
result, errs := vcursor.ExecuteMultiShard(rss, queries, true /* isDML */, autocommit)
if errs != nil {
return nil, vterrors.Wrap(vterrors.Aggregate(errs), "execInsertSharded")
}
if insertID != 0 {
result.InsertID = uint64(insertID)
}
return result, nil
}
// processGenerate generates new values using a sequence if necessary.
// If no value was generated, it returns 0. Values are generated only
// for cases where none are supplied.
func (ins *Insert) processGenerate(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (insertID int64, err error) {
if ins.Generate == nil {
return 0, nil
}
// Scan input values to compute the number of values to generate, and
// keep track of where they should be filled.
resolved, err := ins.Generate.Values.ResolveList(bindVars)
if err != nil {
return 0, vterrors.Wrap(err, "processGenerate")
}
count := int64(0)
for _, val := range resolved {
if val.IsNull() {
count++
}
}
// If generation is needed, generate the requested number of values (as one call).
if count != 0 {
rss, _, err := vcursor.ResolveDestinations(ins.Generate.Keyspace.Name, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
return 0, vterrors.Wrap(err, "processGenerate")
}
if len(rss) != 1 {
return 0, vterrors.Wrapf(err, "processGenerate len(rss)=%v", len(rss))
}
bindVars := map[string]*querypb.BindVariable{"n": sqltypes.Int64BindVariable(count)}
qr, err := vcursor.ExecuteStandalone(ins.Generate.Query, bindVars, rss[0])
if err != nil {
return 0, err
}
// If no rows are returned, it's an internal error, and the code
// must panic, which will be caught and reported.
insertID, err = sqltypes.ToInt64(qr.Rows[0][0])
if err != nil {
return 0, err
}
}
// Fill the holes where no value was supplied.
cur := insertID
for i, v := range resolved {
if v.IsNull() {
bindVars[SeqVarName+strconv.Itoa(i)] = sqltypes.Int64BindVariable(cur)
cur++
} else {
bindVars[SeqVarName+strconv.Itoa(i)] = sqltypes.ValueBindVariable(v)
}
}
return insertID, nil
}
// getInsertShardedRoute performs all the vindex related work
// and returns a map of shard to queries.
// Using the primary vindex, it computes the target keyspace ids.
// For owned vindexes, it creates entries.
// For unowned vindexes with no input values, it reverse maps.
// For unowned vindexes with values, it validates.
// If it's an IGNORE or ON DUPLICATE key insert, it drops unroutable rows.
func (ins *Insert) getInsertShardedRoute(vcursor VCursor, bindVars map[string]*querypb.BindVariable) ([]*srvtopo.ResolvedShard, []*querypb.BoundQuery, error) {
// vindexRowsValues builds the values of all vindex columns.
// the 3-d structure indexes are colVindex, row, col. Note that
// ins.Values indexes are colVindex, col, row. So, the conversion
// involves a transpose.
// The reason we need to transpose is because all the Vindex APIs
// require inputs in that format.
vindexRowsValues := make([][][]sqltypes.Value, len(ins.VindexValues))
rowCount := 0
for vIdx, vColValues := range ins.VindexValues {
if len(vColValues.Values) != len(ins.Table.ColumnVindexes[vIdx].Columns) {
return nil, nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: supplied vindex column values don't match vschema: %v %v", vColValues, ins.Table.ColumnVindexes[vIdx].Columns)
}
for colIdx, colValues := range vColValues.Values {
rowsResolvedValues, err := colValues.ResolveList(bindVars)
if err != nil {
return nil, nil, vterrors.Wrap(err, "getInsertShardedRoute")
}
// This is the first iteration: allocate for transpose.
if colIdx == 0 {
if len(rowsResolvedValues) == 0 {
return nil, nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: rowcount is zero for inserts: %v", rowsResolvedValues)
}
if rowCount == 0 {
rowCount = len(rowsResolvedValues)
}
if rowCount != len(rowsResolvedValues) {
return nil, nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: uneven row values for inserts: %d %d", rowCount, len(rowsResolvedValues))
}
vindexRowsValues[vIdx] = make([][]sqltypes.Value, rowCount)
}
// Perform the transpose.
for rowNum, colVal := range rowsResolvedValues {
vindexRowsValues[vIdx][rowNum] = append(vindexRowsValues[vIdx][rowNum], colVal)
}
}
}
// The output from the following 'process' functions is a list of
// keyspace ids. For regular inserts, a failure to find a route
// results in an error. For 'ignore' type inserts, the keyspace
// id is returned as nil, which is used later to drop such rows.
keyspaceIDs, err := ins.processPrimary(vcursor, vindexRowsValues[0], ins.Table.ColumnVindexes[0], bindVars)
if err != nil {
return nil, nil, vterrors.Wrap(err, "getInsertShardedRoute")
}
for vIdx := 1; vIdx < len(vindexRowsValues); vIdx++ {
colVindex := ins.Table.ColumnVindexes[vIdx]
var err error
if colVindex.Owned {
switch ins.Opcode {
case InsertSharded:
err = ins.processOwned(vcursor, vindexRowsValues[vIdx], colVindex, bindVars, keyspaceIDs)
case InsertShardedIgnore:
// For InsertShardedIgnore, the work is substantially different.
// So, we use a separate function.
err = ins.processOwnedIgnore(vcursor, vindexRowsValues[vIdx], colVindex, bindVars, keyspaceIDs)
default:
err = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: unexpected opcode: %v", ins.Opcode)
}
} else {
err = ins.processUnowned(vcursor, vindexRowsValues[vIdx], colVindex, bindVars, keyspaceIDs)
}
if err != nil {
return nil, nil, vterrors.Wrap(err, "getInsertShardedRoute")
}
}
// We need to know the keyspace ids and the Mids associated with
// each RSS. So we pass the ksid indexes in as ids, and get them back
// as values. We also skip nil KeyspaceIds, no need to resolve them.
var indexes []*querypb.Value
var destinations []key.Destination
for i, ksid := range keyspaceIDs {
if ksid != nil {
indexes = append(indexes, &querypb.Value{
Value: strconv.AppendInt(nil, int64(i), 10),
})
destinations = append(destinations, key.DestinationKeyspaceID(ksid))
}
}
if len(destinations) == 0 {
// In this case, all we have is nil KeyspaceIds, we don't do
// anything at all.
return nil, nil, nil
}
rss, indexesPerRss, err := vcursor.ResolveDestinations(ins.Keyspace.Name, indexes, destinations)
if err != nil {
return nil, nil, vterrors.Wrap(err, "getInsertShardedRoute")
}
queries := make([]*querypb.BoundQuery, len(rss))
for i := range rss {
var ksids [][]byte
var mids []string
for _, indexValue := range indexesPerRss[i] {
index, _ := strconv.ParseInt(string(indexValue.Value), 0, 64)
if keyspaceIDs[index] != nil {
ksids = append(ksids, keyspaceIDs[index])
mids = append(mids, ins.Mid[index])
}
}
rewritten := ins.Prefix + strings.Join(mids, ",") + ins.Suffix
rewritten = sqlannotation.AddKeyspaceIDs(rewritten, ksids, "")
queries[i] = &querypb.BoundQuery{
Sql: rewritten,
BindVariables: bindVars,
}
}
return rss, queries, nil
}
// processPrimary maps the primary vindex values to the kesypace ids.
func (ins *Insert) processPrimary(vcursor VCursor, vindexKeys [][]sqltypes.Value, colVindex *vindexes.ColumnVindex, bv map[string]*querypb.BindVariable) ([][]byte, error) {
var flattenedVindexKeys []sqltypes.Value
// TODO: @rafael - this will change once vindex Primary keys also support multicolumns
for _, val := range vindexKeys {
for _, internalVal := range val {
flattenedVindexKeys = append(flattenedVindexKeys, internalVal)
}
}
destinations, err := colVindex.Vindex.Map(vcursor, flattenedVindexKeys)
if err != nil {
return nil, err
}
keyspaceIDs := make([][]byte, len(destinations))
for i, destination := range destinations {
switch d := destination.(type) {
case key.DestinationKeyspaceID:
// This is a single keyspace id, we're good.
keyspaceIDs[i] = d
case key.DestinationNone:
// No valid keyspace id, we may return an error.
if ins.Opcode != InsertShardedIgnore {
return nil, fmt.Errorf("could not map %v to a keyspace id", flattenedVindexKeys[i])
}
default:
return nil, fmt.Errorf("could not map %v to a unique keyspace id: %v", flattenedVindexKeys[i], destination)
}
}
for rowNum, vindexKey := range flattenedVindexKeys {
if keyspaceIDs[rowNum] == nil {
// InsertShardedIgnore: skip the row.
continue
}
for _, col := range colVindex.Columns {
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexKey)
}
}
return keyspaceIDs, nil
}
// processOwned creates vindex entries for the values of an owned column for InsertSharded.
func (ins *Insert) processOwned(vcursor VCursor, vindexColumnsKeys [][]sqltypes.Value, colVindex *vindexes.ColumnVindex, bv map[string]*querypb.BindVariable, ksids [][]byte) error {
for rowNum, rowColumnKeys := range vindexColumnsKeys {
for colIdx, vindexKey := range rowColumnKeys {
col := colVindex.Columns[colIdx]
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexKey)
}
}
return colVindex.Vindex.(vindexes.Lookup).Create(vcursor, vindexColumnsKeys, ksids, false /* ignoreMode */)
}
// processOwnedIgnore creates vindex entries for the values of an owned column for InsertShardedIgnore.
func (ins *Insert) processOwnedIgnore(vcursor VCursor, vindexColumnsKeys [][]sqltypes.Value, colVindex *vindexes.ColumnVindex, bv map[string]*querypb.BindVariable, ksids [][]byte) error {
var createIndexes []int
var createKeys [][]sqltypes.Value
var createKsids [][]byte
for rowNum, rowColumnKeys := range vindexColumnsKeys {
var rowKeys []sqltypes.Value
if ksids[rowNum] == nil {
continue
}
createIndexes = append(createIndexes, rowNum)
createKsids = append(createKsids, ksids[rowNum])
for colIdx, vindexKey := range rowColumnKeys {
rowKeys = append(rowKeys, vindexKey)
col := colVindex.Columns[colIdx]
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexKey)
}
createKeys = append(createKeys, rowKeys)
}
if createKeys == nil {
return nil
}
err := colVindex.Vindex.(vindexes.Lookup).Create(vcursor, createKeys, createKsids, true /* ignoreMode */)
if err != nil {
return err
}
// After creation, verify that the keys map to the keyspace ids. If not, remove
// those that don't map.
// If values were supplied, we validate against keyspace id.
var ids []sqltypes.Value
for _, vindexValues := range createKeys {
ids = append(ids, vindexValues[0])
}
verified, err := colVindex.Vindex.Verify(vcursor, ids, createKsids)
if err != nil {
return err
}
for i, v := range verified {
if !v {
ksids[createIndexes[i]] = nil
}
}
return nil
}
// processUnowned either reverse maps or validates the values for an unowned column.
func (ins *Insert) processUnowned(vcursor VCursor, vindexColumnsKeys [][]sqltypes.Value, colVindex *vindexes.ColumnVindex, bv map[string]*querypb.BindVariable, ksids [][]byte) error {
var reverseIndexes []int
var reverseKsids [][]byte
var verifyIndexes []int
var verifyKeys []sqltypes.Value
var verifyKsids [][]byte
for rowNum, rowColumnKeys := range vindexColumnsKeys {
// Right now, we only validate against the first column of a colvindex.
// TODO(sougou): address this when we add multicolumn Map support.
vindexKey := rowColumnKeys[0]
if ksids[rowNum] == nil {
continue
}
if vindexKey.IsNull() {
reverseIndexes = append(reverseIndexes, rowNum)
reverseKsids = append(reverseKsids, ksids[rowNum])
} else {
verifyIndexes = append(verifyIndexes, rowNum)
verifyKeys = append(verifyKeys, vindexKey)
verifyKsids = append(verifyKsids, ksids[rowNum])
}
}
// For cases where a value was not supplied, we reverse map it
// from the keyspace id, if possible.
if reverseKsids != nil {
reversible, ok := colVindex.Vindex.(vindexes.Reversible)
if !ok {
return fmt.Errorf("value must be supplied for column %v", colVindex.Columns)
}
reverseKeys, err := reversible.ReverseMap(vcursor, reverseKsids)
if err != nil {
return err
}
for i, reverseKey := range reverseKeys {
rowNum := reverseIndexes[i]
for colIdx, col := range colVindex.Columns {
if colIdx == 0 {
// Fill the first column with the reverse-mapped value.
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(reverseKey)
} else {
// Fill other columns with supplied values.
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexColumnsKeys[rowNum][colIdx])
}
}
}
}
if verifyKsids != nil {
// If values were supplied, we validate against keyspace id.
verified, err := colVindex.Vindex.Verify(vcursor, verifyKeys, verifyKsids)
if err != nil {
return err
}
for i, v := range verified {
rowNum := verifyIndexes[i]
if !v {
if ins.Opcode != InsertShardedIgnore {
return fmt.Errorf("values %v for column %v does not map to keyspace ids", vindexColumnsKeys, colVindex.Columns)
}
// InsertShardedIgnore: skip the row.
ksids[rowNum] = nil
continue
}
for colIdx, col := range colVindex.Columns {
bv[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexColumnsKeys[rowNum][colIdx])
}
}
}
return nil
}
func insertVarName(col sqlparser.ColIdent, rowNum int) string {
return "_" + col.CompliantName() + strconv.Itoa(rowNum)
}