pingcap / parser

A MySQL Compatible SQL Parser
Apache License 2.0
1.41k stars 489 forks source link

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

Closed tangenta closed 3 years ago

tangenta commented 3 years ago

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?

Check List

Tests

Code changes

Side effects

NA

Related changes

ti-chi-bot commented 3 years ago

[REVIEW NOTIFICATION]

This pull request has been approved by:

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.
kennytm commented 3 years ago

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)

lance6716 commented 3 years ago

Thanks. TODO: DM needs to revert https://github.com/pingcap/dm/pull/1823 after update parser dependency

kennytm commented 3 years ago

/merge

ti-chi-bot commented 3 years ago

@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](https://prow.tidb.io/command-help). If you have questions or suggestions related to my behavior, please file an issue against the [ti-community-infra/tichi](https://github.com/ti-community-infra/tichi/issues/new?title=Prow%20issue:) repository.
kennytm commented 3 years ago

/merge cancel

kennytm commented 3 years ago

/merge

ti-chi-bot commented 3 years ago

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

Commit hash: d0b457fbbb0321dfe3a4fd228630ff03e2bea216