Skip to content

Commit

Permalink
disallow non primary key to be clustered
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Mar 18, 2021
1 parent f68bbf7 commit 42e53ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,12 @@ func buildTableInfo(
tbInfo.Columns = append(tbInfo.Columns, hiddenCol)
tblColumns = append(tblColumns, table.ToColumn(hiddenCol))
}
// Check clustered on non-primary key.
if constr.Option != nil && constr.Option.PrimaryKeyTp != model.PrimaryKeyTypeDefault &&
constr.Tp != ast.ConstraintPrimaryKey {
msg := mysql.Message("CLUSTERED/NONCLUSTERED keyword is only supported for primary key", nil)
return nil, dbterror.ClassDDL.NewStdErr(errno.ErrUnsupportedDDLOperation, msg)
}
if constr.Tp == ast.ConstraintForeignKey {
for _, fk := range tbInfo.ForeignKeys {
if fk.Name.L == strings.ToLower(constr.Name) {
Expand Down
11 changes: 11 additions & 0 deletions session/clustered_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,17 @@ func (s *testClusteredSuite) TestClusteredIndexSyntax(c *C) {
assertPkType("create table t (a int, b varchar(255), primary key(b, a) /*T![clustered_index] nonclustered */);", nonClustered)
assertPkType("create table t (a int, b varchar(255), primary key(b, a) clustered);", clustered)
assertPkType("create table t (a int, b varchar(255), primary key(b, a) /*T![clustered_index] clustered */);", clustered)

tk.MustGetErrCode("create table t (a varchar(255) unique key clustered);", errno.ErrParse)
tk.MustGetErrCode("create table t (a varchar(255), foreign key (a) reference t1(a) clustered);", errno.ErrParse)
tk.MustGetErrCode("create table t (a varchar(255), foreign key (a) clustered reference t1(a));", errno.ErrParse)
tk.MustGetErrCode("create table t (a varchar(255) clustered);", errno.ErrParse)

errMsg := "[ddl:8200]CLUSTERED/NONCLUSTERED keyword is only supported for primary key"
tk.MustGetErrMsg("create table t (a varchar(255), unique key(a) clustered);", errMsg)
tk.MustGetErrMsg("create table t (a varchar(255), unique index(a) clustered);", errMsg)
tk.MustGetErrMsg("create table t (a varchar(255), key(a) clustered);", errMsg)
tk.MustGetErrMsg("create table t (a varchar(255), index(a) clustered);", errMsg)
}

func (s *testClusteredSerialSuite) TestPrefixClusteredIndexAddIndexAndRecover(c *C) {
Expand Down

0 comments on commit 42e53ae

Please sign in to comment.