forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem_enumtype.go
More file actions
317 lines (273 loc) · 8.01 KB
/
Copy pathsystem_enumtype.go
File metadata and controls
317 lines (273 loc) · 8.01 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2021 Dolthub, Inc.
//
// 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 sql
import (
"math"
"reflect"
"strconv"
"strings"
"github.com/shopspring/decimal"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
)
var systemEnumValueType = reflect.TypeOf(string(""))
// systemEnumType is an internal enum type ONLY for system variables.
type systemEnumType struct {
varName string
valToIndex map[string]int
indexToVal []string
}
var _ SystemVariableType = systemEnumType{}
// NewSystemEnumType returns a new systemEnumType.
func NewSystemEnumType(varName string, values ...string) SystemVariableType {
if len(values) > 65535 { // system variables should NEVER hit this
// Instead of panicking, return a default safe value
// Log error internally and cap at max allowed size
values = values[:65535]
}
valToIndex := make(map[string]int)
for i, value := range values {
valToIndex[strings.ToLower(value)] = i
}
return systemEnumType{varName, valToIndex, values}
}
// Compare implements Type interface.
func (t systemEnumType) Compare(a interface{}, b interface{}) (int, error) {
as, err := t.Convert(a)
if err != nil {
return 0, err
}
bs, err := t.Convert(b)
if err != nil {
return 0, err
}
// Handle nil values that might be returned by Convert
if as == nil {
if bs == nil {
return 0, nil
}
return -1, nil
} else if bs == nil {
return 1, nil
}
// Safe type assertion with validation
ai, ok := as.(string)
if !ok {
return 0, ErrInvalidSystemVariableValue.New(t.varName, a)
}
bi, ok := bs.(string)
if !ok {
return 0, ErrInvalidSystemVariableValue.New(t.varName, b)
}
if ai == bi {
return 0, nil
}
if ai < bi {
return -1, nil
}
return 1, nil
}
// Convert implements Type interface.
func (t systemEnumType) Convert(v interface{}) (interface{}, error) {
if v == nil {
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
}
// Nil values are not accepted
switch value := v.(type) {
case int:
if value >= 0 && value < len(t.indexToVal) {
return t.indexToVal[value], nil
}
case uint:
if value <= math.MaxInt {
return t.Convert(int(value))
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, value)
case int8:
return t.Convert(int(value))
case uint8:
return t.Convert(int(value))
case int16:
return t.Convert(int(value))
case uint16:
return t.Convert(int(value))
case int32:
return t.Convert(int(value))
case uint32:
// uint32 max value is less than MaxInt, so no overflow possible
return t.Convert(int(value))
case int64:
if value >= math.MinInt && value <= math.MaxInt {
return t.Convert(int(value))
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, value)
case uint64:
if value <= math.MaxInt {
return t.Convert(int(value))
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, value)
case float32:
return t.Convert(float64(value))
case float64:
// Float values aren't truly accepted, but the engine will give them when it should give ints.
// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
if value >= 0 && value <= float64(math.MaxInt) && value == math.Trunc(value) {
return t.Convert(int(value))
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, value)
case decimal.Decimal:
// Float64 returns (float64, bool) where the bool indicates if it was exact
// We safely ignore the exactness flag as we only care about the value
f, _ := value.Float64()
return t.Convert(f)
case decimal.NullDecimal:
if value.Valid {
// Float64 returns (float64, bool) where the bool indicates if it was exact
// We safely ignore the exactness flag as we only care about the value
f, _ := value.Decimal.Float64()
return t.Convert(f)
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
case string:
if idx, ok := t.valToIndex[strings.ToLower(value)]; ok {
return t.indexToVal[idx], nil
}
// Check if the string represents a numeric index
if parsedIndex, err := strconv.ParseInt(value, 10, 32); err == nil {
if parsedIndex >= 0 && parsedIndex < int64(len(t.indexToVal)) {
return t.indexToVal[parsedIndex], nil
}
}
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
}
// MustConvert implements the Type interface.
func (t systemEnumType) MustConvert(v interface{}) interface{} {
// Even though this method is named "Must", we should never panic
// Return a safe default value if conversion fails
value, err := t.Convert(v)
if err != nil {
return t.Zero()
}
// Even with a nil error, Convert might return nil for invalid values
if value == nil {
return t.Zero()
}
return value
}
// Equals implements the Type interface.
func (t systemEnumType) Equals(otherType Type) bool {
if otherType == nil {
return false
}
ot, ok := otherType.(systemEnumType)
if !ok {
return false
}
if t.varName != ot.varName || len(t.indexToVal) != len(ot.indexToVal) {
return false
}
for i, val := range t.indexToVal {
if i >= len(ot.indexToVal) || ot.indexToVal[i] != val {
return false
}
}
return true
}
// MaxTextResponseByteLength implements the Type interface
func (t systemEnumType) MaxTextResponseByteLength() uint32 {
// system types are not sent directly across the wire
return 0
}
// Promote implements the Type interface.
func (t systemEnumType) Promote() Type {
return t
}
// SQL implements Type interface.
func (t systemEnumType) SQL(ctx *Context, dest []byte, v interface{}) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
v, err := t.Convert(v)
if err != nil {
return sqltypes.Value{}, err
}
// Check if conversion returned nil
if v == nil {
return sqltypes.NULL, nil
}
// Safe type assertion with validation
strValue, ok := v.(string)
if !ok {
return sqltypes.Value{}, ErrInvalidSystemVariableValue.New(t.varName, v)
}
val := appendAndSliceString(dest, strValue)
return sqltypes.MakeTrusted(t.Type(), val), nil
}
// String implements Type interface.
func (t systemEnumType) String() string {
return "system_enum"
}
// Type implements Type interface.
func (t systemEnumType) Type() query.Type {
return sqltypes.VarChar
}
// ValueType implements Type interface.
func (t systemEnumType) ValueType() reflect.Type {
return systemEnumValueType
}
// Zero implements Type interface.
func (t systemEnumType) Zero() interface{} {
return ""
}
// EncodeValue implements SystemVariableType interface.
func (t systemEnumType) EncodeValue(val interface{}) (string, error) {
if val == nil {
return "", ErrSystemVariableCodeFail.New(val, t.String())
}
// Try to convert value to ensure it's valid for this enum
convertedVal, err := t.Convert(val)
if err != nil {
return "", err
}
// Ensure conversion returned a valid value
if convertedVal == nil {
return "", ErrSystemVariableCodeFail.New(val, t.String())
}
expectedVal, ok := convertedVal.(string)
if !ok {
return "", ErrSystemVariableCodeFail.New(val, t.String())
}
return expectedVal, nil
}
// DecodeValue implements SystemVariableType interface.
func (t systemEnumType) DecodeValue(val string) (interface{}, error) {
if val == "" {
return nil, ErrSystemVariableCodeFail.New(val, t.String())
}
outVal, err := t.Convert(val)
if err != nil {
return nil, ErrSystemVariableCodeFail.New(val, t.String())
}
// Ensure conversion returned a valid value
if outVal == nil {
return nil, ErrSystemVariableCodeFail.New(val, t.String())
}
// Validate that the returned value is a string
_, ok := outVal.(string)
if !ok {
return nil, ErrSystemVariableCodeFail.New(val, t.String())
}
return outVal, nil
}