-
Notifications
You must be signed in to change notification settings - Fork 41
[WIP] #4270
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
base: master
Are you sure you want to change the base?
[WIP] #4270
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package mysql | ||
|
|
||
| import ( | ||
| "github.com/pingcap/errors" | ||
| "github.com/pingcap/ticdc/pkg/common" | ||
| commonEvent "github.com/pingcap/ticdc/pkg/common/event" | ||
| timodel "github.com/pingcap/tidb/pkg/meta/model" | ||
| "github.com/pingcap/tidb/pkg/parser" | ||
| "github.com/pingcap/tidb/pkg/parser/ast" | ||
| ) | ||
|
|
||
| type indexKeyPart struct { | ||
| nameL string | ||
| length int | ||
| } | ||
|
|
||
| func restoreAnonymousIndexToNamedIndex(query string, tableInfo *common.TableInfo) (string, bool, error) { | ||
| if query == "" || tableInfo == nil { | ||
| return query, false, nil | ||
| } | ||
|
|
||
| p := parser.New() | ||
| stmt, err := p.ParseOneStmt(query, "", "") | ||
| if err != nil { | ||
| return query, false, errors.Trace(err) | ||
| } | ||
|
|
||
| alterStmt, ok := stmt.(*ast.AlterTableStmt) | ||
| if !ok { | ||
| return query, false, nil | ||
| } | ||
|
|
||
| changed := false | ||
| for _, spec := range alterStmt.Specs { | ||
| if spec == nil || spec.Tp != ast.AlterTableAddConstraint || spec.Constraint == nil { | ||
| continue | ||
| } | ||
| constraint := spec.Constraint | ||
| if constraint.Name != "" { | ||
| continue | ||
| } | ||
| if !isIndexConstraint(constraint) { | ||
| continue | ||
| } | ||
|
|
||
| indexName, ok := findIndexNameForConstraint(tableInfo, constraint) | ||
| if !ok { | ||
| continue | ||
| } | ||
| constraint.Name = indexName | ||
| changed = true | ||
| } | ||
|
|
||
| if !changed { | ||
| return query, false, nil | ||
| } | ||
|
|
||
| restoredQuery, err := commonEvent.Restore(stmt) | ||
| if err != nil { | ||
| return query, false, err | ||
| } | ||
| return restoredQuery, true, nil | ||
| } | ||
|
|
||
| func isIndexConstraint(constraint *ast.Constraint) bool { | ||
| if constraint == nil { | ||
| return false | ||
| } | ||
| switch constraint.Tp { | ||
| case ast.ConstraintKey, | ||
| ast.ConstraintIndex, | ||
| ast.ConstraintUniq, | ||
| ast.ConstraintUniqKey, | ||
| ast.ConstraintUniqIndex, | ||
| ast.ConstraintFulltext, | ||
| ast.ConstraintVector, | ||
| ast.ConstraintColumnar: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func isUniqueIndexConstraint(constraint *ast.Constraint) bool { | ||
| if constraint == nil { | ||
| return false | ||
| } | ||
| switch constraint.Tp { | ||
| case ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func findIndexNameForConstraint(tableInfo *common.TableInfo, constraint *ast.Constraint) (string, bool) { | ||
| keyParts, ok := getConstraintKeyParts(constraint) | ||
| if !ok { | ||
| return "", false | ||
| } | ||
|
|
||
| wantUnique := isUniqueIndexConstraint(constraint) | ||
| indices := tableInfo.GetIndices() | ||
| if len(indices) == 0 { | ||
| return "", false | ||
| } | ||
|
|
||
| matches := make([]*timodel.IndexInfo, 0, 1) | ||
| for _, index := range indices { | ||
| if index == nil || index.Primary { | ||
| continue | ||
| } | ||
| if index.Unique != wantUnique { | ||
| continue | ||
| } | ||
| // Only use non-public indices. | ||
| if index.State == timodel.StatePublic { | ||
| continue | ||
| } | ||
| if !indexColumnsMatchKeyParts(index.Columns, keyParts) { | ||
| continue | ||
| } | ||
| matches = append(matches, index) | ||
| } | ||
| if len(matches) != 1 { | ||
| return "", false | ||
| } | ||
| return matches[0].Name.O, true | ||
| } | ||
|
|
||
| func getConstraintKeyParts(constraint *ast.Constraint) ([]indexKeyPart, bool) { | ||
| if constraint == nil || len(constraint.Keys) == 0 { | ||
| return nil, false | ||
| } | ||
|
|
||
| parts := make([]indexKeyPart, 0, len(constraint.Keys)) | ||
| for _, key := range constraint.Keys { | ||
| if key == nil || key.Expr != nil || key.Column == nil { | ||
| return nil, false | ||
| } | ||
| parts = append(parts, indexKeyPart{ | ||
| nameL: key.Column.Name.L, | ||
| length: key.Length, | ||
| }) | ||
| } | ||
| return parts, true | ||
| } | ||
|
|
||
| func indexColumnsMatchKeyParts(indexColumns []*timodel.IndexColumn, keyParts []indexKeyPart) bool { | ||
| if len(indexColumns) != len(keyParts) { | ||
| return false | ||
| } | ||
| for i, part := range keyParts { | ||
| col := indexColumns[i] | ||
| if col == nil { | ||
| return false | ||
| } | ||
| if col.Name.L != part.nameL { | ||
| return false | ||
| } | ||
| if part.length > 0 { | ||
| if col.Length != part.length { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,21 @@ func (w *Writer) execDDL(event *commonEvent.DDLEvent) error { | |
| ctx := w.ctx | ||
| shouldSwitchDB := needSwitchDB(event) | ||
|
|
||
| if event.GetDDLType() == timodel.ActionAddIndex { | ||
| newQuery, changed, err := restoreAnonymousIndexToNamedIndex(event.Query, event.TableInfo) | ||
| if err != nil { | ||
| log.Warn("failed to restore anonymous index name", | ||
| zap.String("changefeed", w.ChangefeedID.String()), | ||
| zap.String("query", event.Query), | ||
| zap.Error(err)) | ||
|
Comment on lines
+60
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log message indicates a failure to restore the anonymous index name. It would be helpful to include the log.Warn("failed to restore anonymous index name",
zap.String("changefeed", w.ChangefeedID.String()),
zap.String("query", event.Query),
zap.Error(err)) |
||
| } else if changed { | ||
| log.Info("restore anonymous index to named index", | ||
| zap.String("changefeed", w.ChangefeedID.String()), | ||
| zap.String("query", event.Query), | ||
| zap.String("newQuery", newQuery)) | ||
|
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log message indicates a successful restoration of the anonymous index name. It would be helpful to include the log.Info("restore anonymous index to named index",
zap.String("changefeed", w.ChangefeedID.String()),
zap.String("query", event.Query),
zap.String("newQuery", newQuery)) |
||
| event.Query = newQuery | ||
| } | ||
| } | ||
| // Convert vector type to string type for unsupport database | ||
| if w.cfg.HasVectorType { | ||
| if newQuery := formatQuery(event.Query); newQuery != event.Query { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a brief comment explaining the purpose of this function, which is to restore anonymous index names to named indexes in DDL queries.