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

Merged
merged 10 commits into from
Nov 11, 2020
19 changes: 15 additions & 4 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,10 +1668,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Set also has this problem, could you fix it together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just compared with mysql, current logic's result is same as mysql.

below is mysql result:

mysql> create table t (id int primary key, c int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t (id, c) values (1, 1), (2, 2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> alter table t add column cc set('a','b','c','d') not null;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> update t set c = 2 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t;
+----+------+----+
| id | c    | cc |
+----+------+----+
|  1 |    2 |    |
|  2 |    2 |    |
+----+------+----+
2 rows in set (0.00 sec)

mysql> select * from t where cc = 0;
+----+------+----+
| id | c    | cc |
+----+------+----+
|  1 |    2 |    |
|  2 |    2 |    |
+----+------+----+
2 rows in set (0.00 sec)

mysql> select * from t where cc = 1;
Empty set (0.00 sec)

mysql> insert into t (id, c) values (3, 3);
ERROR 1364 (HY000): Field 'cc' doesn't have a default value

Then TiDB result:

mysql> create table t (id int primary key, c int);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t (id, c) values (1, 1), (2, 2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> alter table t add column cc set('a','b','c','d') not null;
Query OK, 0 rows affected (0.01 sec)

mysql> update t set c = 2 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t;
+----+------+----+
| id | c    | cc |
+----+------+----+
|  1 |    2 |    |
|  2 |    2 |    |
+----+------+----+
2 rows in set (0.00 sec)

mysql> select * from t where cc = 0;
+----+------+----+
| id | c    | cc |
+----+------+----+
|  1 |    2 |    |
|  2 |    2 |    |
+----+------+----+
2 rows in set (0.00 sec)

mysql> select * from t where cc = 1;
Empty set (0.00 sec)

mysql> insert into t (id, c) values (3, 3);
ERROR 1364 (HY000): Field 'cc' doesn't have a default value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add these results as a test.

// 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)
}
}
}

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

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

func (s *testIntegrationSuite3) TestIssue20741(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"))
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6A
cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY=
cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc=
cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0=
cloud.google.com/go v0.50.0 h1:0E3eE8MX426vUOs7aHfI7aN1BrIzzzf4ccKCSfSjGmc=
cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0 h1:sAbMqjY1PEQKZBWfbu6Y6bsupJ9c4QdHnzg/VvYTLcE=
Expand Down