Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table, executor: set a real enum as the default enum value #8469

Merged
merged 3 commits into from Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions executor/write_test.go
Expand Up @@ -2115,3 +2115,11 @@ func (s *testSuite) TestDeferConstraintCheckForInsert(c *C) {
tk.MustExec(`commit;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows("2"))
}

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
Expand Up @@ -358,7 +358,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
6 changes: 6 additions & 0 deletions types/datum.go
Expand Up @@ -1757,6 +1757,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