From bfc96821f185bb666900528038ddbd5d831bbd14 Mon Sep 17 00:00:00 2001 From: TonsnakeLin Date: Wed, 10 Nov 2021 14:05:45 +0800 Subject: [PATCH 1/2] ddl: Do not consider the clustered index when checking the length of the secondary index --- ddl/db_integration_test.go | 11 +++--- ddl/ddl_api.go | 69 ++++++-------------------------------- ddl/index.go | 4 +-- 3 files changed, 18 insertions(+), 66 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index bb045d6ff80b0..b6f1106ad2fca 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -1530,7 +1530,7 @@ func (s *testSerialDBSuite1) TestCreateSecondaryIndexInCluster(c *C) { tk.MustExec("use test") // test create table with non-unique key - tk.MustGetErrCode(` + tk.MustExec(` CREATE TABLE t ( c01 varchar(255) NOT NULL, c02 varchar(255) NOT NULL, @@ -1540,7 +1540,8 @@ CREATE TABLE t ( c06 varchar(255) DEFAULT NULL, PRIMARY KEY (c01,c02,c03) clustered, KEY c04 (c04) -)`, errno.ErrTooLongKey) +)`) + tk.MustExec("drop table t") // test create long clustered primary key. tk.MustGetErrCode(` @@ -1580,7 +1581,7 @@ CREATE TABLE t ( PRIMARY KEY (c01,c02) clustered )`) tk.MustExec("create index idx1 on t(c03)") - tk.MustGetErrCode("create index idx2 on t(c03, c04)", errno.ErrTooLongKey) + tk.MustExec("create index idx2 on t(c03, c04)") tk.MustExec("create unique index uk2 on t(c03, c04)") tk.MustExec("drop table t") @@ -1599,9 +1600,9 @@ CREATE TABLE t ( )`) tk.MustExec("alter table t change c03 c10 varchar(256) default null") tk.MustGetErrCode("alter table t change c10 c100 varchar(1024) default null", errno.ErrTooLongKey) - tk.MustGetErrCode("alter table t modify c10 varchar(600) default null", errno.ErrTooLongKey) + tk.MustExec("alter table t modify c10 varchar(600) default null") tk.MustExec("alter table t modify c06 varchar(600) default null") - tk.MustGetErrCode("alter table t modify c01 varchar(510)", errno.ErrTooLongKey) + tk.MustExec("alter table t modify c01 varchar(510)") tk.MustExec("create table t2 like t") } diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 4b47459203343..63f1c5afbdc61 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1483,27 +1483,7 @@ func buildTableInfo( idxInfo.ID = allocateIndexID(tbInfo) tbInfo.Indices = append(tbInfo.Indices, idxInfo) } - if tbInfo.IsCommonHandle { - // Ensure tblInfo's each non-unique secondary-index's len + primary-key's len <= MaxIndexLength for clustered index table. - var pkLen, idxLen int - pkLen, err = indexColumnsLen(tbInfo.Columns, tables.FindPrimaryIndex(tbInfo).Columns) - if err != nil { - return - } - for _, idx := range tbInfo.Indices { - if idx.Unique { - // Only need check for non-unique secondary-index. - continue - } - idxLen, err = indexColumnsLen(tbInfo.Columns, idx.Columns) - if err != nil { - return - } - if pkLen+idxLen > config.GetGlobalConfig().MaxIndexLength { - return nil, errTooLongKey.GenWithStackByArgs(config.GetGlobalConfig().MaxIndexLength) - } - } - } + return } @@ -4124,40 +4104,30 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol } pkIndex := tables.FindPrimaryIndex(tbInfo) - var clusteredPkLen int - if tbInfo.IsCommonHandle { - var err error - clusteredPkLen, err = indexColumnsLen(columns, pkIndex.Columns) - if err != nil { - return err - } - } - checkOneIndex := func(indexInfo *model.IndexInfo, pkLenAppendToKey int, skipCheckIfNotModify bool) (modified bool, err error) { + checkOneIndex := func(indexInfo *model.IndexInfo) (modified bool, err error) { for _, col := range indexInfo.Columns { if col.Name.L == originalCol.Name.L { modified = true break } } - if skipCheckIfNotModify && !modified { + if !modified { return } err = checkIndexInModifiableColumns(columns, indexInfo.Columns) if err != nil { return } - err = checkIndexPrefixLength(columns, indexInfo.Columns, pkLenAppendToKey) + err = checkIndexPrefixLength(columns, indexInfo.Columns) return } // Check primary key first and get "does primary key's column has be modified?" info. - var ( - pkModified bool - err error - ) + var err error + if pkIndex != nil { - pkModified, err = checkOneIndex(pkIndex, 0, true) + _, err = checkOneIndex(pkIndex) if err != nil { return err } @@ -4168,12 +4138,9 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol if indexInfo.Primary { continue } - var pkLenAppendToKey int - if !indexInfo.Unique { - pkLenAppendToKey = clusteredPkLen - } - - _, err = checkOneIndex(indexInfo, pkLenAppendToKey, !tbInfo.IsCommonHandle || !pkModified) + // the second param should always be set to true, check index length only if it was modified + // checkOneIndex needs one param only. + _, err = checkOneIndex(indexInfo) if err != nil { return err } @@ -5302,22 +5269,6 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde return errors.Trace(err) } - if !unique && tblInfo.IsCommonHandle { - // Ensure new created non-unique secondary-index's len + primary-key's len <= MaxIndexLength in clustered index table. - var pkLen, idxLen int - pkLen, err = indexColumnsLen(tblInfo.Columns, tables.FindPrimaryIndex(tblInfo).Columns) - if err != nil { - return err - } - idxLen, err = indexColumnsLen(finalColumns, indexColumns) - if err != nil { - return err - } - if pkLen+idxLen > config.GetGlobalConfig().MaxIndexLength { - return errTooLongKey.GenWithStackByArgs(config.GetGlobalConfig().MaxIndexLength) - } - } - global := false if unique && tblInfo.GetPartitionInfo() != nil { ck, err := checkPartitionKeysConstraint(tblInfo.GetPartitionInfo(), indexColumns, tblInfo) diff --git a/ddl/index.go b/ddl/index.go index 8585487af10a0..e79b07af523db 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -110,12 +110,12 @@ func checkPKOnGeneratedColumn(tblInfo *model.TableInfo, indexPartSpecifications return lastCol, nil } -func checkIndexPrefixLength(columns []*model.ColumnInfo, idxColumns []*model.IndexColumn, pkLenAppendToKey int) error { +func checkIndexPrefixLength(columns []*model.ColumnInfo, idxColumns []*model.IndexColumn) error { idxLen, err := indexColumnsLen(columns, idxColumns) if err != nil { return err } - if idxLen+pkLenAppendToKey > config.GetGlobalConfig().MaxIndexLength { + if idxLen > config.GetGlobalConfig().MaxIndexLength { return errTooLongKey.GenWithStackByArgs(config.GetGlobalConfig().MaxIndexLength) } return nil From 68a32a059dbdc05b7ffded591d7802d901d0ea89 Mon Sep 17 00:00:00 2001 From: TonsnakeLin Date: Mon, 22 Nov 2021 09:44:52 +0800 Subject: [PATCH 2/2] ddl: Do not consider the clustered index when checking the length of the secondary index(#29660) close #29658 --- ddl/ddl_api.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 7e3ab0ed003bb..e7408b34abd7d 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -4237,9 +4237,6 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, sctx sessionctx.Contex // Index has a max-prefix-length constraint. eg: a varchar(100), index idx(a), modifying column a to a varchar(4000) // will cause index idx to break the max-prefix-length constraint. // -// For clustered index: -// Change column in pk need recheck all non-unique index, new pk len + index len < maxIndexLength. -// Change column in secondary only need related index, pk len + new index len < maxIndexLength. func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol *model.ColumnInfo) error { columns := make([]*model.ColumnInfo, 0, len(tbInfo.Columns)) columns = append(columns, tbInfo.Columns...) @@ -4255,7 +4252,8 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol pkIndex := tables.FindPrimaryIndex(tbInfo) - checkOneIndex := func(indexInfo *model.IndexInfo) (modified bool, err error) { + checkOneIndex := func(indexInfo *model.IndexInfo) (err error) { + var modified bool for _, col := range indexInfo.Columns { if col.Name.L == originalCol.Name.L { modified = true @@ -4273,11 +4271,11 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol return } - // Check primary key first and get "does primary key's column has be modified?" info. + // Check primary key first. var err error if pkIndex != nil { - _, err = checkOneIndex(pkIndex) + err = checkOneIndex(pkIndex) if err != nil { return err } @@ -4290,7 +4288,7 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol } // the second param should always be set to true, check index length only if it was modified // checkOneIndex needs one param only. - _, err = checkOneIndex(indexInfo) + err = checkOneIndex(indexInfo) if err != nil { return err }