Skip to content

Commit

Permalink
Merge pull request pingcap#259 from pingcap/siddontang/dev-enum-type
Browse files Browse the repository at this point in the history
support enum type
  • Loading branch information
shenli committed Sep 25, 2015
2 parents ee03f7e + 16996cf commit 44d2404
Show file tree
Hide file tree
Showing 27 changed files with 370 additions and 14 deletions.
17 changes: 12 additions & 5 deletions column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,20 @@ const defaultPrivileges string = "select,insert,update,references"

func (c *Col) getTypeDesc() string {
ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
if c.Flen != -1 {
if c.Decimal == -1 {
ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
} else {
ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
switch c.Tp {
case mysql.TypeSet, mysql.TypeEnum:
// Format is ENUM ('e1', 'e2') or SET ('e1', 'e2')
ans = append(ans, fmt.Sprintf("('%s')", strings.Join(c.Elems, "','")))
default:
if c.Flen != -1 {
if c.Decimal == -1 {
ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
} else {
ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
}
}
}

if mysql.HasUnsignedFlag(c.Flag) {
ans = append(ans, "UNSIGNED")
}
Expand Down
5 changes: 5 additions & 0 deletions column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (s *testColumnSuite) TestString(c *C) {

cs := col.String()
c.Assert(len(cs), Greater, 0)

col.Tp = mysql.TypeEnum
col.Flag = 0
col.Elems = []string{"a", "b"}
c.Assert(col.getTypeDesc(), Equals, "ENUM ('a','b')")
}

func (s *testColumnSuite) TestFind(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ func (r *driverRows) Next(dest []driver.Value) error {
dest[i] = v.ToString()
case mysql.Bit:
dest[i] = v.ToString()
case mysql.Enum:
dest[i] = v.String()
default:
return errors.Errorf("unable to handle type %T", xi)
}
Expand Down
2 changes: 2 additions & 0 deletions expression/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ func (o *BinaryOperation) coerceArithmetic(a interface{}) (interface{}, error) {
return x.ToNumber(), nil
case mysql.Bit:
return x.ToNumber(), nil
case mysql.Enum:
return x.ToNumber(), nil
default:
return x, nil
}
Expand Down
1 change: 1 addition & 0 deletions expression/binop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {
{1, opcode.Plus, []byte("1"), 2},
{1, opcode.Plus, mysql.Hex{Value: 1}, 2},
{1, opcode.Plus, mysql.Bit{Value: 1, Width: 1}, 2},
{1, opcode.Plus, mysql.Enum{Name: "a", Value: 1}, 2},

// minus
{1, opcode.Minus, 1, 0},
Expand Down
4 changes: 4 additions & 0 deletions expression/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
return x, nil
case mysql.Bit:
return x, nil
case mysql.Enum:
return x, nil
default:
return types.UndOp(a, op)
}
Expand Down Expand Up @@ -266,6 +268,8 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
return -x.ToNumber(), nil
case mysql.Bit:
return -x.ToNumber(), nil
case mysql.Enum:
return -x.ToNumber(), nil
default:
return types.UndOp(a, op)
}
Expand Down
3 changes: 3 additions & 0 deletions expression/unary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{nil, opcode.Not, nil},
{mysql.Hex{Value: 0}, opcode.Not, int64(1)},
{mysql.Bit{Value: 0, Width: 1}, opcode.Not, int64(1)},
{mysql.Enum{Name: "a", Value: 1}, opcode.Not, int64(0)},

// test BitNeg.
{nil, opcode.BitNeg, nil},
Expand All @@ -59,6 +60,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{mysql.Bit{Value: 1, Width: 1}, opcode.Plus, mysql.Bit{Value: 1, Width: 1}},
{true, opcode.Plus, int64(1)},
{false, opcode.Plus, int64(0)},
{mysql.Enum{Name: "a", Value: 1}, opcode.Plus, mysql.Enum{Name: "a", Value: 1}},

// test Minus.
{nil, opcode.Minus, nil},
Expand All @@ -77,6 +79,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{mysql.Bit{Value: 1, Width: 1}, opcode.Minus, -1.0},
{true, opcode.Minus, int64(-1)},
{false, opcode.Minus, int64(0)},
{mysql.Enum{Name: "a", Value: 1}, opcode.Minus, -1.0},
}

for _, t := range tbl {
Expand Down
2 changes: 2 additions & 0 deletions mysqldef/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func ConvertToDecimal(value interface{}) (Decimal, error) {
return NewDecimalFromInt(int64(v.Value), 0), nil
case Bit:
return NewDecimalFromUint(uint64(v.Value), 0), nil
case Enum:
return NewDecimalFromUint(uint64(v.Value), 0), nil
default:
return Decimal{}, fmt.Errorf("can't convert %v to decimal", value)
}
Expand Down
62 changes: 62 additions & 0 deletions mysqldef/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package mysqldef

import (
"strconv"
"strings"

"github.com/juju/errors"
)

// Enum is for MySQL enum type.
type Enum struct {
Name string
Value uint64
}

// String implements fmt.Stringer interface.
func (e Enum) String() string {
return e.Name
}

// ToNumber changes enum index to float64 for numeric operation.
func (e Enum) ToNumber() float64 {
return float64(e.Value)
}

// ParseEnumName creates a Enum with item name.
func ParseEnumName(elems []string, name string) (Enum, error) {
for i, n := range elems {
if strings.EqualFold(n, name) {
return Enum{Name: n, Value: uint64(i) + 1}, nil
}
}

// name doesn't exist, maybe an integer?
if num, err := strconv.ParseUint(name, 0, 64); err == nil {
return ParseEnumValue(elems, num)
}

return Enum{}, errors.Errorf("item %s is not in enum %v", name, elems)
}

// ParseEnumValue creates a Enum with special number.
func ParseEnumValue(elems []string, number uint64) (Enum, error) {
if number == 0 || number > uint64(len(elems)) {
return Enum{}, errors.Errorf("number %d overflow enum boundary [1, %d]", number, len(elems))
}

return Enum{Name: elems[number-1], Value: number}, nil
}
69 changes: 69 additions & 0 deletions mysqldef/enum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package mysqldef

import (
. "github.com/pingcap/check"
)

var _ = Suite(&testEnumSuite{})

type testEnumSuite struct {
}

func (s *testEnumSuite) TestEnum(c *C) {
tbl := []struct {
Elems []string
Name string
Expected int
}{
{[]string{"a", "b"}, "a", 1},
{[]string{"a"}, "b", 0},
{[]string{"a"}, "1", 1},
}

for _, t := range tbl {
e, err := ParseEnumName(t.Elems, t.Name)
if t.Expected == 0 {
c.Assert(err, NotNil)
c.Assert(e.ToNumber(), Equals, float64(0))
c.Assert(e.String(), Equals, "")
continue
}

c.Assert(err, IsNil)
c.Assert(e.String(), Equals, t.Elems[t.Expected-1])
c.Assert(e.ToNumber(), Equals, float64(t.Expected))
}

tblNumber := []struct {
Elems []string
Number uint64
Expected int
}{
{[]string{"a"}, 1, 1},
{[]string{"a"}, 0, 0},
}

for _, t := range tblNumber {
e, err := ParseEnumValue(t.Elems, t.Number)
if t.Expected == 0 {
c.Assert(err, NotNil)
continue
}

c.Assert(err, IsNil)
c.Assert(e.ToNumber(), Equals, float64(t.Expected))
}
}
28 changes: 28 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import (
end "END"
engine "ENGINE"
engines "ENGINES"
enum "ENUM"
eq "="
execute "EXECUTE"
exists "EXISTS"
Expand Down Expand Up @@ -439,6 +440,7 @@ import (
SimpleQualifiedIdent "Qualified identifier without *"
Statement "statement"
StatementList "statement list"
StringList "string list"
ExplainableStmt "explainable statement"
SubSelect "Sub Select"
Symbol "Constraint Symbol"
Expand Down Expand Up @@ -3617,6 +3619,22 @@ StringType:
x.Collate = $4.(string)
$$ = x
}
| "ENUM" '(' StringList ')' OptCharset OptCollate
{
x := types.NewFieldType(mysql.TypeEnum)
x.Elems = $3.([]string)
x.Charset = $5.(string)
x.Collate = $6.(string)
$$ = x
}
| "SET" '(' StringList ')' OptCharset OptCollate
{
x := types.NewFieldType(mysql.TypeSet)
x.Elems = $3.([]string)
x.Charset = $5.(string)
x.Collate = $6.(string)
$$ = x
}

BlobType:
"TINYBLOB"
Expand Down Expand Up @@ -3787,6 +3805,16 @@ OptCollate:
$$ = $2.(string)
}

StringList:
stringLit
{
$$ = []string{$1.(string)}
}
| StringList ',' stringLit
{
$$ = append($1.([]string), $3.(string))
}

/************************************************************************************
* Union statement
* See: https://dev.mysql.com/doc/refman/5.7/en/union.html
Expand Down
5 changes: 5 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func (s *testParserSuite) TestParser0(c *C) {
// For dual
{"select 1 from dual", true},
{"select 1 from dual limit 1", true},

// For enum and set type
{"create table t (c1 enum('a', 'b'), c2 set('a', 'b'))", true},
{"create table t (c1 enum)", false},
{"create table t (c1 set)", false},
}

for _, t := range table {
Expand Down
1 change: 1 addition & 0 deletions parser/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ sys_var "@@"(({global}".")|({session}".")|{local}".")?{ident}
return engines
{execute} lval.item = string(l.val)
return execute
{enum} return enum
{exists} return exists
{explain} return explain
{first} lval.item = string(l.val)
Expand Down
4 changes: 4 additions & 0 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (t *Table) unflatten(rec interface{}, col *column.Col) (interface{}, error)
return mysql.Duration{Duration: time.Duration(rec.(int64)), Fsp: col.Decimal}, nil
case mysql.TypeNewDecimal, mysql.TypeDecimal:
return mysql.ParseDecimal(rec.(string))
case mysql.TypeEnum:
return mysql.ParseEnumValue(col.Elems, rec.(uint64))
}
log.Error(col.Tp, rec, reflect.TypeOf(rec))
return nil, nil
Expand All @@ -167,6 +169,8 @@ func (t *Table) flatten(data interface{}) (interface{}, error) {
return int64(x.Duration), nil
case mysql.Decimal:
return x.String(), nil
case mysql.Enum:
return x.Value, nil
default:
return data, nil
}
Expand Down
13 changes: 13 additions & 0 deletions table/tables/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ func (ts *testSuite) TestTypes(c *C) {
c.Assert(row[5], Equals, uint64(6))
_, err = ts.se.Execute("drop table test.t")
c.Assert(err, IsNil)

_, err = ts.se.Execute("CREATE TABLE test.t (c1 enum('a', 'b', 'c'))")
c.Assert(err, IsNil)
_, err = ts.se.Execute("insert test.t values ('a'), (2), ('c')")
c.Assert(err, IsNil)
rs, err = ts.se.Execute("select c1 + 1 from test.t where c1 = 1")
c.Assert(err, IsNil)
row, err = rs[0].FirstRow()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
c.Assert(row[0], DeepEquals, float64(2))
_, err = ts.se.Execute("drop table test.t")
c.Assert(err, IsNil)
}

func (ts *testSuite) TestUniqueIndexMultipleNullEntries(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions tidb-server/server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ func dumpRowValuesBinary(alloc arena.Allocator, columns []*ColumnInfo, row []int
data = append(data, dumpBinaryTime(v)...)
case mysql.Decimal:
data = append(data, dumpLengthEncodedString(hack.Slice(v.String()), alloc)...)
case mysql.Enum:
data = append(data, dumpLengthEncodedString(hack.Slice(v.String()), alloc)...)
}
}
return
Expand Down Expand Up @@ -337,6 +339,8 @@ func dumpTextValue(mysqlType uint8, value interface{}) ([]byte, error) {
return hack.Slice(v.String()), nil
case mysql.Decimal:
return hack.Slice(v.String()), nil
case mysql.Enum:
return hack.Slice(v.String()), nil
default:
return nil, errors.Errorf("invalid type %T", value)
}
Expand Down
Loading

0 comments on commit 44d2404

Please sign in to comment.