-
Notifications
You must be signed in to change notification settings - Fork 307
sink/mysql: align DDL time defaults with origin_default #12490
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
Merged
ti-chi-bot
merged 25 commits into
pingcap:master
from
haiboumich:fix-handle-alter-add-default_current_timestamp
Jan 30, 2026
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
916b7db
feat: add comprehensive DDL timestamp handling for sink
haiboumich 275df5b
sink(ticdc): align MySQL DDL timestamp handling with upstream default…
haiboumich f8c9cd3
tiflow: gate DDL session timestamp on origin_default
haiboumich 8002d41
tiflow: update DDL timestamp handling
haiboumich ef3aeec
ticdc: revert log
haiboumich 50a83e9
ticdc: refine code
haiboumich d635098
ticdc: refine code
haiboumich caff434
ticdc: update log
haiboumich d837830
Removed //go:build intest and // +build intest
haiboumich 5ba109a
Made the targeted‑skip variant: we now ignore SET TIMESTAMP / reset f…
haiboumich e0b4f36
Added a MySQL‑only integration test that reproduces the ALTER TABLE …
haiboumich dc7a1ab
Revert "Made the targeted‑skip variant: we now ignore SET TIMESTAMP /…
haiboumich 67267c6
Merge branch 'pingcap:master' into fix-handle-alter-add-default_curre…
haiboumich 980163d
add more dml before and after adding column for integration test
haiboumich 29d6e56
added a pre-DDL SET TIMESTAMP = DEFAULT so every DDL starts from a cl…
haiboumich 83c5cb5
fix ut
haiboumich 20e2425
sink/mysql: make ddl timestamp leak test deterministic
haiboumich 952bafa
avoid system tz noise
haiboumich 3d6848b
deliberate no pre ddl reset
haiboumich ece8374
failpoint the right place
haiboumich 0dd9d3c
uncomment pre-ddl
haiboumich 6f42244
sink(ticdc): improve DDL timestamp failpoint coverage
haiboumich fff1040
sink(ticdc): add failpoint coverage for DDL timestamp reset
haiboumich 76a6e4b
chore: reorder
haiboumich 445aaae
sink(ticdc): guard DDL timestamp helper for nil table info
haiboumich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| // Copyright 2025 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 ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/failpoint" | ||
| "github.com/pingcap/log" | ||
| timodel "github.com/pingcap/tidb/pkg/meta/model" | ||
| "github.com/pingcap/tidb/pkg/parser" | ||
| "github.com/pingcap/tidb/pkg/parser/ast" | ||
| tidbmysql "github.com/pingcap/tidb/pkg/parser/mysql" | ||
| "github.com/pingcap/tiflow/cdc/model" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| func setSessionTimestamp(ctx context.Context, tx *sql.Tx, unixTimestamp float64) error { | ||
| _, err := tx.ExecContext(ctx, fmt.Sprintf("SET TIMESTAMP = %s", formatUnixTimestamp(unixTimestamp))) | ||
| return err | ||
| } | ||
|
|
||
| // resetSessionTimestamp clears session @@timestamp to prevent stale values from | ||
| // leaking across DDLs using the same session; it's a cheap safety net before | ||
| // and after DDL execution. | ||
| func resetSessionTimestamp(ctx context.Context, tx *sql.Tx) error { | ||
| _, err := tx.ExecContext(ctx, "SET TIMESTAMP = DEFAULT") | ||
| return err | ||
| } | ||
|
|
||
| func formatUnixTimestamp(unixTimestamp float64) string { | ||
| return strconv.FormatFloat(unixTimestamp, 'f', 6, 64) | ||
| } | ||
|
|
||
| func ddlSessionTimestampFromOriginDefault(ddl *model.DDLEvent, timezone string) (float64, bool) { | ||
| if ddl == nil || ddl.TableInfo == nil || ddl.TableInfo.TableInfo == nil { | ||
| return 0, false | ||
| } | ||
| targetColumns, err := extractCurrentTimestampDefaultColumns(ddl.Query) | ||
| if err != nil || len(targetColumns) == 0 { | ||
| return 0, false | ||
| } | ||
|
|
||
| for _, col := range ddl.TableInfo.Columns { | ||
| if col == nil { | ||
| continue | ||
| } | ||
| if _, ok := targetColumns[col.Name.L]; !ok { | ||
| continue | ||
| } | ||
| val := col.GetOriginDefaultValue() | ||
| valStr, ok := val.(string) | ||
| if !ok || valStr == "" { | ||
| continue | ||
| } | ||
| ts, err := parseOriginDefaultTimestamp(valStr, col, timezone) | ||
| if err != nil { | ||
| log.Warn("Failed to parse OriginDefaultValue for DDL timestamp", | ||
| zap.String("column", col.Name.O), | ||
| zap.String("originDefault", valStr), | ||
| zap.Error(err)) | ||
| continue | ||
| } | ||
| log.Info("Using OriginDefaultValue for DDL timestamp", | ||
| zap.String("column", col.Name.O), | ||
| zap.String("originDefault", valStr), | ||
| zap.Float64("timestamp", ts), | ||
| zap.String("timezone", timezone)) | ||
| return ts, true | ||
| } | ||
|
|
||
| return 0, false | ||
| } | ||
|
|
||
| func extractCurrentTimestampDefaultColumns(query string) (map[string]struct{}, error) { | ||
| p := parser.New() | ||
| stmt, err := p.ParseOneStmt(query, "", "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cols := make(map[string]struct{}) | ||
| switch s := stmt.(type) { | ||
| case *ast.CreateTableStmt: | ||
| for _, col := range s.Cols { | ||
| if hasCurrentTimestampDefault(col) { | ||
| cols[col.Name.Name.L] = struct{}{} | ||
| } | ||
| } | ||
| case *ast.AlterTableStmt: | ||
| for _, spec := range s.Specs { | ||
| switch spec.Tp { | ||
| case ast.AlterTableAddColumns, ast.AlterTableModifyColumn, ast.AlterTableChangeColumn, ast.AlterTableAlterColumn: | ||
| for _, col := range spec.NewColumns { | ||
| if hasCurrentTimestampDefault(col) { | ||
| cols[col.Name.Name.L] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return cols, nil | ||
| } | ||
|
|
||
| func hasCurrentTimestampDefault(col *ast.ColumnDef) bool { | ||
| if col == nil { | ||
| return false | ||
| } | ||
| for _, opt := range col.Options { | ||
| if opt.Tp != ast.ColumnOptionDefaultValue { | ||
| continue | ||
| } | ||
| if isCurrentTimestampExpr(opt.Expr) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isCurrentTimestampExpr(expr ast.ExprNode) bool { | ||
| if expr == nil { | ||
| return false | ||
| } | ||
| switch v := expr.(type) { | ||
| case *ast.FuncCallExpr: | ||
| return isCurrentTimestampFuncName(v.FnName.L) | ||
| case ast.ValueExpr: | ||
| return isCurrentTimestampFuncName(strings.ToLower(v.GetString())) | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func isCurrentTimestampFuncName(name string) bool { | ||
| switch name { | ||
| case ast.CurrentTimestamp, ast.Now, ast.LocalTime, ast.LocalTimestamp: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func parseOriginDefaultTimestamp(val string, col *timodel.ColumnInfo, timezone string) (float64, error) { | ||
| loc, err := resolveOriginDefaultLocation(col, timezone) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return parseTimestampInLocation(val, loc) | ||
| } | ||
|
|
||
| func resolveOriginDefaultLocation(col *timodel.ColumnInfo, timezone string) (*time.Location, error) { | ||
| if col != nil && col.GetType() == tidbmysql.TypeTimestamp && col.Version >= timodel.ColumnInfoVersion1 { | ||
| return time.UTC, nil | ||
| } | ||
| if timezone == "" { | ||
| return time.UTC, nil | ||
| } | ||
| tz := strings.Trim(timezone, "\"") | ||
| return time.LoadLocation(tz) | ||
| } | ||
|
|
||
| func parseTimestampInLocation(val string, loc *time.Location) (float64, error) { | ||
| formats := []string{ | ||
| "2006-01-02 15:04:05", | ||
| "2006-01-02 15:04:05.999999", | ||
| } | ||
| for _, f := range formats { | ||
| t, err := time.ParseInLocation(f, val, loc) | ||
| if err == nil { | ||
| return float64(t.UnixNano()) / float64(time.Second), nil | ||
| } | ||
| } | ||
| return 0, fmt.Errorf("failed to parse timestamp: %s", val) | ||
| } | ||
|
|
||
| func matchFailpointValue(val failpoint.Value, ddlQuery string) bool { | ||
| if val == nil { | ||
| return true | ||
| } | ||
| switch v := val.(type) { | ||
| case bool: | ||
| return v | ||
| case string: | ||
| if v == "" { | ||
| return true | ||
| } | ||
| return strings.Contains(strings.ToLower(ddlQuery), strings.ToLower(v)) | ||
| default: | ||
| return true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright 2025 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 ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMatchFailpointValue(t *testing.T) { | ||
| ddl := "ALTER TABLE t ADD COLUMN c2 int" | ||
| tests := []struct { | ||
| name string | ||
| val any | ||
| want bool | ||
| }{ | ||
| {name: "nil", val: nil, want: true}, | ||
| {name: "bool-true", val: true, want: true}, | ||
| {name: "bool-false", val: false, want: false}, | ||
| {name: "empty-string", val: "", want: true}, | ||
| {name: "match-string", val: "c2", want: true}, | ||
| {name: "match-string-case", val: "C2", want: true}, | ||
| {name: "no-match", val: "d2", want: false}, | ||
| {name: "unknown-type", val: 123, want: true}, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| require.Equal(t, tc.want, matchFailpointValue(tc.val, ddl)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please add useSessionTimestamp check.
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.
I think it might not be necessary?
The pre-DDL SET TIMESTAMP = DEFAULT is a defensive reset for pooled connections and protects against leaked session state from prior failures.
And useSessionTimestamp only reflects the current DDL’s needs, coupling them could leave leaks unaddressed.
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.
Ok, I think we can remove resetSessionTimestamp after the current DDL executes. Because every DDL will reset the sessionTimestamp.
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.
It looks like set the time zone at the session level, so the resetSessionTimestamp is not necessary?
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.
I think the resetSessionTimestamp might still be necessary and the pre-ddl one could be a safety net for any case where the previous DDL didn’t reset, and I think the cost is cheap as DDLs are low‑frequency and extra round‑trip is cost is negligible compared to the general metadata locks, schema change.
I did a failpiont test, if there is no reset timestamp, it may still leak to the following ddl added a debug failpoint test PR: #12496, which I did the following thing:
For the test scenario above, I run twice for wihout and with pre-ddl reset timestamp, and I got: