forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenumtype.go
More file actions
380 lines (332 loc) · 10.2 KB
/
Copy pathenumtype.go
File metadata and controls
380 lines (332 loc) · 10.2 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2020-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 (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"unicode/utf8"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/shopspring/decimal"
"gopkg.in/src-d/go-errors.v1"
"github.com/dolthub/go-mysql-server/sql/encodings"
)
const (
// EnumTypeMinElements returns the minimum number of enumerations for the Enum type.
EnumTypeMinElements = 1
// EnumTypeMaxElements returns the maximum number of enumerations for the Enum type.
EnumTypeMaxElements = 65535
/// An ENUM column can have a maximum of 65,535 distinct elements.
)
var (
ErrConvertingToEnum = errors.NewKind("value %v is not valid for this Enum")
ErrUnmarshallingEnum = errors.NewKind("value %v is not a marshalled value for this Enum")
ErrTemporaryEnum = errors.NewKind("attempted to use temporary enum")
enumValueType = reflect.TypeOf(uint16(0))
)
// Comments with three slashes were taken directly from the linked documentation.
// EnumType represents the ENUM type.
// https://dev.mysql.com/doc/refman/8.0/en/enum.html
// The type of the returned value is uint16.
type EnumType interface {
Type
// At returns the string at the given index, as well if the string was found.
At(index int) (string, bool)
CharacterSet() CharacterSetID
Collation() CollationID
// IndexOf returns the index of the given string. If the string was not found, then this returns -1.
IndexOf(v string) int
// NumberOfElements returns the number of enumerations.
NumberOfElements() uint16
// Values returns the elements, in order, of every enumeration.
Values() []string
}
type enumType struct {
collation CollationID
hashedValToIndex map[uint64]int
indexToVal []string
maxResponseByteLength uint32
}
var _ EnumType = enumType{}
var _ TypeWithCollation = enumType{}
// CreateEnumType creates a EnumType.
func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
if len(values) < EnumTypeMinElements {
return nil, fmt.Errorf("number of values may not be zero")
}
if len(values) > EnumTypeMaxElements {
return nil, fmt.Errorf("number of values is too large")
}
// maxResponseByteLength for an enum type is the bytes required to send back the largest enum value,
// including accounting for multibyte character representations.
var maxResponseByteLength uint32
maxCharLength := collation.Collation().CharacterSet.MaxLength()
valToIndex := make(map[uint64]int)
for i, value := range values {
if !collation.Equals(Collation_binary) {
// Trailing spaces are automatically deleted from ENUM member values in the table definition when a table
// is created, unless the binary charset and collation is in use
value = strings.TrimRight(value, " ")
}
values[i] = value
hashedVal, err := collation.HashToUint(value)
if err != nil {
return nil, err
}
if _, ok := valToIndex[hashedVal]; ok {
return nil, fmt.Errorf("duplicate entry: %v", value)
}
// The elements listed in the column specification are assigned index numbers, beginning with 1.
valToIndex[hashedVal] = i + 1
byteLength := uint32(utf8.RuneCountInString(value) * int(maxCharLength))
if byteLength > maxResponseByteLength {
maxResponseByteLength = byteLength
}
}
return enumType{
collation: collation,
hashedValToIndex: valToIndex,
indexToVal: values,
maxResponseByteLength: maxResponseByteLength,
}, nil
}
// MustCreateEnumType is the same as CreateEnumType except it panics on errors.
func MustCreateEnumType(values []string, collation CollationID) EnumType {
et, err := CreateEnumType(values, collation)
if err != nil {
panic(err)
}
return et
}
// MaxTextResponseByteLength implements the Type interface
func (t enumType) MaxTextResponseByteLength() uint32 {
return t.maxResponseByteLength
}
// Compare implements Type interface.
func (t enumType) Compare(a interface{}, b interface{}) (int, error) {
if hasNulls, res := compareNulls(a, b); hasNulls {
return res, nil
}
ai, err := t.Convert(a)
if err != nil {
return 0, err
}
bi, err := t.Convert(b)
if err != nil {
return 0, err
}
au := ai.(uint16)
bu := bi.(uint16)
if au < bu {
return -1, nil
} else if au > bu {
return 1, nil
}
return 0, nil
}
// Convert implements Type interface.
func (t enumType) Convert(v interface{}) (interface{}, error) {
if v == nil {
return nil, nil
}
switch value := v.(type) {
case int:
if _, ok := t.At(value); ok {
return uint16(value), nil
}
case uint:
return t.Convert(int(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:
return t.Convert(int(value))
case int64:
return t.Convert(int(value))
case uint64:
return t.Convert(int(value))
case float32:
if value < float32(math.MinInt) || value > float32(math.MaxInt) {
return nil, ErrConvertingToEnum.New(v)
}
return t.Convert(int(value))
case float64:
if value < float64(math.MinInt) || value > float64(math.MaxInt) {
return nil, ErrConvertingToEnum.New(v)
}
return t.Convert(int(value))
case decimal.Decimal:
return t.Convert(value.IntPart())
case decimal.NullDecimal:
if !value.Valid {
return nil, nil
}
return t.Convert(value.Decimal.IntPart())
case string:
if index := t.IndexOf(value); index != -1 {
if index > math.MaxUint16 {
return nil, ErrConvertingToEnum.New(v)
}
return uint16(index), nil
}
case []byte:
return t.Convert(string(value))
}
return nil, ErrConvertingToEnum.New(v)
}
// MustConvert implements the Type interface.
func (t enumType) MustConvert(v interface{}) interface{} {
value, err := t.Convert(v)
if err != nil {
panic(err)
}
return value
}
// Equals implements the Type interface.
func (t enumType) Equals(otherType Type) bool {
if ot, ok := otherType.(enumType); ok && t.collation.Equals(ot.collation) && len(t.indexToVal) == len(ot.indexToVal) {
for i, val := range t.indexToVal {
if ot.indexToVal[i] != val {
return false
}
}
return true
}
return false
}
// Promote implements the Type interface.
func (t enumType) Promote() Type {
return t
}
// SQL implements Type interface.
func (t enumType) SQL(ctx *Context, dest []byte, v interface{}) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
convertedValue, err := t.Convert(v)
if err != nil {
return sqltypes.Value{}, err
}
// Handle the case where Convert returns nil
if convertedValue == nil {
return sqltypes.NULL, nil
}
// Safe type assertion with validation
enumVal, ok := convertedValue.(uint16)
if !ok {
return sqltypes.Value{}, ErrConvertingToEnum.New(v)
}
value, found := t.At(int(enumVal))
if !found {
return sqltypes.Value{}, ErrConvertingToEnum.New(v)
}
resultCharset := ctx.GetCharacterSetResults()
if resultCharset == CharacterSet_Unspecified || resultCharset == CharacterSet_binary {
resultCharset = t.collation.CharacterSet()
}
encodedBytes, ok := resultCharset.Encoder().Encode(encodings.StringToBytes(value))
if !ok {
return sqltypes.Value{}, ErrCharSetFailedToEncode.New(t.collation.CharacterSet().Name())
}
val := appendAndSliceBytes(dest, encodedBytes)
return sqltypes.MakeTrusted(sqltypes.Enum, val), nil
}
// String implements Type interface.
func (t enumType) String() string {
s := fmt.Sprintf("enum('%v')", strings.Join(t.indexToVal, `','`))
if t.CharacterSet() != Collation_Default.CharacterSet() {
s += " CHARACTER SET " + t.CharacterSet().String()
}
if !t.collation.Equals(Collation_Default) {
s += " COLLATE " + t.collation.String()
}
return s
}
// Type implements Type interface.
func (t enumType) Type() query.Type {
return sqltypes.Enum
}
// ValueType implements Type interface.
func (t enumType) ValueType() reflect.Type {
return enumValueType
}
// Zero implements Type interface.
func (t enumType) Zero() interface{} {
/// If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
return uint16(1)
}
// At implements EnumType interface.
func (t enumType) At(index int) (string, bool) {
/// The elements listed in the column specification are assigned index numbers, beginning with 1.
index -= 1
if index < 0 || index >= len(t.indexToVal) {
return "", false
}
return t.indexToVal[index], true
}
// CharacterSet implements EnumType interface.
func (t enumType) CharacterSet() CharacterSetID {
return t.collation.CharacterSet()
}
// Collation implements EnumType interface.
func (t enumType) Collation() CollationID {
return t.collation
}
// IndexOf implements EnumType interface.
func (t enumType) IndexOf(v string) int {
if v == "" {
return -1
}
hashedVal, err := t.collation.HashToUint(v)
if err == nil {
if index, ok := t.hashedValToIndex[hashedVal]; ok {
return index
}
}
/// ENUM('0','1','2')
/// If you store '3', it does not match any enumeration value, so it is treated as an index and becomes '2' (the value with index 3).
if parsedIndex, err := strconv.ParseInt(v, 10, 32); err == nil {
if parsedIndex <= 0 || parsedIndex > int64(len(t.indexToVal)) {
return -1
}
if _, ok := t.At(int(parsedIndex)); ok {
return int(parsedIndex)
}
}
return -1
}
// NumberOfElements implements EnumType interface.
func (t enumType) NumberOfElements() uint16 {
return uint16(len(t.indexToVal))
}
// Values implements EnumType interface.
func (t enumType) Values() []string {
vals := make([]string, len(t.indexToVal))
copy(vals, t.indexToVal)
return vals
}
// WithNewCollation implements TypeWithCollation interface.
func (t enumType) WithNewCollation(collation CollationID) (Type, error) {
return CreateEnumType(t.indexToVal, collation)
}