-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
expr_bvar.go
137 lines (121 loc) · 3.54 KB
/
expr_bvar.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
/*
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 evalengine
import (
"vitess.io/vitess/go/mysql/collations"
"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"
)
type (
BindVariable struct {
Key string
Type sqltypes.Type
Collation collations.TypedCollation
typed bool
}
)
var _ Expr = (*BindVariable)(nil)
func (env *ExpressionEnv) lookupBindVar(key string) (*querypb.BindVariable, error) {
val, ok := env.BindVars[key]
if !ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query arguments missing for %s", key)
}
return val, nil
}
// eval implements the Expr interface
func (bv *BindVariable) eval(env *ExpressionEnv) (eval, error) {
bvar, err := env.lookupBindVar(bv.Key)
if err != nil {
return nil, err
}
switch bvar.Type {
case sqltypes.Tuple:
if bv.Type != sqltypes.Tuple {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' cannot be a tuple", bv.Key)
}
tuple := make([]eval, 0, len(bvar.Values))
for _, value := range bvar.Values {
e, err := valueToEval(sqltypes.MakeTrusted(value.Type, value.Value), collations.TypedCollation{})
if err != nil {
return nil, err
}
tuple = append(tuple, e)
}
return &evalTuple{t: tuple}, nil
default:
if bv.Type == sqltypes.Tuple {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type)
}
typ := bvar.Type
if bv.typed {
typ = bv.Type
}
return valueToEval(sqltypes.MakeTrusted(typ, bvar.Value), bv.Collation)
}
}
// typeof implements the Expr interface
func (bv *BindVariable) typeof(env *ExpressionEnv, _ []*querypb.Field) (sqltypes.Type, typeFlag) {
var tt sqltypes.Type
if bv.typed {
tt = bv.Type
} else {
if bvar, err := env.lookupBindVar(bv.Key); err == nil {
tt = bvar.Type
}
}
switch tt {
case sqltypes.Null:
return sqltypes.Null, flagNull | flagNullable
case sqltypes.HexNum, sqltypes.HexVal:
return sqltypes.VarBinary, flagHex
default:
return tt, 0
}
}
func (bvar *BindVariable) compile(c *compiler) (ctype, error) {
if !bvar.typed {
return ctype{}, c.unsupported(bvar)
}
switch tt := bvar.Type; {
case sqltypes.IsSigned(tt):
c.asm.PushBVar_i(bvar.Key)
case sqltypes.IsUnsigned(tt):
c.asm.PushBVar_u(bvar.Key)
case sqltypes.IsFloat(tt):
c.asm.PushBVar_f(bvar.Key)
case sqltypes.IsDecimal(tt):
c.asm.PushBVar_d(bvar.Key)
case sqltypes.IsText(tt):
if tt == sqltypes.HexNum {
c.asm.PushBVar_hexnum(bvar.Key)
} else if tt == sqltypes.HexVal {
c.asm.PushBVar_hexval(bvar.Key)
} else {
c.asm.PushBVar_text(bvar.Key, bvar.Collation)
}
case sqltypes.IsBinary(tt):
c.asm.PushBVar_bin(bvar.Key)
case sqltypes.IsNull(tt):
c.asm.PushNull()
case tt == sqltypes.TypeJSON:
c.asm.PushBVar_json(bvar.Key)
default:
return ctype{}, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "Type is not supported: %s", tt)
}
return ctype{
Type: bvar.Type,
Col: bvar.Collation,
}, nil
}