-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
lookup_internal.go
393 lines (359 loc) · 13.6 KB
/
lookup_internal.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
/*
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 (
"bytes"
"context"
"fmt"
"sort"
"strconv"
"strings"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// lookupInternal implements the functions for the Lookup vindexes.
type lookupInternal struct {
Table string `json:"table"`
FromColumns []string `json:"from_columns"`
To string `json:"to"`
Autocommit bool `json:"autocommit,omitempty"`
MultiShardAutocommit bool `json:"multi_shard_autocommit,omitempty"`
Upsert bool `json:"upsert,omitempty"`
IgnoreNulls bool `json:"ignore_nulls,omitempty"`
BatchLookup bool `json:"batch_lookup,omitempty"`
sel, ver, del string // sel: map query, ver: verify query, del: delete query
}
func (lkp *lookupInternal) Init(lookupQueryParams map[string]string, autocommit, upsert, multiShardAutocommit bool) error {
lkp.Table = lookupQueryParams["table"]
lkp.To = lookupQueryParams["to"]
var fromColumns []string
for _, from := range strings.Split(lookupQueryParams["from"], ",") {
fromColumns = append(fromColumns, strings.TrimSpace(from))
}
lkp.FromColumns = fromColumns
var err error
lkp.IgnoreNulls, err = boolFromMap(lookupQueryParams, "ignore_nulls")
if err != nil {
return err
}
lkp.BatchLookup, err = boolFromMap(lookupQueryParams, "batch_lookup")
if err != nil {
return err
}
lkp.Autocommit = autocommit
lkp.Upsert = upsert
if multiShardAutocommit {
lkp.Autocommit = true
lkp.MultiShardAutocommit = true
}
// TODO @rafael: update sel and ver to support multi column vindexes. This will be done
// as part of face 2 of https://github.com/vitessio/vitess/issues/3481
// For now multi column behaves as a single column for Map and Verify operations
lkp.sel = fmt.Sprintf("select %s, %s from %s where %s in ::%s", lkp.FromColumns[0], lkp.To, lkp.Table, lkp.FromColumns[0], lkp.FromColumns[0])
lkp.ver = fmt.Sprintf("select %s from %s where %s = :%s and %s = :%s", lkp.FromColumns[0], lkp.Table, lkp.FromColumns[0], lkp.FromColumns[0], lkp.To, lkp.To)
lkp.del = lkp.initDelStmt()
return nil
}
// Lookup performs a lookup for the ids.
func (lkp *lookupInternal) Lookup(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, co vtgatepb.CommitOrder) ([]*sqltypes.Result, error) {
if vcursor == nil {
return nil, fmt.Errorf("cannot perform lookup: no vcursor provided")
}
results := make([]*sqltypes.Result, 0, len(ids))
if lkp.Autocommit {
co = vtgatepb.CommitOrder_AUTOCOMMIT
}
sel := lkp.sel
if vcursor.InTransactionAndIsDML() {
sel = sel + " for update"
}
if ids[0].IsIntegral() || lkp.BatchLookup {
// for integral types, batch query all ids and then map them back to the input order
vars, err := sqltypes.BuildBindVariable(ids)
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
bindVars := map[string]*querypb.BindVariable{
lkp.FromColumns[0]: vars,
}
result, err := vcursor.Execute(ctx, "VindexLookup", sel, bindVars, false /* rollbackOnError */, co)
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
resultMap := make(map[string][][]sqltypes.Value)
for _, row := range result.Rows {
resultMap[row[0].ToString()] = append(resultMap[row[0].ToString()], []sqltypes.Value{row[1]})
}
for _, id := range ids {
results = append(results, &sqltypes.Result{
Rows: resultMap[id.ToString()],
})
}
} else {
// for non integral and binary type, fallback to send query per id
for _, id := range ids {
vars, err := sqltypes.BuildBindVariable([]any{id})
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
bindVars := map[string]*querypb.BindVariable{
lkp.FromColumns[0]: vars,
}
var result *sqltypes.Result
result, err = vcursor.Execute(ctx, "VindexLookup", sel, bindVars, false /* rollbackOnError */, co)
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
rows := make([][]sqltypes.Value, 0, len(result.Rows))
for _, row := range result.Rows {
rows = append(rows, []sqltypes.Value{row[1]})
}
results = append(results, &sqltypes.Result{
Rows: rows,
})
}
}
return results, nil
}
// Verify returns true if ids map to values.
func (lkp *lookupInternal) Verify(ctx context.Context, vcursor VCursor, ids, values []sqltypes.Value) ([]bool, error) {
co := vtgatepb.CommitOrder_NORMAL
if lkp.Autocommit {
co = vtgatepb.CommitOrder_AUTOCOMMIT
}
return lkp.VerifyCustom(ctx, vcursor, ids, values, co)
}
func (lkp *lookupInternal) VerifyCustom(ctx context.Context, vcursor VCursor, ids, values []sqltypes.Value, co vtgatepb.CommitOrder) ([]bool, error) {
out := make([]bool, len(ids))
for i, id := range ids {
bindVars := map[string]*querypb.BindVariable{
lkp.FromColumns[0]: sqltypes.ValueBindVariable(id),
lkp.To: sqltypes.ValueBindVariable(values[i]),
}
result, err := vcursor.Execute(ctx, "VindexVerify", lkp.ver, bindVars, false /* rollbackOnError */, co)
if err != nil {
return nil, fmt.Errorf("lookup.Verify: %v", err)
}
out[i] = (len(result.Rows) != 0)
}
return out, nil
}
type sorter struct {
rowsColValues [][]sqltypes.Value
toValues []sqltypes.Value
}
func (v *sorter) Len() int {
return len(v.toValues)
}
func (v *sorter) Less(i, j int) bool {
leftRow := v.rowsColValues[i]
rightRow := v.rowsColValues[j]
for cell, left := range leftRow {
right := rightRow[cell]
lBytes, _ := left.ToBytes()
rBytes, _ := right.ToBytes()
compare := bytes.Compare(lBytes, rBytes)
if compare < 0 {
return true
}
if compare > 0 {
return false
}
}
iBytes, _ := v.toValues[i].ToBytes()
jBytes, _ := v.toValues[j].ToBytes()
return bytes.Compare(iBytes, jBytes) < 0
}
func (v *sorter) Swap(i, j int) {
v.toValues[i], v.toValues[j] = v.toValues[j], v.toValues[i]
v.rowsColValues[i], v.rowsColValues[j] = v.rowsColValues[j], v.rowsColValues[i]
}
// Create creates an association between rowsColValues and toValues by inserting rows in the vindex table.
// rowsColValues contains all the rows that are being inserted.
// For each row, we store the value of each column defined in the vindex.
// toValues contains the keyspace_id of each row being inserted.
// Given a vindex with two columns and the following insert:
//
// INSERT INTO table_a (colum_a, column_b, column_c) VALUES (value_a0, value_b0, value_c0), (value_a1, value_b1, value_c1);
// If we assume that the primary vindex is on column_c. The call to create will look like this:
// Create(vcursor, [[value_a0, value_b0,], [value_a1, value_b1]], [binary(value_c0), binary(value_c1)])
// Notice that toValues contains the computed binary value of the keyspace_id.
func (lkp *lookupInternal) Create(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, toValues []sqltypes.Value, ignoreMode bool) error {
if lkp.Autocommit {
return lkp.createCustom(ctx, vcursor, rowsColValues, toValues, ignoreMode, vtgatepb.CommitOrder_AUTOCOMMIT)
}
return lkp.createCustom(ctx, vcursor, rowsColValues, toValues, ignoreMode, vtgatepb.CommitOrder_NORMAL)
}
func (lkp *lookupInternal) createCustom(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, toValues []sqltypes.Value, ignoreMode bool, co vtgatepb.CommitOrder) error {
// Trim rows with null values
trimmedRowsCols := make([][]sqltypes.Value, 0, len(rowsColValues))
trimmedToValues := make([]sqltypes.Value, 0, len(toValues))
nextRow:
for i, row := range rowsColValues {
for j, col := range row {
if col.IsNull() {
if !lkp.IgnoreNulls {
return fmt.Errorf("lookup.Create: input has null values: row: %d, col: %d", i, j)
}
continue nextRow
}
}
trimmedRowsCols = append(trimmedRowsCols, row)
trimmedToValues = append(trimmedToValues, toValues[i])
}
if len(trimmedRowsCols) == 0 {
return nil
}
// We only need to check the first row. Number of cols per row
// is guaranteed by the engine to be uniform.
if len(trimmedRowsCols[0]) != len(lkp.FromColumns) {
return fmt.Errorf("lookup.Create: column vindex count does not match the columns in the lookup: %d vs %v", len(trimmedRowsCols[0]), lkp.FromColumns)
}
sort.Sort(&sorter{rowsColValues: trimmedRowsCols, toValues: trimmedToValues})
insStmt := "insert"
if lkp.MultiShardAutocommit {
insStmt = "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */"
}
buf := new(bytes.Buffer)
if ignoreMode {
fmt.Fprintf(buf, "%s ignore into %s(", insStmt, lkp.Table)
} else {
fmt.Fprintf(buf, "%s into %s(", insStmt, lkp.Table)
}
for _, col := range lkp.FromColumns {
fmt.Fprintf(buf, "%s, ", col)
}
fmt.Fprintf(buf, "%s) values(", lkp.To)
bindVars := make(map[string]*querypb.BindVariable, 2*len(trimmedRowsCols))
for rowIdx := range trimmedToValues {
colIds := trimmedRowsCols[rowIdx]
if rowIdx != 0 {
buf.WriteString(", (")
}
for colIdx, colID := range colIds {
fromStr := lkp.FromColumns[colIdx] + "_" + strconv.Itoa(rowIdx)
bindVars[fromStr] = sqltypes.ValueBindVariable(colID)
buf.WriteString(":" + fromStr + ", ")
}
toStr := lkp.To + "_" + strconv.Itoa(rowIdx)
buf.WriteString(":" + toStr + ")")
bindVars[toStr] = sqltypes.ValueBindVariable(trimmedToValues[rowIdx])
}
if lkp.Upsert {
fmt.Fprintf(buf, " on duplicate key update ")
for _, col := range lkp.FromColumns {
fmt.Fprintf(buf, "%s=values(%s), ", col, col)
}
fmt.Fprintf(buf, "%s=values(%s)", lkp.To, lkp.To)
}
if _, err := vcursor.Execute(ctx, "VindexCreate", buf.String(), bindVars, true /* rollbackOnError */, co); err != nil {
return fmt.Errorf("lookup.Create: %v", err)
}
return nil
}
// Delete deletes the association between ids and value.
// rowsColValues contains all the rows that are being deleted.
// For each row, we store the value of each column defined in the vindex.
// value cointains the keyspace_id of the vindex entry being deleted.
//
// Given the following information in a vindex table with two columns:
//
// +------------------+-----------+--------+
// | hex(keyspace_id) | a | b |
// +------------------+-----------+--------+
// | 52CB7B1B31B2222E | valuea | valueb |
// +------------------+-----------+--------+
//
// A call to Delete would look like this:
// Delete(vcursor, [[valuea, valueb]], 52CB7B1B31B2222E)
func (lkp *lookupInternal) Delete(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, value sqltypes.Value, co vtgatepb.CommitOrder) error {
// In autocommit mode, it's not safe to delete. So, it's a no-op.
if lkp.Autocommit {
return nil
}
if len(rowsColValues) == 0 {
// This code is unreachable. It's just a failsafe.
return nil
}
// We only need to check the first row. Number of cols per row
// is guaranteed by the engine to be uniform.
if len(rowsColValues[0]) != len(lkp.FromColumns) {
return fmt.Errorf("lookup.Delete: column vindex count does not match the columns in the lookup: %d vs %v", len(rowsColValues[0]), lkp.FromColumns)
}
for _, column := range rowsColValues {
bindVars := make(map[string]*querypb.BindVariable, len(rowsColValues))
for colIdx, columnValue := range column {
bindVars[lkp.FromColumns[colIdx]] = sqltypes.ValueBindVariable(columnValue)
}
bindVars[lkp.To] = sqltypes.ValueBindVariable(value)
_, err := vcursor.Execute(ctx, "VindexDelete", lkp.del, bindVars, true /* rollbackOnError */, co)
if err != nil {
return fmt.Errorf("lookup.Delete: %v", err)
}
}
return nil
}
// Update implements the update functionality.
func (lkp *lookupInternal) Update(ctx context.Context, vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, toValue sqltypes.Value, newValues []sqltypes.Value) error {
if err := lkp.Delete(ctx, vcursor, [][]sqltypes.Value{oldValues}, toValue, vtgatepb.CommitOrder_NORMAL); err != nil {
return err
}
return lkp.Create(ctx, vcursor, [][]sqltypes.Value{newValues}, []sqltypes.Value{toValue}, false /* ignoreMode */)
}
func (lkp *lookupInternal) initDelStmt() string {
var delBuffer bytes.Buffer
fmt.Fprintf(&delBuffer, "delete from %s where ", lkp.Table)
for colIdx, column := range lkp.FromColumns {
if colIdx != 0 {
delBuffer.WriteString(" and ")
}
delBuffer.WriteString(column + " = :" + column)
}
delBuffer.WriteString(" and " + lkp.To + " = :" + lkp.To)
return delBuffer.String()
}
func (lkp *lookupInternal) query() (selQuery string, arguments []string) {
return lkp.sel, lkp.FromColumns
}
type commonConfig struct {
autocommit bool
multiShardAutocommit bool
}
func parseCommonConfig(m map[string]string) (*commonConfig, error) {
var c commonConfig
var err error
if c.autocommit, err = boolFromMap(m, "autocommit"); err != nil {
return nil, err
}
if c.multiShardAutocommit, err = boolFromMap(m, "multi_shard_autocommit"); err != nil {
return nil, err
}
return &c, nil
}
func boolFromMap(m map[string]string, key string) (bool, error) {
val, ok := m[key]
if !ok {
return false, nil
}
switch val {
case "true":
return true, nil
case "false":
return false, nil
default:
return false, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "%s value must be 'true' or 'false': '%s'", key, val)
}
}