Skip to content

Commit

Permalink
types: fix clustered keyword to hint regexp (#23247)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Mar 11, 2021
1 parent b298b86 commit bc572f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
18 changes: 14 additions & 4 deletions sessionctx/binloginfo/binloginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func init() {
// shared by all sessions.
var pumpsClient *pumpcli.PumpsClient
var pumpsClientLock sync.RWMutex
var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+\s*`)
var preSplitPat = regexp.MustCompile(`PRE_SPLIT_REGIONS\s*=\s*\d+\s*`)
var shardPat = regexp.MustCompile(`(?P<REPLACE>SHARD_ROW_ID_BITS\s*=\s*\d+\s*)`)
var preSplitPat = regexp.MustCompile(`(?P<REPLACE>PRE_SPLIT_REGIONS\s*=\s*\d+\s*)`)

// BinlogInfo contains binlog data and binlog client.
type BinlogInfo struct {
Expand Down Expand Up @@ -300,7 +300,7 @@ func SetDDLBinlog(client *pumpcli.PumpsClient, txn kv.Transaction, jobID int64,
const specialPrefix = `/*T! `

// AddSpecialComment uses to add comment for table option in DDL query.
// Export for testing.
// Used by pingcap/ticdc.
func AddSpecialComment(ddlQuery string) string {
if strings.Contains(ddlQuery, specialPrefix) || strings.Contains(ddlQuery, driver.SpecialCommentVersionPrefix) {
return ddlQuery
Expand All @@ -320,7 +320,17 @@ func addSpecialCommentByRegexps(ddlQuery string, prefix string, regs ...*regexp.
minIdx := math.MaxInt64
for i := 0; i < len(regs); {
reg := regs[i]
loc := reg.FindStringIndex(upperQuery)
locs := reg.FindStringSubmatchIndex(upperQuery)
ns := reg.SubexpNames()
var loc []int
if len(locs) > 0 {
for i, n := range ns {
if n == "REPLACE" {
loc = locs[i*2 : (i+1)*2]
break
}
}
}
if len(loc) < 2 {
i++
continue
Expand Down
28 changes: 28 additions & 0 deletions sessionctx/binloginfo/binloginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,18 @@ func (s *testBinlogSuite) TestAddSpecialComment(c *C) {
"create table t1 (id int, a varchar(255), primary key (a, b) clustered);",
"create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] clustered */ );",
},
{
"create table t1(id int, v int, primary key(a) clustered);",
"create table t1(id int, v int, primary key(a) /*T![clustered_index] clustered */ );",
},
{
"create table t1(id int primary key clustered, v int);",
"create table t1(id int primary key /*T![clustered_index] clustered */ , v int);",
},
{
"alter table t add primary key(a) clustered;",
"alter table t add primary key(a) /*T![clustered_index] clustered */ ;",
},
{
"create table t1 (id int, a varchar(255), primary key (a, b) nonclustered);",
"create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */ );",
Expand All @@ -622,6 +634,22 @@ func (s *testBinlogSuite) TestAddSpecialComment(c *C) {
"create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */);",
"create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */);",
},
{
"create table clustered_test(id int)",
"create table clustered_test(id int)",
},
{
"create database clustered_test",
"create database clustered_test",
},
{
"create database clustered",
"create database clustered",
},
{
"create table clustered (id int)",
"create table clustered (id int)",
},
}
for _, ca := range testCase {
re := binloginfo.AddSpecialComment(ca.input)
Expand Down
8 changes: 4 additions & 4 deletions types/parser_driver/special_cmt_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const (

// FeatureIDPatterns is used to record special comments patterns.
var FeatureIDPatterns = map[featureID]*regexp.Regexp{
FeatureIDAutoRandom: regexp.MustCompile(`(?i)AUTO_RANDOM\b\s*(\s*\(\s*\d+\s*\)\s*)?`),
FeatureIDAutoIDCache: regexp.MustCompile(`(?i)AUTO_ID_CACHE\s*=?\s*\d+\s*`),
FeatureIDAutoRandomBase: regexp.MustCompile(`(?i)AUTO_RANDOM_BASE\s*=?\s*\d+\s*`),
FeatureClusteredIndex: regexp.MustCompile(`(?i)(NON)?CLUSTERED`),
FeatureIDAutoRandom: regexp.MustCompile(`(?P<REPLACE>(?i)AUTO_RANDOM\b\s*(\s*\(\s*\d+\s*\)\s*)?)`),
FeatureIDAutoIDCache: regexp.MustCompile(`(?P<REPLACE>(?i)AUTO_ID_CACHE\s*=?\s*\d+\s*)`),
FeatureIDAutoRandomBase: regexp.MustCompile(`(?P<REPLACE>(?i)AUTO_RANDOM_BASE\s*=?\s*\d+\s*)`),
FeatureClusteredIndex: regexp.MustCompile(`(?i)PRIMARY\s+KEY(\s*\(.*\))?\s+(?P<REPLACE>(NON)?CLUSTERED\b)`),
}

0 comments on commit bc572f3

Please sign in to comment.