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

ddl: Fix default value of a newly added enum column (#20798) #20999

Merged
merged 6 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,21 @@ func generateOriginDefaultValue(col *model.ColumnInfo) (interface{}, error) {
var err error
odValue := col.GetDefaultValue()
if odValue == nil && mysql.HasNotNullFlag(col.Flag) {
zeroVal := table.GetZeroValue(col)
odValue, err = zeroVal.ToString()
if err != nil {
return nil, errors.Trace(err)
switch col.Tp {
// Just use enum field's first element for OriginDefaultValue.
case mysql.TypeEnum:
defEnum, verr := types.ParseEnumValue(col.FieldType.Elems, 1)
if verr != nil {
return nil, errors.Trace(verr)
}
defVal := types.NewMysqlEnumDatum(defEnum)
return defVal.ToString()
default:
zeroVal := table.GetZeroValue(col)
odValue, err = zeroVal.ToString()
if err != nil {
return nil, errors.Trace(err)
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,3 +2059,32 @@ func (s *testIntegrationSuite3) TestIssue20490(c *C) {

tk.MustQuery("select b from issue20490 order by a;").Check(testkit.Rows("1", "1", "<nil>"))
}

func (s *testIntegrationSuite3) TestIssue20741WithEnumField(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists issue20741")
tk.MustExec("create table issue20741(id int primary key, c int)")
tk.MustExec("insert into issue20741(id, c) values(1, 2), (2, 2)")
tk.MustExec("alter table issue20741 add column cc enum('a', 'b', 'c', 'd') not null")
tk.MustExec("update issue20741 set c=2 where id=1")
tk.MustQuery("select * from issue20741").Check(testkit.Rows("1 2 a", "2 2 a"))
tk.MustQuery("select * from issue20741 where cc = 0").Check(testkit.Rows())
tk.MustQuery("select * from issue20741 where cc = 1").Check(testkit.Rows("1 2 a", "2 2 a"))
}

func (s *testIntegrationSuite3) TestIssue20741WithSetField(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists issue20741_2")
tk.MustExec("create table issue20741_2(id int primary key, c int)")
tk.MustExec("insert into issue20741_2(id, c) values(1, 2), (2, 2)")
tk.MustExec("alter table issue20741_2 add column cc set('a', 'b', 'c', 'd') not null")
tk.MustExec("update issue20741_2 set c=2 where id=1")
tk.MustQuery("select * from issue20741_2").Check(testkit.Rows("1 2 ", "2 2 "))
tk.MustQuery("select * from issue20741_2 where cc = 0").Check(testkit.Rows("1 2 ", "2 2 "))
tk.MustQuery("select * from issue20741_2 where cc = 1").Check(testkit.Rows())
_, err := tk.Exec("insert into issue20741_2(id, c) values (3, 3)")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[table:1364]Field 'cc' doesn't have a default value")
}