/
lookup.go
356 lines (305 loc) · 10.9 KB
/
lookup.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
/*
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 (
"context"
"encoding/json"
"fmt"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
)
var (
_ SingleColumn = (*LookupUnique)(nil)
_ Lookup = (*LookupUnique)(nil)
_ LookupPlanable = (*LookupUnique)(nil)
_ SingleColumn = (*LookupNonUnique)(nil)
_ Lookup = (*LookupNonUnique)(nil)
_ LookupPlanable = (*LookupNonUnique)(nil)
)
func init() {
Register("lookup", NewLookup)
Register("lookup_unique", NewLookupUnique)
}
// LookupNonUnique defines a vindex that uses a lookup table and create a mapping between from ids and KeyspaceId.
// It's NonUnique and a Lookup.
type LookupNonUnique struct {
name string
writeOnly bool
lkp lookupInternal
}
func (ln *LookupNonUnique) GetCommitOrder() vtgatepb.CommitOrder {
return vtgatepb.CommitOrder_NORMAL
}
func (ln *LookupNonUnique) AllowBatch() bool {
return ln.lkp.BatchLookup
}
// String returns the name of the vindex.
func (ln *LookupNonUnique) String() string {
return ln.name
}
// Cost returns the cost of this vindex as 20.
func (ln *LookupNonUnique) Cost() int {
return 20
}
// IsUnique returns false since the Vindex is non unique.
func (ln *LookupNonUnique) IsUnique() bool {
return false
}
// NeedsVCursor satisfies the Vindex interface.
func (ln *LookupNonUnique) NeedsVCursor() bool {
return true
}
// Map can map ids to key.Destination objects.
func (ln *LookupNonUnique) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
if ln.writeOnly {
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
// if ignore_nulls is set and the query is about single null value, then fallback to all shards
if len(ids) == 1 && ids[0].IsNull() && ln.lkp.IgnoreNulls {
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
results, err := ln.lkp.Lookup(ctx, vcursor, ids, vtgatepb.CommitOrder_NORMAL)
if err != nil {
return nil, err
}
return ln.MapResult(ids, results)
}
// MapResult implements the LookupPlanable interface
func (ln *LookupNonUnique) MapResult(ids []sqltypes.Value, results []*sqltypes.Result) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
if ln.writeOnly {
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
for _, result := range results {
if len(result.Rows) == 0 {
out = append(out, key.DestinationNone{})
continue
}
ksids := make([][]byte, 0, len(result.Rows))
for _, row := range result.Rows {
rowBytes, err := row[0].ToBytes()
if err != nil {
return nil, err
}
ksids = append(ksids, rowBytes)
}
out = append(out, key.DestinationKeyspaceIDs(ksids))
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (ln *LookupNonUnique) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
if ln.writeOnly {
out := make([]bool, len(ids))
for i := range ids {
out[i] = true
}
return out, nil
}
return ln.lkp.Verify(ctx, vcursor, ids, ksidsToValues(ksids))
}
// Create reserves the id by inserting it into the vindex table.
func (ln *LookupNonUnique) Create(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte, ignoreMode bool) error {
return ln.lkp.Create(ctx, vcursor, rowsColValues, ksidsToValues(ksids), ignoreMode)
}
// Delete deletes the entry from the vindex table.
func (ln *LookupNonUnique) Delete(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
return ln.lkp.Delete(ctx, vcursor, rowsColValues, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid), vtgatepb.CommitOrder_NORMAL)
}
// Update updates the entry in the vindex table.
func (ln *LookupNonUnique) Update(ctx context.Context, vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error {
return ln.lkp.Update(ctx, vcursor, oldValues, ksid, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid), newValues)
}
// MarshalJSON returns a JSON representation of LookupHash.
func (ln *LookupNonUnique) MarshalJSON() ([]byte, error) {
return json.Marshal(ln.lkp)
}
// Query implements the LookupPlanable interface
func (ln *LookupNonUnique) Query() (selQuery string, arguments []string) {
return ln.lkp.query()
}
// NewLookup creates a LookupNonUnique vindex.
// The supplied map has the following required fields:
//
// table: name of the backing table. It can be qualified by the keyspace.
// from: list of columns in the table that have the 'from' values of the lookup vindex.
// to: The 'to' column name of the table.
//
// The following fields are optional:
//
// autocommit: setting this to "true" will cause inserts to upsert and deletes to be ignored.
// write_only: in this mode, Map functions return the full keyrange causing a full scatter.
func NewLookup(name string, m map[string]string) (Vindex, error) {
lookup := &LookupNonUnique{name: name}
cc, err := parseCommonConfig(m)
if err != nil {
return nil, err
}
lookup.writeOnly, err = boolFromMap(m, "write_only")
if err != nil {
return nil, err
}
// if autocommit is on for non-unique lookup, upsert should also be on.
upsert := cc.autocommit || cc.multiShardAutocommit
if err := lookup.lkp.Init(m, cc.autocommit, upsert, cc.multiShardAutocommit); err != nil {
return nil, err
}
return lookup, nil
}
func ksidsToValues(ksids [][]byte) []sqltypes.Value {
values := make([]sqltypes.Value, 0, len(ksids))
for _, ksid := range ksids {
values = append(values, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid))
}
return values
}
//====================================================================
// LookupUnique defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// Unique and a Lookup.
type LookupUnique struct {
name string
writeOnly bool
lkp lookupInternal
}
func (lu *LookupUnique) GetCommitOrder() vtgatepb.CommitOrder {
return vtgatepb.CommitOrder_NORMAL
}
func (lu *LookupUnique) AllowBatch() bool {
return lu.lkp.BatchLookup
}
// NewLookupUnique creates a LookupUnique vindex.
// The supplied map has the following required fields:
//
// table: name of the backing table. It can be qualified by the keyspace.
// from: list of columns in the table that have the 'from' values of the lookup vindex.
// to: The 'to' column name of the table.
//
// The following fields are optional:
//
// autocommit: setting this to "true" will cause deletes to be ignored.
// write_only: in this mode, Map functions return the full keyrange causing a full scatter.
func NewLookupUnique(name string, m map[string]string) (Vindex, error) {
lu := &LookupUnique{name: name}
cc, err := parseCommonConfig(m)
if err != nil {
return nil, err
}
lu.writeOnly, err = boolFromMap(m, "write_only")
if err != nil {
return nil, err
}
// Don't allow upserts for unique vindexes.
if err := lu.lkp.Init(m, cc.autocommit, false /* upsert */, cc.multiShardAutocommit); err != nil {
return nil, err
}
return lu, nil
}
// String returns the name of the vindex.
func (lu *LookupUnique) String() string {
return lu.name
}
// Cost returns the cost of this vindex as 10.
func (lu *LookupUnique) Cost() int {
return 10
}
// IsUnique returns true since the Vindex is unique.
func (lu *LookupUnique) IsUnique() bool {
return true
}
// NeedsVCursor satisfies the Vindex interface.
func (lu *LookupUnique) NeedsVCursor() bool {
return true
}
// Map can map ids to key.Destination objects.
func (lu *LookupUnique) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
if lu.writeOnly {
out := make([]key.Destination, 0, len(ids))
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
results, err := lu.lkp.Lookup(ctx, vcursor, ids, vtgatepb.CommitOrder_NORMAL)
if err != nil {
return nil, err
}
return lu.MapResult(ids, results)
}
func (lu *LookupUnique) MapResult(ids []sqltypes.Value, results []*sqltypes.Result) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
for i, result := range results {
switch len(result.Rows) {
case 0:
out = append(out, key.DestinationNone{})
case 1:
rowBytes, err := result.Rows[0][0].ToBytes()
if err != nil {
return nil, err
}
out = append(out, key.DestinationKeyspaceID(rowBytes))
default:
return nil, fmt.Errorf("Lookup.Map: unexpected multiple results from vindex %s: %v", lu.lkp.Table, ids[i])
}
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (lu *LookupUnique) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
if lu.writeOnly {
out := make([]bool, len(ids))
for i := range ids {
out[i] = true
}
return out, nil
}
return lu.lkp.Verify(ctx, vcursor, ids, ksidsToValues(ksids))
}
// Create reserves the id by inserting it into the vindex table.
func (lu *LookupUnique) Create(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte, ignoreMode bool) error {
return lu.lkp.Create(ctx, vcursor, rowsColValues, ksidsToValues(ksids), ignoreMode)
}
// Update updates the entry in the vindex table.
func (lu *LookupUnique) Update(ctx context.Context, vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error {
return lu.lkp.Update(ctx, vcursor, oldValues, ksid, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid), newValues)
}
// Delete deletes the entry from the vindex table.
func (lu *LookupUnique) Delete(ctx context.Context, vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
return lu.lkp.Delete(ctx, vcursor, rowsColValues, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid), vtgatepb.CommitOrder_NORMAL)
}
// MarshalJSON returns a JSON representation of LookupUnique.
func (lu *LookupUnique) MarshalJSON() ([]byte, error) {
return json.Marshal(lu.lkp)
}
// IsBackfilling implements the LookupBackfill interface
func (lu *LookupUnique) IsBackfilling() bool {
return lu.writeOnly
}
func (lu *LookupUnique) LookupQuery() (string, error) {
return lu.lkp.sel, nil
}
func (lu *LookupUnique) Query() (string, []string) {
return lu.lkp.query()
}