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: support adding constraint with column in one spec #36020

Merged
merged 10 commits into from
Jul 8, 2022
17 changes: 13 additions & 4 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2997,11 +2997,20 @@ func needToOverwriteColCharset(options []*ast.TableOption) bool {
// `ALTER TABLE ADD COLUMN (c1 INT, c2 INT)` is split into
// `ALTER TABLE ADD COLUMN c1 INT, ADD COLUMN c2 INT`.
func resolveAlterTableAddColumns(spec *ast.AlterTableSpec) []*ast.AlterTableSpec {
Copy link
Contributor

Choose a reason for hiding this comment

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

the comment should be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As an example, it is still correct.

specs := make([]*ast.AlterTableSpec, len(spec.NewColumns))
for i, col := range spec.NewColumns {
specs := make([]*ast.AlterTableSpec, 0, len(spec.NewColumns)+len(spec.NewConstraints))
for _, col := range spec.NewColumns {
t := *spec
t.NewColumns = []*ast.ColumnDef{col}
specs[i] = &t
t.NewConstraints = []*ast.Constraint{}
specs = append(specs, &t)
}
for _, con := range spec.NewConstraints {
tangenta marked this conversation as resolved.
Show resolved Hide resolved
t := *spec
t.NewColumns = []*ast.ColumnDef{}
t.NewConstraints = []*ast.Constraint{}
t.Constraint = con
t.Tp = ast.AlterTableAddConstraint
specs = append(specs, &t)
}
return specs
}
Expand All @@ -3019,7 +3028,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
if isIgnorableSpec(spec.Tp) {
continue
}
if spec.Tp == ast.AlterTableAddColumns && len(spec.NewColumns) > 1 {
if spec.Tp == ast.AlterTableAddColumns && (len(spec.NewColumns) > 1 || len(spec.NewConstraints) > 0) {
validSpecs = append(validSpecs, resolveAlterTableAddColumns(spec)...)
} else {
validSpecs = append(validSpecs, spec)
Expand Down
20 changes: 19 additions & 1 deletion ddl/multi_schema_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestMultiSchemaChangeAddColumns(t *testing.T) {
tk.MustExec("alter table t add column b int default 2, add column c int default 3;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))

// Test add multiple columns in one spec.
// Test add multiple columns and constraints in one spec.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
Expand All @@ -55,6 +55,24 @@ func TestMultiSchemaChangeAddColumns(t *testing.T) {
tk.MustExec("alter table t add column (d int default 4, e int default 5);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4 5"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (index i(a), index i1(a));")
tk.MustQuery("select * from t use index (i, i1);").Check(testkit.Rows("1"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (b int default 2, index i(a), primary key (a));")
tk.MustQuery("select * from t use index (i, primary)").Check(testkit.Rows("1 2"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (index i(a));")
tk.MustQuery("select * from t use index (i)").Check(testkit.Rows("1"))
tangenta marked this conversation as resolved.
Show resolved Hide resolved

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1);")
tk.MustExec("insert into t values ();")
Expand Down