-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vindex_lookup.go
262 lines (219 loc) · 7.63 KB
/
vindex_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
/*
Copyright 2022 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 engine
import (
"context"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
var _ Primitive = (*VindexLookup)(nil)
type VindexLookup struct {
Opcode Opcode
// The vindex to use to do the Map
Vindex vindexes.LookupPlanable
// Keyspace specifies the keyspace to send the query to.
Keyspace *vindexes.Keyspace
Arguments []string
// Values specifies the vindex values to use for routing.
Values []evalengine.Expr
// We fetch data in order to do the map from this primitive
Lookup Primitive
// This is the side that needs to be routed
SendTo *Route
}
// RouteType implements the Primitive interface
func (vr *VindexLookup) RouteType() string {
return "VindexLookup"
}
// GetKeyspaceName implements the Primitive interface
func (vr *VindexLookup) GetKeyspaceName() string {
return vr.SendTo.GetKeyspaceName()
}
// GetTableName implements the Primitive interface
func (vr *VindexLookup) GetTableName() string {
return vr.SendTo.GetTableName()
}
// GetFields implements the Primitive interface
func (vr *VindexLookup) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
return vr.SendTo.GetFields(ctx, vcursor, bindVars)
}
// NeedsTransaction implements the Primitive interface
func (vr *VindexLookup) NeedsTransaction() bool {
return vr.SendTo.NeedsTransaction()
}
// TryExecute implements the Primitive interface
func (vr *VindexLookup) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
ids, err := vr.generateIds(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
results, err := vr.lookup(ctx, vcursor, ids)
if err != nil {
return nil, err
}
dest, err := vr.mapVindexToDestination(ids, results, bindVars)
if err != nil {
return nil, err
}
return vr.SendTo.executeAfterLookup(ctx, vcursor, bindVars, wantfields, ids, dest)
}
func (vr *VindexLookup) mapVindexToDestination(ids []sqltypes.Value, results []*sqltypes.Result, bindVars map[string]*querypb.BindVariable) ([]key.Destination, error) {
dest, err := vr.Vindex.MapResult(ids, results)
if err != nil {
return nil, err
}
if vr.Opcode == IN {
valsBV := &querypb.BindVariable{Type: querypb.Type_TUPLE}
valsBV.Values = make([]*querypb.Value, 0, len(ids))
for _, value := range ids {
valsBV.Values = append(valsBV.Values, sqltypes.ValueToProto(value))
}
bindVars[ListVarName] = valsBV
}
return dest, nil
}
// TryStreamExecute implements the Primitive interface
func (vr *VindexLookup) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
ids, err := vr.generateIds(ctx, vcursor, bindVars)
if err != nil {
return err
}
results, err := vr.lookup(ctx, vcursor, ids)
if err != nil {
return err
}
dest, err := vr.mapVindexToDestination(ids, results, bindVars)
if err != nil {
return err
}
return vr.SendTo.streamExecuteAfterLookup(ctx, vcursor, bindVars, wantfields, callback, ids, dest)
}
// Inputs implements the Primitive interface
func (vr *VindexLookup) Inputs() []Primitive {
if vr.Lookup != nil {
return []Primitive{vr.Lookup, vr.SendTo}
}
return []Primitive{vr.SendTo}
}
// description implements the Primitive interface
func (vr *VindexLookup) description() PrimitiveDescription {
other := map[string]any{}
if vr.Values != nil {
formattedValues := make([]string, 0, len(vr.Values))
for _, value := range vr.Values {
formattedValues = append(formattedValues, evalengine.FormatExpr(value))
}
other["Values"] = formattedValues
}
other["Vindex"] = vr.Vindex.String()
return PrimitiveDescription{
OperatorType: "VindexLookup",
Variant: vr.Opcode.String(),
Keyspace: vr.Keyspace,
Other: other,
}
}
func (vr *VindexLookup) lookup(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]*sqltypes.Result, error) {
co := vr.Vindex.GetCommitOrder()
if co != vtgatepb.CommitOrder_NORMAL {
vcursor.Session().SetCommitOrder(co)
defer vcursor.Session().SetCommitOrder(vtgatepb.CommitOrder_NORMAL)
}
if ids[0].IsIntegral() || vr.Vindex.AllowBatch() {
return vr.executeBatch(ctx, vcursor, ids)
}
return vr.executeNonBatch(ctx, vcursor, ids)
}
func (vr *VindexLookup) executeNonBatch(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]*sqltypes.Result, error) {
results := make([]*sqltypes.Result, 0, len(ids))
// 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, err
}
bindVars := map[string]*querypb.BindVariable{
vr.Arguments[0]: vars,
}
var result *sqltypes.Result
if vr.Vindex.AutoCommitEnabled() {
result, err = vcursor.ExecutePrimitiveStandalone(ctx, vr.Lookup, bindVars, false)
} else {
result, err = vcursor.ExecutePrimitive(ctx, vr.Lookup, bindVars, false)
}
if err != nil {
return nil, 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
}
func (vr *VindexLookup) executeBatch(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]*sqltypes.Result, error) {
results := make([]*sqltypes.Result, 0, len(ids))
// 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, err
}
bindVars := map[string]*querypb.BindVariable{
vr.Arguments[0]: vars,
}
var result *sqltypes.Result
if vr.Vindex.AutoCommitEnabled() {
result, err = vcursor.ExecutePrimitiveStandalone(ctx, vr.Lookup, bindVars, false)
} else {
result, err = vcursor.ExecutePrimitive(ctx, vr.Lookup, bindVars, false)
}
if err != nil {
return nil, err
}
if err != nil {
return nil, vterrors.Wrapf(err, "failed while running the lookup query")
}
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()],
})
}
return results, nil
}
func (vr *VindexLookup) generateIds(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) ([]sqltypes.Value, error) {
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
value, err := env.Evaluate(vr.Values[0])
if err != nil {
return nil, err
}
switch vr.Opcode {
case Equal, EqualUnique:
return []sqltypes.Value{value.Value(vcursor.ConnCollation())}, nil
case IN:
return value.TupleValues(), nil
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "opcode %s not supported for VindexLookup", vr.Opcode.String())
}