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

Conversation

blacktear23
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #20741

Problem Summary:

The default value for new add enum column not perform correctly.

What is changed and how it works?

What's Changed:

Check column's type if is Enum just use first element for default value.

Related changes

  • No

Check List

Tests

  • Integration test

Side effects

  • No

Release note

  • Set correct default value for new added enum column.

@github-actions github-actions bot added the sig/sql-infra SIG: SQL Infra label Nov 3, 2020
@wjhuang2016
Copy link
Member

/reward 600

@ti-challenge-bot
Copy link

Reward success.

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.

@blacktear23
Copy link
Contributor Author

@wjhuang2016 after I add some debug log I found when create table the OriginDefaultValue is nil and then insert some records it can use enum first element as default value. But when execute alter table add column, we should set OriginDefaultValue to enum's first element explicit. That makes me feel strange.

@wjhuang2016
Copy link
Member

@wjhuang2016 after I add some debug log I found when create table the OriginDefaultValue is nil and then insert some records it can use enum first element as default value. But when execute alter table add column, we should set OriginDefaultValue to enum's first element explicit. That makes me feel strange.

Yeah, this is an optimization. Add column can be done quickly.

@blacktear23
Copy link
Contributor Author

@wjhuang2016 after I add some debug log I found when create table the OriginDefaultValue is nil and then insert some records it can use enum first element as default value. But when execute alter table add column, we should set OriginDefaultValue to enum's first element explicit. That makes me feel strange.

Yeah, this is an optimization. Add column can be done quickly.

Do we need add OriginDefaultValue when create table?

@wjhuang2016
Copy link
Member

@wjhuang2016 after I add some debug log I found when create table the OriginDefaultValue is nil and then insert some records it can use enum first element as default value. But when execute alter table add column, we should set OriginDefaultValue to enum's first element explicit. That makes me feel strange.

Yeah, this is an optimization. Add column can be done quickly.

Do we need add OriginDefaultValue when create table?

No

@blacktear23
Copy link
Contributor Author

@wjhuang2016 PTAL

Copy link
Member

@wjhuang2016 wjhuang2016 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 11, 2020
@bb7133
Copy link
Member

bb7133 commented Nov 11, 2020

LGTM

@ti-srebot ti-srebot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 11, 2020
@bb7133
Copy link
Member

bb7133 commented Nov 11, 2020

PTAL @xhebox @tangenta

@bb7133 bb7133 added the require-LGT3 Indicates that the PR requires three LGTM. label Nov 11, 2020
Copy link
Contributor

@xhebox xhebox left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Nov 11, 2020
@wjhuang2016
Copy link
Member

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 11, 2020
@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

@blacktear23 merge failed.

@blacktear23
Copy link
Contributor Author

/run-all-tests

@wjhuang2016
Copy link
Member

/run-all-tests

@wjhuang2016
Copy link
Member

/run-unit-test

@bb7133
Copy link
Member

bb7133 commented Nov 11, 2020

/merge

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot ti-srebot merged commit dd32482 into pingcap:master Nov 11, 2020
@ti-challenge-bot
Copy link

@blacktear23, Congratulations, you get 600 in this PR, and your total score is 600 in challenge program.

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Nov 12, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #20998

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Nov 12, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-3.0 in PR #20999

ti-srebot added a commit that referenced this pull request Nov 25, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
require-LGT3 Indicates that the PR requires three LGTM. sig/sql-infra SIG: SQL Infra status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The default value of a newly added enum column is set improperly
6 participants