Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: improve priority of suppressErrorTooLongKeyKey for DM (#55164) #55772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -65,8 +66,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, error) {
// Build offsets.
idxParts := make([]*model.IndexColumn, 0, len(indexPartSpecifications))
Expand All @@ -91,7 +104,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 @@ -203,9 +216,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(maxIndexLength)
if indexColumnLen > maxIndexLength {
if ctx == nil || (ctx.GetSessionVars().StrictSQLMode && !suppressErrorTooLongKeyForSchemaTracker(ctx)) {
// return error in strict sql mode
return dbterror.ErrTooLongKey.GenWithStackByArgs(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
Loading