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

Fix handling of unsigned and zerofill in parser and normalization #10220

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ func (c *CreateTableEntity) normalizeColumnOptions() {
// doesn't mean anything anymore.
if _, ok := integralTypes[col.Type.Type]; ok {
col.Type.Length = nil
// Remove zerofill for integral types but keep the behavior that this marks the value
// as unsigned
if col.Type.Zerofill {
col.Type.Zerofill = false
col.Type.Unsigned = true
}
}

if _, ok := charsetTypes[col.Type.Type]; ok {
Expand Down
22 changes: 20 additions & 2 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func TestCreateTableDiff(t *testing.T) {
from: "create table t1 (id int primary key, `i` int not null default 0)",
to: "create table t2 (id int primary key, `i` bigint unsigned default null)",
diff: "alter table t1 modify column i bigint unsigned",
cdiff: "ALTER TABLE `t1` MODIFY COLUMN `i` bigint UNSIGNED",
cdiff: "ALTER TABLE `t1` MODIFY COLUMN `i` bigint unsigned",
},
{
name: "added column, dropped column, modified column",
from: "create table t1 (id int primary key, `i` int not null default 0, c char(3) default '')",
to: "create table t2 (id int primary key, ts timestamp null, `i` bigint unsigned default null)",
diff: "alter table t1 drop column c, modify column i bigint unsigned, add column ts timestamp null after id",
cdiff: "ALTER TABLE `t1` DROP COLUMN `c`, MODIFY COLUMN `i` bigint UNSIGNED, ADD COLUMN `ts` timestamp NULL AFTER `id`",
cdiff: "ALTER TABLE `t1` DROP COLUMN `c`, MODIFY COLUMN `i` bigint unsigned, ADD COLUMN `ts` timestamp NULL AFTER `id`",
},
// columns, reordering
{
Expand Down Expand Up @@ -659,6 +659,13 @@ func TestCreateTableDiff(t *testing.T) {
diff: "alter table t modify column t1 varchar(128) not null, modify column t2 varchar(128) not null, modify column t3 tinytext, charset utf8mb4",
cdiff: "ALTER TABLE `t` MODIFY COLUMN `t1` varchar(128) NOT NULL, MODIFY COLUMN `t2` varchar(128) NOT NULL, MODIFY COLUMN `t3` tinytext, CHARSET utf8mb4",
},
{
name: "normalized unsigned attribute",
from: "create table t1 (id int primary key)",
to: "create table t1 (id int unsigned primary key)",
diff: "alter table t1 modify column id int unsigned primary key",
cdiff: "ALTER TABLE `t1` MODIFY COLUMN `id` int unsigned PRIMARY KEY",
},
{
name: "normalized ENGINE InnoDB value",
from: "create table t1 (id int primary key) character set=utf8",
Expand Down Expand Up @@ -925,6 +932,12 @@ func TestNormalize(t *testing.T) {
from: "create table t (id int primary key, i int default null)",
to: "CREATE TABLE `t` (\n\t`id` int PRIMARY KEY,\n\t`i` int\n)",
},
{
name: "keeps not exist",
from: "create table if not exists t (id int primary key, i int)",
to: "CREATE TABLE IF NOT EXISTS `t` (\n\t`id` int PRIMARY KEY,\n\t`i` int\n)",
},

{
name: "timestamp null",
from: "create table t (id int primary key, t timestamp null)",
Expand All @@ -950,6 +963,11 @@ func TestNormalize(t *testing.T) {
from: "create table t (id int primary key, i int(11) default null)",
to: "CREATE TABLE `t` (\n\t`id` int PRIMARY KEY,\n\t`i` int\n)",
},
{
name: "removes zerofill and maps to unsigned",
from: "create table t (id int primary key, i int zerofill default null)",
to: "CREATE TABLE `t` (\n\t`id` int PRIMARY KEY,\n\t`i` int unsigned\n)",
},
{
name: "removes int sizes case insensitive",
from: "create table t (id int primary key, i INT(11) default null)",
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,10 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
}

if ct.Unsigned {
buf.astPrintf(ct, " %s", keywordStrings[UNSIGNED])
buf.astPrintf(ct, " %#s", keywordStrings[UNSIGNED])
}
if ct.Zerofill {
buf.astPrintf(ct, " %s", keywordStrings[ZEROFILL])
buf.astPrintf(ct, " %#s", keywordStrings[ZEROFILL])
}
if ct.Charset != "" {
buf.astPrintf(ct, " %s %s %#s", keywordStrings[CHARACTER], keywordStrings[SET], ct.Charset)
Expand Down
10 changes: 9 additions & 1 deletion go/vt/sqlparser/tracked_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func TestCanonicalOutput(t *testing.T) {
"create table `a`(`id` int, primary key(`id`))",
"CREATE TABLE `a` (\n\t`id` int,\n\tPRIMARY KEY (`id`)\n)",
},
{
"create table `a`(`id` int unsigned, primary key(`id`))",
"CREATE TABLE `a` (\n\t`id` int unsigned,\n\tPRIMARY KEY (`id`)\n)",
},
{
"create table `a`(`id` int zerofill, primary key(`id`))",
"CREATE TABLE `a` (\n\t`id` int zerofill,\n\tPRIMARY KEY (`id`)\n)",
},
{
"create table `a`(`id` int primary key)",
"CREATE TABLE `a` (\n\t`id` int PRIMARY KEY\n)",
Expand Down Expand Up @@ -110,7 +118,7 @@ func TestCanonicalOutput(t *testing.T) {
},
{
"alter table t2 modify column id bigint unsigned primary key",
"ALTER TABLE `t2` MODIFY COLUMN `id` bigint UNSIGNED PRIMARY KEY",
"ALTER TABLE `t2` MODIFY COLUMN `id` bigint unsigned PRIMARY KEY",
},
{
"alter table t1 modify column a int first, modify column b int after a",
Expand Down