Skip to content

Conversation

@haiboumich
Copy link

@haiboumich haiboumich commented Jan 13, 2026

What problem does this PR solve?

DDL statements that add columns with CURRENT_TIMESTAMP-style defaults can produce different initial values downstream if the DDL is applied later or in a different time zone. This PR ensures TiCDC uses the upstream-evaluated default when executing those DDLs.

Issue Number: close #4007

What is changed and how it works?

  • Parse DDL statements to detect CURRENT_TIMESTAMP/NOW/LOCALTIME/LOCALTIMESTAMP defaults.
  • Use the column origin_default from TableInfo to set session @@timestamp before executing the DDL, then reset it.
  • Move DDL timestamp helper functions into pkg/sink/mysql/helper.go for reuse and keep mysql_writer_ddl.go focused.
  • Add unit tests to cover the conditional session timestamp behavior.

Check List

Tests

  • Unit test
  • Manual test
    -- create changefeed
    CREATE DATABASE IF NOT EXISTS test_time_funcs;
    USE test_time_funcs;
    -- Timestamp & Datetime Functions
    -- Test: NOW(), CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP, LOCALTIME(), LOCALTIME, LOCALTIMESTAMP, LOCALTIMESTAMP(), UTC_TIMESTAMP()
    CREATE TABLE t_timestamp (id INT PRIMARY KEY, info VARCHAR(20));
    INSERT INTO t_timestamp VALUES (1, 'initial_row');
    -- pause the changefeed
    ALTER TABLE t_timestamp ADD COLUMN ts_now DATETIME DEFAULT NOW();
    ALTER TABLE t_timestamp ADD COLUMN ts_current_timestamp_func TIMESTAMP DEFAULT CURRENT_TIMESTAMP();
    ALTER TABLE t_timestamp ADD COLUMN ts_current_timestamp_const TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
    ALTER TABLE t_timestamp ADD COLUMN ts_localtime_func TIMESTAMP DEFAULT LOCALTIME();
    ALTER TABLE t_timestamp ADD COLUMN ts_localtime_const TIMESTAMP DEFAULT LOCALTIME;
    ALTER TABLE t_timestamp ADD COLUMN ts_localtimestamp_func TIMESTAMP DEFAULT LOCALTIMESTAMP();
    ALTER TABLE t_timestamp ADD COLUMN ts_localtimestamp_const TIMESTAMP DEFAULT LOCALTIMESTAMP;
    SELECT * FROM test_time_funcs.t_timestamp\G
    -- wait for seconds & resume the changefeed
    SELECT * FROM test_time_funcs.t_timestamp\G

Questions

Will it cause performance regression or break compatibility?

No. The change only affects DDL execution for CURRENT_TIMESTAMP-style defaults and does not alter DML paths.

Do you need to update user documentation, design documentation or monitoring documentation?

No.

Release note

Fix MySQL sink DDL execution to apply CURRENT_TIMESTAMP-style defaults using upstream origin_default values for consistent initial data.

haiboumich and others added 9 commits December 5, 2025 00:54
  - Add StartTs field to DDLEvent struct for preserving upstream execution timestamps
  - Update MySQL sink to set session timestamps based on upstream StartTs/CommitTs
  - Add proper session timestamp management with fallback logic
  - Enhance error handling for timestamp-related failures
  - Fix timestamp drift issues for DDL statements with NOW(), CURRENT_TIMESTAMP(), etc.
When executing DDLs, check for  in the table info and use it to set the session . This ensures columns with  are initialized with the exact same time as the upstream.
@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jan 13, 2026
@pingcap-cla-assistant
Copy link

pingcap-cla-assistant bot commented Jan 13, 2026

CLA assistant check
All committers have signed the CLA.

@ti-chi-bot ti-chi-bot bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 13, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @haiboumich, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the MySQL sink's DDL execution mechanism to maintain data consistency when dealing with columns that have CURRENT_TIMESTAMP-style default values. By programmatically aligning the session timestamp with the original upstream DDL execution time for such columns, it prevents discrepancies in initial data values that could arise from different application times or timezones, ensuring that the replicated schema accurately reflects the source.

Highlights

  • Problem Solved: Addresses data inconsistency for DDLs adding columns with CURRENT_TIMESTAMP-style defaults by ensuring the downstream system uses the upstream-evaluated default value.
  • DDL Execution Alignment: Implements logic to parse DDL statements, detect columns with CURRENT_TIMESTAMP, NOW, LOCALTIME, or LOCALTIMESTAMP defaults, and temporarily set the MySQL session @@timestamp to the column's origin_default value before execution.
  • Code Refactoring: Moves DDL timestamp-related helper functions into pkg/sink/mysql/helper.go to improve modularity and keep mysql_writer_ddl.go focused.
  • Testing: Adds comprehensive unit tests to validate the conditional session timestamp behavior during DDL execution.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to align CURRENT_TIMESTAMP-style default values in DDLs with the upstream values by setting the session @@timestamp. The logic is well-encapsulated in pkg/sink/mysql/helper.go, and new unit tests are added to verify the behavior. The changes are well-structured and improve data consistency. My review identified a few minor logging issues where the wrong error variable was being used in error logs, which could hinder debugging.

zap.String("query", event.GetDDLQuery()),
zap.Error(err))
if rbErr := tx.Rollback(); rbErr != nil {
log.Error("Failed to rollback", zap.Error(err))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The wrong error variable err is used here for logging. It should be rbErr to log the actual rollback error.

Suggested change
log.Error("Failed to rollback", zap.Error(err))
log.Error("Failed to rollback", zap.Error(rbErr))

}
}
if rbErr := tx.Rollback(); rbErr != nil {
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(err))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The wrong error variable err is used here for logging. It should be rbErr to log the actual rollback error.

Suggested change
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(err))
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(rbErr))

if err := resetSessionTimestamp(ctx, tx); err != nil {
log.Error("Failed to reset session timestamp after DDL execution", zap.Error(err))
if rbErr := tx.Rollback(); rbErr != nil {
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(err))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The wrong error variable err is used here for logging. It should be rbErr to log the actual rollback error.

Suggested change
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(err))
log.Error("Failed to rollback", zap.String("sql", event.GetDDLQuery()), zap.Error(rbErr))

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 14, 2026

@Debra-He: adding LGTM is restricted to approvers and reviewers in OWNERS files.

Details

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 14, 2026

@zier-one: adding LGTM is restricted to approvers and reviewers in OWNERS files.

Details

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 14, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Debra-He, zier-one
Once this PR has been reviewed and has the lgtm label, please assign kennytm for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@haiboumich
Copy link
Author

/retest-required

@haiboumich
Copy link
Author

/run-check-issue-triage-complete

@haiboumich
Copy link
Author

/retest-required

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 15, 2026

@haiboumich: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-error-log-review c5c2b30 link true /test pull-error-log-review

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@wk989898
Copy link
Collaborator

Please add unit tests and integration tests 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Limitation] TiCDC can't not handle DDL as alter table xx add column xx datetime default current_timestamp properly

4 participants