From 8c2492cfd34ddefd3c48e3ae3ee7d20f16d38e4b Mon Sep 17 00:00:00 2001 From: Rain Li Date: Wed, 11 Nov 2020 21:20:53 +0800 Subject: [PATCH] cherry pick #20798 to release-3.0 Signed-off-by: ti-srebot --- ddl/column.go | 19 ++++++++++++--- ddl/db_integration_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/ddl/column.go b/ddl/column.go index e4295c8d6b9eb..d5daa55f65a9f 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -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.NewCollateMysqlEnumDatum(defEnum, col.Collate) + return defVal.ToString() + default: + zeroVal := table.GetZeroValue(col) + odValue, err = zeroVal.ToString() + if err != nil { + return nil, errors.Trace(err) + } } } diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 5f251057ababa..06bf64cedfe16 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2059,3 +2059,53 @@ func (s *testIntegrationSuite3) TestIssue20490(c *C) { tk.MustQuery("select b from issue20490 order by a;").Check(testkit.Rows("1", "1", "")) } +<<<<<<< HEAD +======= + +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") +} + +// TestDefaultValueIsLatin1 for issue #18977 +func (s *testIntegrationSuite3) TestEnumAndSetDefaultValue(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + defer tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a enum(0x61, 'b') not null default 0x61, b set(0x61, 'b') not null default 0x61) character set latin1") + tbl := testGetTableByName(c, s.ctx, "test", "t") + c.Assert(tbl.Meta().Columns[0].DefaultValue, Equals, "a") + c.Assert(tbl.Meta().Columns[1].DefaultValue, Equals, "a") + + tk.MustExec("drop table t") + tk.MustExec("create table t (a enum(0x61, 'b') not null default 0x61, b set(0x61, 'b') not null default 0x61) character set utf8mb4") + tbl = testGetTableByName(c, s.ctx, "test", "t") + c.Assert(tbl.Meta().Columns[0].DefaultValue, Equals, "a") + c.Assert(tbl.Meta().Columns[1].DefaultValue, Equals, "a") +} +>>>>>>> dd32482f1... ddl: Fix default value of a newly added enum column (#20798)