/
plan_test_vindex.go
233 lines (206 loc) · 9.01 KB
/
plan_test_vindex.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
/*
Copyright 2023 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 planbuilder
import (
"context"
"strconv"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// hashIndex is a functional, unique Vindex.
type hashIndex struct{ name string }
func (v *hashIndex) String() string { return v.name }
func (*hashIndex) Cost() int { return 1 }
func (*hashIndex) IsUnique() bool { return true }
func (*hashIndex) NeedsVCursor() bool { return false }
func (*hashIndex) Verify(context.Context, vindexes.VCursor, []sqltypes.Value, [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*hashIndex) Map(ctx context.Context, vcursor vindexes.VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func newHashIndex(name string, _ map[string]string) (vindexes.Vindex, error) {
return &hashIndex{name: name}, nil
}
// lookupIndex is a unique Vindex, and satisfies Lookup.
type lookupIndex struct{ name string }
func (v *lookupIndex) String() string { return v.name }
func (*lookupIndex) Cost() int { return 2 }
func (*lookupIndex) IsUnique() bool { return true }
func (*lookupIndex) NeedsVCursor() bool { return false }
func (*lookupIndex) Verify(context.Context, vindexes.VCursor, []sqltypes.Value, [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*lookupIndex) Map(ctx context.Context, vcursor vindexes.VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func (*lookupIndex) Create(context.Context, vindexes.VCursor, [][]sqltypes.Value, [][]byte, bool) error {
return nil
}
func (*lookupIndex) Delete(context.Context, vindexes.VCursor, [][]sqltypes.Value, []byte) error {
return nil
}
func (*lookupIndex) Update(context.Context, vindexes.VCursor, []sqltypes.Value, []byte, []sqltypes.Value) error {
return nil
}
func newLookupIndex(name string, _ map[string]string) (vindexes.Vindex, error) {
return &lookupIndex{name: name}, nil
}
var _ vindexes.Lookup = (*lookupIndex)(nil)
// nameLkpIndex satisfies Lookup, NonUnique.
type nameLkpIndex struct {
name string
inBackfill bool
}
func (v *nameLkpIndex) String() string { return v.name }
func (*nameLkpIndex) Cost() int { return 3 }
func (*nameLkpIndex) IsUnique() bool { return false }
func (*nameLkpIndex) NeedsVCursor() bool { return false }
func (*nameLkpIndex) AllowBatch() bool { return true }
func (*nameLkpIndex) AutoCommitEnabled() bool { return false }
func (*nameLkpIndex) GetCommitOrder() vtgatepb.CommitOrder { return vtgatepb.CommitOrder_NORMAL }
func (*nameLkpIndex) Verify(context.Context, vindexes.VCursor, []sqltypes.Value, [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*nameLkpIndex) Map(ctx context.Context, vcursor vindexes.VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func (*nameLkpIndex) Create(context.Context, vindexes.VCursor, [][]sqltypes.Value, [][]byte, bool) error {
return nil
}
func (*nameLkpIndex) Delete(context.Context, vindexes.VCursor, [][]sqltypes.Value, []byte) error {
return nil
}
func (*nameLkpIndex) Update(context.Context, vindexes.VCursor, []sqltypes.Value, []byte, []sqltypes.Value) error {
return nil
}
func (*nameLkpIndex) Query() (string, []string) {
return "select name, keyspace_id from name_user_vdx where name in ::name", []string{"name"}
}
func (*nameLkpIndex) MapResult([]sqltypes.Value, []*sqltypes.Result) ([]key.Destination, error) {
return nil, nil
}
func (v *nameLkpIndex) IsBackfilling() bool { return v.inBackfill }
func newNameLkpIndex(name string, m map[string]string) (vindexes.Vindex, error) {
vdx := &nameLkpIndex{name: name}
if val, ok := m["write_only"]; ok {
vdx.inBackfill = val == "true"
}
return vdx, nil
}
var _ vindexes.Vindex = (*nameLkpIndex)(nil)
var _ vindexes.Lookup = (*nameLkpIndex)(nil)
var _ vindexes.LookupPlanable = (*nameLkpIndex)(nil)
var _ vindexes.LookupBackfill = (*nameLkpIndex)(nil)
// costlyIndex satisfies Lookup, NonUnique.
type costlyIndex struct{ name string }
func (v *costlyIndex) String() string { return v.name }
func (*costlyIndex) Cost() int { return 10 }
func (*costlyIndex) IsUnique() bool { return false }
func (*costlyIndex) NeedsVCursor() bool { return false }
func (*costlyIndex) Verify(context.Context, vindexes.VCursor, []sqltypes.Value, [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*costlyIndex) Map(ctx context.Context, vcursor vindexes.VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func (*costlyIndex) Create(context.Context, vindexes.VCursor, [][]sqltypes.Value, [][]byte, bool) error {
return nil
}
func (*costlyIndex) Delete(context.Context, vindexes.VCursor, [][]sqltypes.Value, []byte) error {
return nil
}
func (*costlyIndex) Update(context.Context, vindexes.VCursor, []sqltypes.Value, []byte, []sqltypes.Value) error {
return nil
}
func newCostlyIndex(name string, _ map[string]string) (vindexes.Vindex, error) {
return &costlyIndex{name: name}, nil
}
var _ vindexes.Vindex = (*costlyIndex)(nil)
var _ vindexes.Lookup = (*costlyIndex)(nil)
// multiColIndex satisfies multi column vindex.
type multiColIndex struct{ name string }
func (m *multiColIndex) String() string { return m.name }
func (*multiColIndex) Cost() int { return 1 }
func (*multiColIndex) IsUnique() bool { return true }
func (*multiColIndex) NeedsVCursor() bool { return false }
func (*multiColIndex) Map(ctx context.Context, vcursor vindexes.VCursor, rowsColValues [][]sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func (*multiColIndex) Verify(ctx context.Context, vcursor vindexes.VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*multiColIndex) PartialVindex() bool { return true }
func newMultiColIndex(name string, _ map[string]string) (vindexes.Vindex, error) {
return &multiColIndex{name: name}, nil
}
var _ vindexes.MultiColumn = (*multiColIndex)(nil)
// unqLkpVdxBackfill satisfies Lookup, Unique.
type unqLkpVdxBackfill struct {
name string
inBackfill bool
cost int
}
func (u *unqLkpVdxBackfill) String() string { return u.name }
func (u *unqLkpVdxBackfill) Cost() int { return u.cost }
func (*unqLkpVdxBackfill) IsUnique() bool { return false }
func (*unqLkpVdxBackfill) NeedsVCursor() bool { return false }
func (*unqLkpVdxBackfill) AllowBatch() bool { return true }
func (*unqLkpVdxBackfill) AutoCommitEnabled() bool { return false }
func (*unqLkpVdxBackfill) GetCommitOrder() vtgatepb.CommitOrder { return vtgatepb.CommitOrder_NORMAL }
func (*unqLkpVdxBackfill) Verify(context.Context, vindexes.VCursor, []sqltypes.Value, [][]byte) ([]bool, error) {
return []bool{}, nil
}
func (*unqLkpVdxBackfill) Map(ctx context.Context, vcursor vindexes.VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
return nil, nil
}
func (*unqLkpVdxBackfill) Create(context.Context, vindexes.VCursor, [][]sqltypes.Value, [][]byte, bool) error {
return nil
}
func (*unqLkpVdxBackfill) Delete(context.Context, vindexes.VCursor, [][]sqltypes.Value, []byte) error {
return nil
}
func (*unqLkpVdxBackfill) Update(context.Context, vindexes.VCursor, []sqltypes.Value, []byte, []sqltypes.Value) error {
return nil
}
func (*unqLkpVdxBackfill) Query() (string, []string) {
return "select unq_key, keyspace_id from unq_lkp_idx where unq_key in ::unq_key", []string{"unq_key"}
}
func (*unqLkpVdxBackfill) MapResult([]sqltypes.Value, []*sqltypes.Result) ([]key.Destination, error) {
return nil, nil
}
func (u *unqLkpVdxBackfill) IsBackfilling() bool { return u.inBackfill }
func newUnqLkpVdxBackfill(name string, m map[string]string) (vindexes.Vindex, error) {
vdx := &unqLkpVdxBackfill{name: name}
if val, ok := m["write_only"]; ok {
vdx.inBackfill = val == "true"
}
if val, ok := m["cost"]; ok {
vdx.cost, _ = strconv.Atoi(val)
}
return vdx, nil
}
var _ vindexes.Vindex = (*unqLkpVdxBackfill)(nil)
var _ vindexes.Lookup = (*unqLkpVdxBackfill)(nil)
var _ vindexes.LookupPlanable = (*unqLkpVdxBackfill)(nil)
var _ vindexes.LookupBackfill = (*unqLkpVdxBackfill)(nil)
func init() {
vindexes.Register("hash_test", newHashIndex)
vindexes.Register("lookup_test", newLookupIndex)
vindexes.Register("name_lkp_test", newNameLkpIndex)
vindexes.Register("costly", newCostlyIndex)
vindexes.Register("multiCol_test", newMultiColIndex)
vindexes.Register("unq_lkp_test", newUnqLkpVdxBackfill)
}