-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vindex_func.go
259 lines (231 loc) · 7.14 KB
/
vindex_func.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
/*
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 engine
import (
"context"
"encoding/json"
"fmt"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/vtgate/vindexes"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
var _ Primitive = (*VindexFunc)(nil)
// VindexFunc is a primitive that performs vindex functions.
type VindexFunc struct {
Opcode VindexOpcode
// Fields is the field info for the result.
Fields []*querypb.Field
// Cols contains source column numbers: 0 for id, 1 for keyspace_id.
Cols []int
// TODO(sougou): add support for MultiColumn.
Vindex vindexes.SingleColumn
Value evalengine.Expr
// VindexFunc does not take inputs
noInputs
// VindexFunc does not need to work inside a tx
noTxNeeded
}
// VindexOpcode is the opcode for a VindexFunc.
type VindexOpcode int
// These are opcode values for VindexFunc.
const (
VindexNone = VindexOpcode(iota)
VindexMap
NumVindexCodes
)
var vindexOpcodeName = map[VindexOpcode]string{
VindexMap: "VindexMap",
}
// MarshalJSON serializes the VindexOpcode into a JSON representation.
// It's used for testing and diagnostics.
func (code VindexOpcode) MarshalJSON() ([]byte, error) {
return json.Marshal(vindexOpcodeName[code])
}
// RouteType returns a description of the query routing type used by the primitive
func (vf *VindexFunc) RouteType() string {
return vindexOpcodeName[vf.Opcode]
}
// GetKeyspaceName specifies the Keyspace that this primitive routes to.
func (vf *VindexFunc) GetKeyspaceName() string {
return ""
}
// GetTableName specifies the table that this primitive routes to.
func (vf *VindexFunc) GetTableName() string {
return ""
}
// TryExecute performs a non-streaming exec.
func (vf *VindexFunc) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
return vf.mapVindex(ctx, vcursor, bindVars)
}
// TryStreamExecute performs a streaming exec.
func (vf *VindexFunc) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
r, err := vf.mapVindex(ctx, vcursor, bindVars)
if err != nil {
return err
}
if err := callback(r.Metadata()); err != nil {
return err
}
return callback(&sqltypes.Result{Rows: r.Rows})
}
// GetFields fetches the field info.
func (vf *VindexFunc) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
return &sqltypes.Result{Fields: vf.Fields}, nil
}
func (vf *VindexFunc) mapVindex(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
k, err := env.Evaluate(vf.Value)
if err != nil {
return nil, err
}
var values []sqltypes.Value
value := k.Value(vcursor.ConnCollation())
if value.Type() == querypb.Type_TUPLE {
values = k.TupleValues()
} else {
values = append(values, value)
}
result := &sqltypes.Result{
Fields: vf.Fields,
}
destinations, err := vf.Vindex.Map(ctx, vcursor, values)
if err != nil {
return nil, err
}
if len(destinations) != len(values) {
// should never happen
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "Vindex.Map() length mismatch: input values count is %d, output destinations count is %d",
len(values), len(destinations))
}
for i, value := range values {
vkey, err := evalengine.Cast(value, sqltypes.VarBinary)
if err != nil {
return nil, err
}
switch d := destinations[i].(type) {
case key.DestinationKeyRange:
if d.KeyRange != nil {
row, err := vf.buildRow(vkey, nil, d.KeyRange)
if err != nil {
return result, err
}
result.Rows = append(result.Rows, row)
}
case key.DestinationKeyspaceID:
if len(d) > 0 {
if vcursor != nil {
resolvedShards, _, err := vcursor.ResolveDestinations(ctx, vcursor.GetKeyspace(), nil, []key.Destination{d})
if err != nil {
return nil, err
}
if len(resolvedShards) > 0 {
kr, err := key.ParseShardingSpec(resolvedShards[0].Target.Shard)
if err != nil {
return nil, err
}
row, err := vf.buildRow(vkey, d, kr[0])
if err != nil {
return result, err
}
result.Rows = append(result.Rows, row)
break
}
}
row, err := vf.buildRow(vkey, d, nil)
if err != nil {
return result, err
}
result.Rows = append(result.Rows, row)
}
case key.DestinationKeyspaceIDs:
for _, ksid := range d {
row, err := vf.buildRow(vkey, ksid, nil)
if err != nil {
return result, err
}
result.Rows = append(result.Rows, row)
}
case key.DestinationNone:
// Nothing to do.
default:
return result, vterrors.NewErrorf(vtrpcpb.Code_INTERNAL, vterrors.WrongTypeForVar, "unexpected destination type: %T", d)
}
}
return result, nil
}
func (vf *VindexFunc) buildRow(id sqltypes.Value, ksid []byte, kr *topodatapb.KeyRange) ([]sqltypes.Value, error) {
row := make([]sqltypes.Value, 0, len(vf.Fields))
for _, col := range vf.Cols {
switch col {
case 0:
row = append(row, id)
case 1:
if ksid != nil {
row = append(row, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid))
} else {
row = append(row, sqltypes.NULL)
}
case 2:
if kr != nil {
row = append(row, sqltypes.MakeTrusted(sqltypes.VarBinary, kr.Start))
} else {
row = append(row, sqltypes.NULL)
}
case 3:
if kr != nil {
row = append(row, sqltypes.MakeTrusted(sqltypes.VarBinary, kr.End))
} else {
row = append(row, sqltypes.NULL)
}
case 4:
if ksid != nil {
row = append(row, sqltypes.NewVarBinary(fmt.Sprintf("%x", ksid)))
} else {
row = append(row, sqltypes.NULL)
}
case 5:
if ksid != nil {
row = append(row, sqltypes.NewVarBinary(key.KeyRangeString(kr)))
} else {
row = append(row, sqltypes.NULL)
}
default:
return row, vterrors.NewErrorf(vtrpcpb.Code_OUT_OF_RANGE, vterrors.BadFieldError, "column %v out of range", col)
}
}
return row, nil
}
func (vf *VindexFunc) description() PrimitiveDescription {
fields := map[string]string{}
for _, field := range vf.Fields {
fields[field.Name] = field.Type.String()
}
other := map[string]any{
"Fields": fields,
"Columns": vf.Cols,
"Value": evalengine.FormatExpr(vf.Value),
}
if vf.Vindex != nil {
other["Vindex"] = vf.Vindex.String()
}
return PrimitiveDescription{
OperatorType: "VindexFunc",
Variant: vindexOpcodeName[vf.Opcode],
Other: other,
}
}