Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

parser: split ALTER TABLE ADD COLUMN #1340

Merged
merged 5 commits into from
Dec 17, 2020
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
21 changes: 21 additions & 0 deletions pkg/parser/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ func SplitDDL(stmt ast.StmtNode, schema string) (sqls []string, err error) {

v.Specs = []*ast.AlterTableSpec{spec}

// handle `alter table t1 add column (c1 int, c2 int)`
if spec.Tp == ast.AlterTableAddColumns && len(spec.NewColumns) > 1 {
columns := spec.NewColumns
spec.Position = &ast.ColumnPosition{
Tp: ast.ColumnPositionNone, // otherwise restore will become "alter table t1 add column (c1 int)"
}
for _, c := range columns {
spec.NewColumns = []*ast.ColumnDef{c}
bf.Reset()
err = stmt.Restore(ctx)
if err != nil {
v.Specs = specs
v.Table = table
return nil, terror.ErrRestoreASTNode.Delegate(err)
}
sqls = append(sqls, bf.String())
}
// we have restore SQL for every columns, skip below general restoring and continue on next spec
continue
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
}

bf.Reset()
err = stmt.Restore(ctx)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/parser/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var sqls = []string{
"alter table `t1` partition by list (a) (partition x default)",
"alter table `t1` partition by system_time (partition x history, partition y current)",
"alter database `test` charset utf8mb4",
"alter table `t1` add column (c1 int, c2 int)",
}

var nonDDLs = []string{
Expand Down Expand Up @@ -171,6 +172,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) {
{"ALTER TABLE `test`.`t1` PARTITION BY LIST (`a`) (PARTITION `x` DEFAULT)"},
{"ALTER TABLE `test`.`t1` PARTITION BY SYSTEM_TIME (PARTITION `x` HISTORY,PARTITION `y` CURRENT)"},
{"ALTER DATABASE `test` CHARACTER SET = utf8mb4"},
{"ALTER TABLE `test`.`t1` ADD COLUMN `c1` INT", "ALTER TABLE `test`.`t1` ADD COLUMN `c2` INT"},
}

expectedTableName := [][][]*filter.Table{
Expand Down Expand Up @@ -215,6 +217,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) {
{{genTableName("test", "t1")}},
{{genTableName("test", "t1")}},
{{genTableName("test", "")}},
{{genTableName("test", "t1")}, {genTableName("test", "t1")}},
}

targetTableNames := [][][]*filter.Table{
Expand Down Expand Up @@ -259,6 +262,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) {
{{genTableName("xtest", "xt1")}},
{{genTableName("xtest", "xt1")}},
{{genTableName("xtest", "")}},
{{genTableName("xtest", "t1")}, {genTableName("xtest", "t1")}},
}

targetSQLs := [][]string{
Expand Down Expand Up @@ -303,6 +307,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) {
{"ALTER TABLE `xtest`.`xt1` PARTITION BY LIST (`a`) (PARTITION `x` DEFAULT)"},
{"ALTER TABLE `xtest`.`xt1` PARTITION BY SYSTEM_TIME (PARTITION `x` HISTORY,PARTITION `y` CURRENT)"},
{"ALTER DATABASE `xtest` CHARACTER SET = utf8mb4"},
{"ALTER TABLE `xtest`.`t1` ADD COLUMN `c1` INT", "ALTER TABLE `xtest`.`t1` ADD COLUMN `c2` INT"},
}

for i, sql := range sqls {
Expand Down
6 changes: 2 additions & 4 deletions tests/sequence_safe_mode/data/db1.increment.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use sequence_safe_mode_test;
insert into t1 (uid, name) values (10003, 'Buenos Aires');
alter table t1 add column age int;
alter table t1 add column (age int, level int);
alter table t1 add index age(age);
alter table t1 add column level int;
alter table t1 add index level(level);
insert into t1 (uid, name, age) values (10005, 'Buenos Aires', 200);
insert into t2 (uid, name) values (20005, 'Aureliano José');
insert into t1 (uid, name, age) values (10006, 'Buenos Aires', 200);
alter table t2 add column age int;
alter table t2 add column (age int, level int);
alter table t2 add index age(age);
alter table t2 add column level int;
alter table t2 add index level(level);
insert into t1 (uid, name, age) values (10007, 'Buenos Aires', 300);
insert into t2 (uid, name, age) values (20006, 'Colonel Aureliano Buendía', 301);
Expand Down
6 changes: 2 additions & 4 deletions tests/sequence_safe_mode/data/db2.increment.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use sequence_safe_mode_test;
delete from t3 where name = 'Santa Sofía de la Piedad';
alter table t2 add column age int;
alter table t2 add column (age int, level int);
alter table t2 add index age(age);
alter table t2 add column level int;
alter table t2 add index level(level);
insert into t2 (uid, name, age) values (40002, 'Remedios Moscote', 100), (40003, 'Amaranta', 103);
insert into t3 (uid, name) values (30004, 'Aureliano José'), (30005, 'Santa Sofía de la Piedad'), (30006, '17 Aurelianos');
alter table t3 add column age int;
alter table t3 add column (age int, level int);
alter table t3 add index age(age);
alter table t3 add column level int;
alter table t3 add index level(level);
update t3 set age = 1;