-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
api_literal.go
240 lines (210 loc) · 6.16 KB
/
api_literal.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
/*
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 (
"encoding/hex"
"math"
"math/big"
"strconv"
"unicode/utf8"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/mysql/decimal"
"vitess.io/vitess/go/sqltypes"
)
// NullExpr is just what you are lead to believe
var NullExpr = &Literal{}
// NewLiteralIntegralFromBytes returns a literal expression.
// It tries to return an int64, but if the value is too large, it tries with an uint64
func NewLiteralIntegralFromBytes(val []byte) (*Literal, error) {
if val[0] == '-' {
panic("NewLiteralIntegralFromBytes: negative value")
}
uval, err := strconv.ParseUint(string(val), 10, 64)
if err != nil {
if numError, ok := err.(*strconv.NumError); ok && numError.Err == strconv.ErrRange {
return NewLiteralDecimalFromBytes(val)
}
return nil, err
}
if uval <= math.MaxInt64 {
return NewLiteralInt(int64(uval)), nil
}
return NewLiteralUint(uval), nil
}
// NewLiteralInt returns a literal expression
func NewLiteralInt(i int64) *Literal {
return &Literal{newEvalInt64(i)}
}
func NewLiteralBool(b bool) *Literal {
return &Literal{newEvalBool(b)}
}
// NewLiteralUint returns a literal expression
func NewLiteralUint(i uint64) *Literal {
return &Literal{newEvalUint64(i)}
}
// NewLiteralFloat returns a literal expression
func NewLiteralFloat(val float64) *Literal {
return &Literal{newEvalFloat(val)}
}
// NewLiteralFloatFromBytes returns a float literal expression from a slice of bytes
func NewLiteralFloatFromBytes(val []byte) (*Literal, error) {
fval, err := strconv.ParseFloat(string(val), 64)
if err != nil {
return nil, err
}
return &Literal{newEvalFloat(fval)}, nil
}
func NewLiteralDecimalFromBytes(val []byte) (*Literal, error) {
dec, err := decimal.NewFromMySQL(val)
if err != nil {
return nil, err
}
return &Literal{newEvalDecimal(dec, 0, 0)}, nil
}
// NewLiteralString returns a literal expression
func NewLiteralString(val []byte, collation collations.TypedCollation) *Literal {
collation.Repertoire = collations.RepertoireASCII
for _, b := range val {
if b >= utf8.RuneSelf {
collation.Repertoire = collations.RepertoireUnicode
break
}
}
return &Literal{newEvalText(val, collation)}
}
// NewLiteralDateFromBytes returns a literal expression.
func NewLiteralDateFromBytes(val []byte) (*Literal, error) {
t, err := parseDate(val)
if err != nil {
return nil, err
}
return &Literal{t}, nil
}
// NewLiteralTimeFromBytes returns a literal expression.
// it validates the time by parsing it and checking the error.
func NewLiteralTimeFromBytes(val []byte) (*Literal, error) {
t, err := parseTime(val)
if err != nil {
return nil, err
}
return &Literal{t}, nil
}
// NewLiteralDatetimeFromBytes returns a literal expression.
// it validates the datetime by parsing it and checking the error.
func NewLiteralDatetimeFromBytes(val []byte) (*Literal, error) {
t, err := parseDateTime(val)
if err != nil {
return nil, err
}
return &Literal{t}, nil
}
func parseHexLiteral(val []byte) ([]byte, error) {
raw := make([]byte, hex.DecodedLen(len(val)))
if _, err := hex.Decode(raw, val); err != nil {
return nil, err
}
return raw, nil
}
func parseHexNumber(val []byte) ([]byte, error) {
if val[0] != '0' || val[1] != 'x' {
panic("malformed hex literal from parser")
}
if len(val)%2 == 0 {
return parseHexLiteral(val[2:])
}
// If the hex literal doesn't have an even amount of hex digits, we need
// to pad it with a '0' in the left. Instead of allocating a new slice
// for padding pad in-place by replacing the 'x' in the original slice with
// a '0', and clean it up after parsing.
val[1] = '0'
defer func() {
val[1] = 'x'
}()
return parseHexLiteral(val[1:])
}
func parseBitLiteral(val []byte) ([]byte, error) {
var i big.Int
_, ok := i.SetString(string(val), 2)
if !ok {
panic("malformed bit literal from parser")
}
return i.Bytes(), nil
}
func NewLiteralBinary(val []byte) *Literal {
return &Literal{newEvalBinary(val)}
}
func NewLiteralBinaryFromHex(val []byte) (*Literal, error) {
raw, err := parseHexLiteral(val)
if err != nil {
return nil, err
}
return &Literal{newEvalBytesHex(raw)}, nil
}
func NewLiteralBinaryFromHexNum(val []byte) (*Literal, error) {
raw, err := parseHexNumber(val)
if err != nil {
return nil, err
}
return &Literal{newEvalBytesHex(raw)}, nil
}
func NewLiteralBinaryFromBit(val []byte) (*Literal, error) {
raw, err := parseBitLiteral(val)
if err != nil {
return nil, err
}
return &Literal{newEvalBytesBit(raw)}, nil
}
// NewBindVar returns a bind variable
func NewBindVar(key string, typ sqltypes.Type, col collations.ID) *BindVariable {
return &BindVariable{
Key: key,
Type: typ,
Collation: collations.TypedCollation{
Collation: col,
Coercibility: collations.CoerceCoercible,
Repertoire: collations.RepertoireUnicode,
},
}
}
// NewBindVarTuple returns a bind variable containing a tuple
func NewBindVarTuple(key string, col collations.ID) *BindVariable {
return &BindVariable{
Key: key,
Type: sqltypes.Tuple,
Collation: collations.TypedCollation{
Collation: col,
Coercibility: collations.CoerceCoercible,
Repertoire: collations.RepertoireUnicode,
},
}
}
// NewColumn returns a column expression
func NewColumn(offset int, typ sqltypes.Type, col collations.ID) *Column {
return &Column{
Offset: offset,
Type: typ,
Collation: collations.TypedCollation{
Collation: col,
Coercibility: collations.CoerceImplicit,
Repertoire: collations.RepertoireUnicode,
},
}
}
// NewTupleExpr returns a tuple expression
func NewTupleExpr(exprs ...Expr) TupleExpr {
tupleExpr := make(TupleExpr, 0, len(exprs))
for _, f := range exprs {
tupleExpr = append(tupleExpr, f)
}
return tupleExpr
}