Skip to content

Commit

Permalink
ddl: improve priority of suppressErrorTooLongKeyKey for DM (#55164)
Browse files Browse the repository at this point in the history
close #55138
  • Loading branch information
lance6716 authored Aug 9, 2024
1 parent 29122de commit 5d867dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
14 changes: 9 additions & 5 deletions pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ var (
SuppressErrorTooLongKeyKey stringutil.StringerStr = "suppressErrorTooLongKeyKey"
)

func suppressErrorTooLongKeyKey(sctx sessionctx.Context) bool {
func suppressErrorTooLongKeyForSchemaTracker(sctx sessionctx.Context) bool {
if sctx == nil {
return false
}
if suppress, ok := sctx.Value(SuppressErrorTooLongKeyKey).(bool); ok && suppress {
return true
}
Expand Down Expand Up @@ -130,11 +133,12 @@ func buildIndexColumns(ctx sessionctx.Context, columns []*model.ColumnInfo, inde
}
sumLength += indexColumnLength

// The sum of all lengths must be shorter than the max length for prefix.
if sumLength > maxIndexLength {
if !suppressErrorTooLongKeyForSchemaTracker(ctx) && sumLength > maxIndexLength {
// The sum of all lengths must be shorter than the max length for prefix.

// The multiple column index and the unique index in which the length sum exceeds the maximum size
// will return an error instead produce a warning.
if ctx == nil || (ctx.GetSessionVars().SQLMode.HasStrictMode() && !suppressErrorTooLongKeyKey(ctx)) || mysql.HasUniKeyFlag(col.GetFlag()) || len(indexPartSpecifications) > 1 {
if ctx == nil || ctx.GetSessionVars().SQLMode.HasStrictMode() || mysql.HasUniKeyFlag(col.GetFlag()) || len(indexPartSpecifications) > 1 {
return nil, false, dbterror.ErrTooLongKey.GenWithStackByArgs(sumLength, maxIndexLength)
}
// truncate index length and produce warning message in non-restrict sql mode.
Expand Down Expand Up @@ -261,7 +265,7 @@ func checkIndexColumn(ctx sessionctx.Context, col *model.ColumnInfo, indexColumn
// Specified length must be shorter than the max length for prefix.
maxIndexLength := config.GetGlobalConfig().MaxIndexLength
if indexColumnLen > maxIndexLength {
if ctx == nil || (ctx.GetSessionVars().SQLMode.HasStrictMode() && !suppressErrorTooLongKeyKey(ctx)) {
if ctx == nil || (ctx.GetSessionVars().SQLMode.HasStrictMode() && !suppressErrorTooLongKeyForSchemaTracker(ctx)) {
// return error in strict sql mode
return dbterror.ErrTooLongKey.GenWithStackByArgs(indexColumnLen, maxIndexLength)
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/ddl/schematracker/dm_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ import (
"github.com/pingcap/tidb/pkg/util/dbterror"
)

// SchemaTracker is used to track schema changes by DM. It implements DDL interface and by applying DDL, it updates the
// table structure to keep tracked with upstream changes.
// It embeds an InfoStore which stores DBInfo and TableInfo. The DBInfo and TableInfo can be treated as immutable, so
// after reading them by SchemaByName or TableByName, later modifications made by SchemaTracker will not change them.
// SchemaTracker is not thread-safe.
// SchemaTracker is used to track schema changes by DM. It implements
// ddl.Executor interface and by applying DDL, it updates the table structure to
// keep tracked with upstream changes.
//
// It embeds an InfoStore which stores DBInfo and TableInfo. The DBInfo and
// TableInfo can be treated as immutable, so after reading them by SchemaByName
// or TableByName, later modifications made by SchemaTracker will not change
// them. SchemaTracker is not thread-safe.
type SchemaTracker struct {
*InfoStore
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ddl/schematracker/dm_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func TestCreateTableLongIndex(t *testing.T) {
tracker := schematracker.NewSchemaTracker(2)
tracker.CreateTestDB(nil)
execCreate(t, tracker, sql)
sql2 := "create table test.t2 (c1 int, c2 blob, c3 varchar(64), unique index idx_c2(c2(555555)));"
execCreate(t, tracker, sql2)
sql3 := "create table test.t3 (c1 int, c2 blob, c3 varchar(64), index idx_c2_c3(c3, c2(555555)));"
execCreate(t, tracker, sql3)
}

func execAlter(t *testing.T, tracker schematracker.SchemaTracker, sql string) {
Expand Down

0 comments on commit 5d867dd

Please sign in to comment.