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

restore: support restoring SQL wrapping with TiDB special comments #1287

Merged
merged 3 commits into from
Jul 30, 2021

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Jul 27, 2021

What problem does this PR solve?

TiDB Binlog and Lightning use regex to substitute the TiDB-specific keyword with special comments:

FeatureIDAutoRandom:   regexp.MustCompile(`(?P<REPLACE>(?i)AUTO_RANDOM\b\s*(\s*\(\s*\d+\s*\)\s*)?)`),
...
FeatureIDForceAutoInc: regexp.MustCompile(`(?P<REPLACE>(?i)FORCE)\b\s*AUTO_INCREMENT\s*`),

The regex is fragile because it is easy to get things wrong when the table names or column names contain these keywords, for example:

CREATE TABLE AUTO_RANDOM (id int);

is incorrectly rewritten to

CREATE TABLE `/*T![auto_rand] AUTO_RANDOM AUTO_RANDOM */ ` (id int);

Comparing with pattern matching, using ast.Restore to wrap special comments is not prone to error.

What is changed and how it works?

  • Collect TiDB-specific features into tidb/features.go as the single source of truth.
  • Add a restore flag: RestoreTiDBSpecialComment, support wrapping with TiDB special comment.
  • Remove SpecialCommentsController.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • Has exported function/method change
  • Has interface methods change

Side effects

NA

Related changes

  • Need to cherry-pick to the release branch

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jul 27, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • kennytm
  • tiancaiamao

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@tangenta tangenta requested review from tiancaiamao and kennytm July 27, 2021 09:14
@kennytm
Copy link
Contributor

kennytm commented Jul 27, 2021

what is the reason removing SpecialCommentsController and exposing the SpecialComments map directly 🤔

(i mean i don't really like to keep SpecialCommentsController, but allowing anyone to modify the global SpecialComments sounds prone to abuse)

FeatureIDForceAutoInc: {},
}

func CanParseFeature(fs ...string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The old code is more general, the parser is not specific to TiDB and you can register the feature to control it's behavior ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, since you are the original author of this code, if you thinks it's OK, I have no problem with it.

Copy link
Contributor Author

@tangenta tangenta Jul 28, 2021

Choose a reason for hiding this comment

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

the parser is not specific to TiDB

But the keywords(AUTO_RANDOM, CLUSTERED_INDEX...) are TiDB-specific. It is reasonable to attach these keywords to fixed feature IDs.

you can register the feature to control it's behavior

The register mechanism is not that convenient. If the user registers a new feature ID(/*T![new_feature_id] */), he must also modify the code in the parser to add a new keyword to wrap. If the user doesn't need the new keyword, he should use /*T! xxx */ instead.

@lance6716

This comment has been minimized.

@ti-chi-bot ti-chi-bot added status/LGT2 LGT2 and removed status/LGT1 LGT1 labels Jul 30, 2021
@kennytm
Copy link
Contributor

kennytm commented Jul 30, 2021

/merge

@ti-chi-bot
Copy link
Member

@tangenta: Your PR was out of date, I have automatically updated it for you.

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 ti-community-infra/tichi repository.

@kennytm
Copy link
Contributor

kennytm commented Jul 30, 2021

/merge cancel

@kennytm
Copy link
Contributor

kennytm commented Jul 30, 2021

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: d0b457f

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

Successfully merging this pull request may close these issues.

5 participants