Skip to content

Commit 26b49aa

Browse files
committed
Moved enum type
1 parent 724a50f commit 26b49aa

File tree

12 files changed

+131
-135
lines changed

12 files changed

+131
-135
lines changed

server/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func TestSchemaToFields(t *testing.T) {
620620

621621
// Set and Enum Types
622622
{Name: "set", Type: types.MustCreateSetType([]string{"one", "two", "three", "four"}, sql.Collation_Default)},
623-
{Name: "enum", Type: sql.MustCreateEnumType([]string{"one", "two", "three", "four"}, sql.Collation_Default)},
623+
{Name: "enum", Type: types.MustCreateEnumType([]string{"one", "two", "three", "four"}, sql.Collation_Default)},
624624
}
625625

626626
expected := []*query.Field{

sql/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ func TypesEqual(a, b Type) bool {
281281
// Some types cannot be compared structurally as they contain non-comparable types (such as slices), so we handle
282282
// those separately.
283283
switch at := a.(type) {
284-
case EnumType_:
284+
case types.EnumType_:
285285
aEnumType := at
286-
bEnumType := b.(EnumType_)
286+
bEnumType := b.(types.EnumType_)
287287
if len(aEnumType.indexToVal) != len(bEnumType.indexToVal) {
288288
return false
289289
}

sql/expression/in_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/dolthub/go-mysql-server/sql/expression/function"
2929
)
3030

31-
var testEnumType = sql.MustCreateEnumType([]string{"", "one", "two"}, sql.Collation_Default)
31+
var testEnumType = types.MustCreateEnumType([]string{"", "one", "two"}, sql.Collation_Default)
3232

3333
var testSetType = types.MustCreateSetType([]string{"", "one", "two"}, sql.Collation_Default)
3434

sql/information_schema/information_schema.go

Lines changed: 19 additions & 19 deletions
Large diffs are not rendered by default.

sql/mysql_db/db_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func init() {
219219
char32_utf8_bin := types.MustCreateString(sqltypes.Char, 32, sql.Collation_utf8_bin)
220220
char64_utf8_bin := types.MustCreateString(sqltypes.Char, 64, sql.Collation_utf8_bin)
221221
char255_ascii_general_ci := types.MustCreateString(sqltypes.Char, 255, sql.Collation_ascii_general_ci)
222-
enum_N_Y_utf8_general_ci := sql.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
222+
enum_N_Y_utf8_general_ci := types.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
223223

224224
// Column Templates
225225
char32_utf8_bin_not_null_default_empty := &sql.Column{

sql/mysql_db/role_edges_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func init() {
172172
// Types
173173
char32_utf8_bin := types.MustCreateString(sqltypes.Char, 32, sql.Collation_utf8_bin)
174174
char255_ascii_general_ci := types.MustCreateString(sqltypes.Char, 255, sql.Collation_ascii_general_ci)
175-
enum_N_Y_utf8_general_ci := sql.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
175+
enum_N_Y_utf8_general_ci := types.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
176176

177177
// Column Templates
178178
char32_utf8_bin_not_null_default_empty := &sql.Column{

sql/mysql_db/user_table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func init() {
113113
char32_utf8_bin := types.MustCreateString(sqltypes.Char, 32, sql.Collation_utf8_bin)
114114
char64_utf8_bin := types.MustCreateString(sqltypes.Char, 64, sql.Collation_utf8_bin)
115115
char255_ascii_general_ci := types.MustCreateString(sqltypes.Char, 255, sql.Collation_ascii_general_ci)
116-
enum_ANY_X509_SPECIFIED_utf8_general_ci := sql.MustCreateEnumType([]string{"", "ANY", "X509", "SPECIFIED"}, sql.Collation_utf8_general_ci)
117-
enum_N_Y_utf8_general_ci := sql.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
116+
enum_ANY_X509_SPECIFIED_utf8_general_ci := types.MustCreateEnumType([]string{"", "ANY", "X509", "SPECIFIED"}, sql.Collation_utf8_general_ci)
117+
enum_N_Y_utf8_general_ci := types.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
118118
text_utf8_bin := types.CreateText(sql.Collation_utf8_bin)
119119

120120
// Column Templates

sql/plan/group_by_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestGroupByAggregationGrouping(t *testing.T) {
161161

162162
func TestGroupByCollations(t *testing.T) {
163163
tString := types.MustCreateString(query.Type_VARCHAR, 255, sql.Collation_utf8mb4_0900_ai_ci)
164-
tEnum := sql.MustCreateEnumType([]string{"col1_1", "col1_2"}, sql.Collation_utf8mb4_0900_ai_ci)
164+
tEnum := types.MustCreateEnumType([]string{"col1_1", "col1_2"}, sql.Collation_utf8mb4_0900_ai_ci)
165165
tSet := types.MustCreateSetType([]string{"col1_1", "col1_2"}, sql.Collation_utf8mb4_0900_ai_ci)
166166

167167
var testCases = []struct {

sql/type.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ type SetType interface {
134134
Values() []string
135135
}
136136

137+
// EnumType represents the ENUM type.
138+
// https://dev.mysql.com/doc/refman/8.0/en/enum.html
139+
// The type of the returned value is uint16.
140+
type EnumType interface {
141+
Type
142+
// At returns the string at the given index, as well if the string was found.
143+
At(index int) (string, bool)
144+
CharacterSet() CharacterSetID
145+
Collation() CollationID
146+
// IndexOf returns the index of the given string. If the string was not found, then this returns -1.
147+
IndexOf(v string) int
148+
// NumberOfElements returns the number of enumerations.
149+
NumberOfElements() uint16
150+
// Values returns the elements, in order, of every enumeration.
151+
Values() []string
152+
}
153+
137154
type Type2 interface {
138155
Type
139156

@@ -479,7 +496,7 @@ func ColumnTypeToType(ct *sqlparser.ColumnType) (Type, error) {
479496
if err != nil {
480497
return nil, err
481498
}
482-
return CreateEnumType(ct.EnumValues, collation)
499+
return types.CreateEnumType(ct.EnumValues, collation)
483500
case "set":
484501
collation, err := ParseCollation(&ct.Charset, &ct.Collate, ct.BinaryCollate)
485502
if err != nil {

sql/enumtype.go renamed to sql/types/enum.go

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Dolthub, Inc.
1+
// Copyright 2022 Dolthub, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package sql
15+
package types
1616

1717
import (
1818
"fmt"
@@ -21,62 +21,40 @@ import (
2121
"strings"
2222
"unicode/utf8"
2323

24-
"github.com/dolthub/go-mysql-server/sql/types"
24+
"github.com/dolthub/go-mysql-server/sql"
25+
"github.com/dolthub/go-mysql-server/sql/encodings"
2526
"github.com/dolthub/vitess/go/sqltypes"
2627
"github.com/dolthub/vitess/go/vt/proto/query"
2728
"github.com/shopspring/decimal"
2829
"gopkg.in/src-d/go-errors.v1"
29-
30-
"github.com/dolthub/go-mysql-server/sql/encodings"
3130
)
3231

3332
const (
3433
// EnumTypeMinElements returns the minimum number of enumerations for the Enum type.
3534
EnumTypeMinElements = 1
3635
// EnumTypeMaxElements returns the maximum number of enumerations for the Enum type.
3736
EnumTypeMaxElements = 65535
38-
/// An ENUM column can have a maximum of 65,535 distinct elements.
37+
// / An ENUM column can have a maximum of 65,535 distinct elements.
3938
)
4039

4140
var (
42-
ErrConvertingToEnum = errors.NewKind("value %v is not valid for this Enum")
43-
ErrUnmarshallingEnum = errors.NewKind("value %v is not a marshalled value for this Enum")
44-
ErrTemporaryEnum = errors.NewKind("attempted to use temporary enum")
41+
ErrConvertingToEnum = errors.NewKind("value %v is not valid for this Enum")
4542

4643
enumValueType = reflect.TypeOf(uint16(0))
4744
)
4845

49-
// Comments with three slashes were taken directly from the linked documentation.
50-
51-
// EnumType represents the ENUM type.
52-
// https://dev.mysql.com/doc/refman/8.0/en/enum.html
53-
// The type of the returned value is uint16.
54-
type EnumType interface {
55-
Type
56-
// At returns the string at the given index, as well if the string was found.
57-
At(index int) (string, bool)
58-
CharacterSet() CharacterSetID
59-
Collation() CollationID
60-
// IndexOf returns the index of the given string. If the string was not found, then this returns -1.
61-
IndexOf(v string) int
62-
// NumberOfElements returns the number of enumerations.
63-
NumberOfElements() uint16
64-
// Values returns the elements, in order, of every enumeration.
65-
Values() []string
66-
}
67-
6846
type EnumType_ struct {
69-
collation CollationID
47+
collation sql.CollationID
7048
hashedValToIndex map[uint64]int
7149
indexToVal []string
7250
maxResponseByteLength uint32
7351
}
7452

75-
var _ EnumType = EnumType_{}
76-
var _ TypeWithCollation = EnumType_{}
53+
var _ sql.EnumType = EnumType_{}
54+
var _ sql.TypeWithCollation = EnumType_{}
7755

7856
// CreateEnumType creates a EnumType.
79-
func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
57+
func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, error) {
8058
if len(values) < EnumTypeMinElements {
8159
return nil, fmt.Errorf("number of values may not be zero")
8260
}
@@ -90,7 +68,7 @@ func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
9068
maxCharLength := collation.Collation().CharacterSet.MaxLength()
9169
valToIndex := make(map[uint64]int)
9270
for i, value := range values {
93-
if !collation.Equals(Collation_binary) {
71+
if !collation.Equals(sql.Collation_binary) {
9472
// Trailing spaces are automatically deleted from ENUM member values in the table definition when a table
9573
// is created, unless the binary charset and collation is in use
9674
value = strings.TrimRight(value, " ")
@@ -120,7 +98,7 @@ func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
12098
}
12199

122100
// MustCreateEnumType is the same as CreateEnumType except it panics on errors.
123-
func MustCreateEnumType(values []string, collation CollationID) EnumType {
101+
func MustCreateEnumType(values []string, collation sql.CollationID) sql.EnumType {
124102
et, err := CreateEnumType(values, collation)
125103
if err != nil {
126104
panic(err)
@@ -135,7 +113,7 @@ func (t EnumType_) MaxTextResponseByteLength() uint32 {
135113

136114
// Compare implements Type interface.
137115
func (t EnumType_) Compare(a interface{}, b interface{}) (int, error) {
138-
if hasNulls, res := CompareNulls(a, b); hasNulls {
116+
if hasNulls, res := sql.CompareNulls(a, b); hasNulls {
139117
return res, nil
140118
}
141119

@@ -230,7 +208,7 @@ func (t EnumType_) MustConvert(v interface{}) interface{} {
230208
}
231209

232210
// Equals implements the Type interface.
233-
func (t EnumType_) Equals(otherType Type) bool {
211+
func (t EnumType_) Equals(otherType sql.Type) bool {
234212
if ot, ok := otherType.(EnumType_); ok && t.collation.Equals(ot.collation) && len(t.indexToVal) == len(ot.indexToVal) {
235213
for i, val := range t.indexToVal {
236214
if ot.indexToVal[i] != val {
@@ -243,12 +221,12 @@ func (t EnumType_) Equals(otherType Type) bool {
243221
}
244222

245223
// Promote implements the Type interface.
246-
func (t EnumType_) Promote() Type {
224+
func (t EnumType_) Promote() sql.Type {
247225
return t
248226
}
249227

250228
// SQL implements Type interface.
251-
func (t EnumType_) SQL(ctx *Context, dest []byte, v interface{}) (sqltypes.Value, error) {
229+
func (t EnumType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Value, error) {
252230
if v == nil {
253231
return sqltypes.NULL, nil
254232
}
@@ -259,25 +237,25 @@ func (t EnumType_) SQL(ctx *Context, dest []byte, v interface{}) (sqltypes.Value
259237
value, _ := t.At(int(convertedValue.(uint16)))
260238

261239
resultCharset := ctx.GetCharacterSetResults()
262-
if resultCharset == CharacterSet_Unspecified || resultCharset == CharacterSet_binary {
240+
if resultCharset == sql.CharacterSet_Unspecified || resultCharset == sql.CharacterSet_binary {
263241
resultCharset = t.collation.CharacterSet()
264242
}
265243
encodedBytes, ok := resultCharset.Encoder().Encode(encodings.StringToBytes(value))
266244
if !ok {
267-
return sqltypes.Value{}, ErrCharSetFailedToEncode.New(t.collation.CharacterSet().Name())
245+
return sqltypes.Value{}, sql.ErrCharSetFailedToEncode.New(t.collation.CharacterSet().Name())
268246
}
269-
val := types.AppendAndSliceBytes(dest, encodedBytes)
247+
val := AppendAndSliceBytes(dest, encodedBytes)
270248

271249
return sqltypes.MakeTrusted(sqltypes.Enum, val), nil
272250
}
273251

274252
// String implements Type interface.
275253
func (t EnumType_) String() string {
276254
s := fmt.Sprintf("enum('%v')", strings.Join(t.indexToVal, `','`))
277-
if t.CharacterSet() != Collation_Default.CharacterSet() {
255+
if t.CharacterSet() != sql.Collation_Default.CharacterSet() {
278256
s += " CHARACTER SET " + t.CharacterSet().String()
279257
}
280-
if !t.collation.Equals(Collation_Default) {
258+
if !t.collation.Equals(sql.Collation_Default) {
281259
s += " COLLATE " + t.collation.String()
282260
}
283261
return s
@@ -295,13 +273,13 @@ func (t EnumType_) ValueType() reflect.Type {
295273

296274
// Zero implements Type interface.
297275
func (t EnumType_) Zero() interface{} {
298-
/// If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
276+
// / If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
299277
return uint16(1)
300278
}
301279

302280
// At implements EnumType interface.
303281
func (t EnumType_) At(index int) (string, bool) {
304-
/// The elements listed in the column specification are assigned index numbers, beginning with 1.
282+
// / The elements listed in the column specification are assigned index numbers, beginning with 1.
305283
index -= 1
306284
if index < 0 || index >= len(t.indexToVal) {
307285
return "", false
@@ -310,12 +288,12 @@ func (t EnumType_) At(index int) (string, bool) {
310288
}
311289

312290
// CharacterSet implements EnumType interface.
313-
func (t EnumType_) CharacterSet() CharacterSetID {
291+
func (t EnumType_) CharacterSet() sql.CharacterSetID {
314292
return t.collation.CharacterSet()
315293
}
316294

317295
// Collation implements EnumType interface.
318-
func (t EnumType_) Collation() CollationID {
296+
func (t EnumType_) Collation() sql.CollationID {
319297
return t.collation
320298
}
321299

@@ -327,8 +305,8 @@ func (t EnumType_) IndexOf(v string) int {
327305
return index
328306
}
329307
}
330-
/// ENUM('0','1','2')
331-
/// 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).
308+
// / ENUM('0','1','2')
309+
// / 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).
332310
if parsedIndex, err := strconv.ParseInt(v, 10, 32); err == nil {
333311
if _, ok := t.At(int(parsedIndex)); ok {
334312
return int(parsedIndex)
@@ -350,6 +328,6 @@ func (t EnumType_) Values() []string {
350328
}
351329

352330
// WithNewCollation implements TypeWithCollation interface.
353-
func (t EnumType_) WithNewCollation(collation CollationID) (Type, error) {
331+
func (t EnumType_) WithNewCollation(collation sql.CollationID) (sql.Type, error) {
354332
return CreateEnumType(t.indexToVal, collation)
355333
}

0 commit comments

Comments
 (0)