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: disallow modifying the generated expression of stored or indexed column #10932

Merged
merged 12 commits into from Jul 3, 2019
21 changes: 21 additions & 0 deletions ddl/db_test.go
Expand Up @@ -2811,6 +2811,27 @@ func (s *testDBSuite5) TestAddIndexForGeneratedColumn(c *C) {
s.tk.MustExec("admin check table gcai_table")
}

func (s *testDBSuite5) TestModifyIndexedGeneratedColumn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

// Test create an exist database
_, err := tk.Exec("CREATE database test")
c.Assert(err, NotNil)
tangenta marked this conversation as resolved.
Show resolved Hide resolved

tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1 (a int, b int as (a+1), index idx(b));")
tangenta marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("insert into t1 set a=1")
_, err = tk.Exec("alter table t1 modify column b int as (a+2)")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:3106]'modifying an indexed column' is not supported for generated columns.")

tk.MustExec("drop index idx on t1;")
tk.MustExec("alter table t1 modify b int as (a + 2);")
tangenta marked this conversation as resolved.
Show resolved Hide resolved

s.mustExec(c, "drop table t1;")
}

func (s *testDBSuite4) TestIssue9100(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
Expand Down
5 changes: 5 additions & 0 deletions ddl/ddl_api.go
Expand Up @@ -2705,6 +2705,11 @@ func (d *ddl) ModifyColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Al
if err := checkAutoIncrementRef(specNewColumn.Name.Name.L, dependColNames, t.Meta()); err != nil {
return errors.Trace(err)
}

// If there is an index on the generated column which we are trying to modify, return an error.
tangenta marked this conversation as resolved.
Show resolved Hide resolved
if tables.FindIndexByColName(t, specNewColumn.Name.Name.L) != nil {
tangenta marked this conversation as resolved.
Show resolved Hide resolved
return errUnsupportedOnGeneratedColumn.GenWithStackByArgs("modifying an indexed column")
}
}
}

Expand Down