Skip to content

Commit

Permalink
ddl: improve priority of suppressErrorTooLongKeyKey for DM (#55164) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Nov 5, 2024
1 parent d4297f2 commit c517012
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
23 changes: 19 additions & 4 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/logutil"
decoder "github.com/pingcap/tidb/util/rowDecoder"
"github.com/pingcap/tidb/util/stringutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikv"
Expand All @@ -72,8 +73,20 @@ const (

var (
telemetryAddIndexIngestUsage = metrics.TelemetryAddIndexIngestCnt
// SuppressErrorTooLongKeyKey is used by SchemaTracker to suppress err too long key error
SuppressErrorTooLongKeyKey stringutil.StringerStr = "suppressErrorTooLongKeyKey"
)

func suppressErrorTooLongKeyForSchemaTracker(sctx sessionctx.Context) bool {
if sctx == nil {
return false
}
if suppress, ok := sctx.Value(SuppressErrorTooLongKeyKey).(bool); ok && suppress {
return true
}
return false
}

func buildIndexColumns(ctx sessionctx.Context, columns []*model.ColumnInfo, indexPartSpecifications []*ast.IndexPartSpecification) ([]*model.IndexColumn, bool, error) {
// Build offsets.
idxParts := make([]*model.IndexColumn, 0, len(indexPartSpecifications))
Expand Down Expand Up @@ -105,7 +118,7 @@ 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 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().StrictSQLMode || mysql.HasUniKeyFlag(col.GetFlag()) || len(indexPartSpecifications) > 1 {
Expand Down Expand Up @@ -217,9 +230,11 @@ 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 && (ctx == nil || ctx.GetSessionVars().StrictSQLMode) {
// return error in strict sql mode
return dbterror.ErrTooLongKey.GenWithStackByArgs(indexColumnLen, maxIndexLength)
if indexColumnLen > maxIndexLength {
if ctx == nil || (ctx.GetSessionVars().StrictSQLMode && !suppressErrorTooLongKeyForSchemaTracker(ctx)) {
// return error in strict sql mode
return dbterror.ErrTooLongKey.GenWithStackByArgs(indexColumnLen, maxIndexLength)
}
}
return nil
}
Expand Down
20 changes: 10 additions & 10 deletions ddl/schematracker/dm_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ import (
"github.com/pingcap/tidb/util/dbterror"
)

var _ ddl.DDL = SchemaTracker{}

// 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
// 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 Expand Up @@ -182,13 +183,12 @@ func (d SchemaTracker) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStm
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(ident.Schema)
}
// suppress ErrTooLongKey
strictSQLModeBackup := ctx.GetSessionVars().StrictSQLMode
ctx.GetSessionVars().StrictSQLMode = false
ctx.SetValue(ddl.SuppressErrorTooLongKeyKey, true)
// support drop PK
enableClusteredIndexBackup := ctx.GetSessionVars().EnableClusteredIndex
ctx.GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeOff
defer func() {
ctx.GetSessionVars().StrictSQLMode = strictSQLModeBackup
ctx.ClearValue(ddl.SuppressErrorTooLongKeyKey)
ctx.GetSessionVars().EnableClusteredIndex = enableClusteredIndexBackup
}()

Expand Down
4 changes: 4 additions & 0 deletions 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()
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 c517012

Please sign in to comment.