From 8c2492cfd34ddefd3c48e3ae3ee7d20f16d38e4b Mon Sep 17 00:00:00 2001 From: Rain Li Date: Wed, 11 Nov 2020 21:20:53 +0800 Subject: [PATCH 1/4] 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) From c5f7cb53aad45816cea76ab906c03b66401ce9ff Mon Sep 17 00:00:00 2001 From: bb7133 Date: Thu, 12 Nov 2020 11:10:24 +0800 Subject: [PATCH 2/4] Resolve the conflict --- ddl/db_integration_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 06bf64cedfe16..19bc4b52edf3a 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2059,8 +2059,6 @@ 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) @@ -2108,4 +2106,3 @@ func (s *testIntegrationSuite3) TestEnumAndSetDefaultValue(c *C) { 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) From fcdebaba07d0e2a2b1d27c1da44fdbe5c125e42c Mon Sep 17 00:00:00 2001 From: "yulai.li" Date: Thu, 12 Nov 2020 17:28:08 +0800 Subject: [PATCH 3/4] ddl: fix cherry-pick compatable --- ddl/column.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/column.go b/ddl/column.go index d5daa55f65a9f..10f6011ba0432 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -609,7 +609,7 @@ func generateOriginDefaultValue(col *model.ColumnInfo) (interface{}, error) { if verr != nil { return nil, errors.Trace(verr) } - defVal := types.NewCollateMysqlEnumDatum(defEnum, col.Collate) + defVal := types.NewMysqlEnumDatum(defEnum) return defVal.ToString() default: zeroVal := table.GetZeroValue(col) From 2be03514086acd4706b87a128f216f41cdb33850 Mon Sep 17 00:00:00 2001 From: "yulai.li" Date: Thu, 12 Nov 2020 17:48:55 +0800 Subject: [PATCH 4/4] ddl: remove unused test case --- ddl/db_integration_test.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 19bc4b52edf3a..9806251836994 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2088,21 +2088,3 @@ func (s *testIntegrationSuite3) TestIssue20741WithSetField(c *C) { 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") -}