Skip to content

Commit

Permalink
table, executor: set a real enum as the default enum value (#8469) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored and zz-jason committed Nov 28, 2018
1 parent 011529f commit a2ec9e9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,3 +1680,11 @@ func (s *testSuite) TestRebaseIfNeeded(c *C) {
tk.MustExec(`insert into t (b) values (6);`)
tk.MustQuery(`select a from t where b = 6;`).Check(testkit.Rows("30003"))
}

func (s *testSuite) TestDefEnumInsert(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table test (id int, prescription_type enum('a','b','c','d','e','f') NOT NULL, primary key(id));")
tk.MustExec("insert into test (id) values (1)")
tk.MustQuery("select prescription_type from test").Check(testkit.Rows("a"))
}
6 changes: 5 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (t
if col.Tp == mysql.TypeEnum {
// For enum type, if no default value and not null is set,
// the default value is the first element of the enum list
return types.NewDatum(col.FieldType.Elems[0]), nil
defEnum, err := types.ParseEnumValue(col.FieldType.Elems, 1)
if err != nil {
return types.Datum{}, err
}
return types.NewMysqlEnumDatum(defEnum), nil
}
if mysql.HasAutoIncrementFlag(col.Flag) {
// Auto increment column doesn't has default value and we should not return error.
Expand Down
2 changes: 1 addition & 1 deletion table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (t *testTableSuite) TestGetDefaultValue(c *C) {
},
},
false,
types.NewStringDatum("abc"),
types.NewMysqlEnumDatum(types.Enum{Name: "abc", Value: 1}),
nil,
},
{
Expand Down
6 changes: 6 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,12 @@ func NewMysqlBitDatum(b BinaryLiteral) (d Datum) {
return d
}

// NewMysqlEnumDatum creates a new MysqlEnum Datum for a Enum value.
func NewMysqlEnumDatum(e Enum) (d Datum) {
d.SetMysqlEnum(e)
return d
}

// MakeDatums creates datum slice from interfaces.
func MakeDatums(args ...interface{}) []Datum {
datums := make([]Datum, len(args))
Expand Down

0 comments on commit a2ec9e9

Please sign in to comment.