From 42e53aeaca70ff5b6036092e0be039455ecb154a Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 18 Mar 2021 14:32:53 +0800 Subject: [PATCH] disallow non primary key to be clustered --- ddl/ddl_api.go | 6 ++++++ session/clustered_index_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 6616422d6feee..57a8f203b6575 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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) { diff --git a/session/clustered_index_test.go b/session/clustered_index_test.go index 2df4e40143ad3..28be2c0ec50a5 100644 --- a/session/clustered_index_test.go +++ b/session/clustered_index_test.go @@ -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) {