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

parser: apply feature-ids special comments to auto_random (#15412) #16155

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
}
}
if tableInfo.PKIsHandle && tableInfo.ContainsAutoRandomBits() && tableInfo.GetPkName().L == col.Name.L {
buf.WriteString(fmt.Sprintf(" /*T!%s AUTO_RANDOM(%d) */", parser.CommentCodeAutoRandom, tableInfo.AutoRandomBits))
buf.WriteString(fmt.Sprintf(" /*T![auto_rand] AUTO_RANDOM(%d) */", tableInfo.AutoRandomBits))
}
if len(col.Comment) > 0 {
buf.WriteString(fmt.Sprintf(" COMMENT '%s'", format.OutputFormat(col.Comment)))
Expand Down
8 changes: 4 additions & 4 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) {
tk.MustQuery("show create table `auto_random_tbl1`").Check(testutil.RowsWithSep("|",
""+
"auto_random_tbl1 CREATE TABLE `auto_random_tbl1` (\n"+
" `a` bigint(20) NOT NULL /*T!40000 AUTO_RANDOM(3) */,\n"+
" `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(3) */,\n"+
" `b` varchar(255) DEFAULT NULL,\n"+
" PRIMARY KEY (`a`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
Expand All @@ -684,18 +684,18 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) {
tk.MustQuery("show create table auto_random_tbl2").Check(testutil.RowsWithSep("|",
""+
"auto_random_tbl2 CREATE TABLE `auto_random_tbl2` (\n"+
" `a` bigint(20) NOT NULL /*T!40000 AUTO_RANDOM(5) */,\n"+
" `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+
" `b` char(1) DEFAULT NULL,\n"+
" PRIMARY KEY (`a`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

// Special version comment can be shown in TiDB with new version.
tk.MustExec("create table auto_random_tbl3 (a bigint /*T!40000 auto_random */ primary key)")
tk.MustExec("create table auto_random_tbl3 (a bigint /*T![auto_rand] auto_random */ primary key)")
tk.MustQuery("show create table auto_random_tbl3").Check(testutil.RowsWithSep("|",
""+
"auto_random_tbl3 CREATE TABLE `auto_random_tbl3` (\n"+
" `a` bigint(20) NOT NULL /*T!40000 AUTO_RANDOM(5) */,\n"+
" `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+
" PRIMARY KEY (`a`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20200317043902-2838e21ca222
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd
github.com/pingcap/parser v3.1.0-beta.2.0.20200422070516-81c09c8b95ac+incompatible
github.com/pingcap/parser v3.1.0-beta.2.0.20200425032215-994651e9b6df+incompatible
github.com/pingcap/pd/v3 v3.1.0-beta.2.0.20200312100832-1206736bd050
github.com/pingcap/tidb-tools v4.0.0-beta.1.0.20200317092225-ed6b2a87af54+incompatible
github.com/pingcap/tipb v0.0.0-20200401093201-cc8b75c53383
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 h1:AJD9pZYm72vMgPcQDww
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd h1:CV3VsP3Z02MVtdpTMfEgRJ4T9NGgGTxdHpJerent7rM=
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/parser v3.1.0-beta.2.0.20200422070516-81c09c8b95ac+incompatible h1:VJYsj/fJer1IsBukZi9GIgg/20YXO3odef8KEc4SHe0=
github.com/pingcap/parser v3.1.0-beta.2.0.20200422070516-81c09c8b95ac+incompatible/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v3.1.0-beta.2.0.20200425032215-994651e9b6df+incompatible h1:9kFDvyd1YTGin3xHetc68xAChSOgQD2vThXxBR9ccH0=
github.com/pingcap/parser v3.1.0-beta.2.0.20200425032215-994651e9b6df+incompatible/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd/v3 v3.1.0-beta.2.0.20200312100832-1206736bd050 h1:mxPdR0pxnUcRfRGX2JnaLyAd9SZWeR42SzvMp4Zv3YI=
github.com/pingcap/pd/v3 v3.1.0-beta.2.0.20200312100832-1206736bd050/go.mod h1:0HfF1LfWLMuGpui0PKhGvkXxfjv1JslMRY6B+cae3dg=
github.com/pingcap/tidb-tools v4.0.0-beta.1.0.20200317092225-ed6b2a87af54+incompatible h1:tYADqdmWwgDOwf/qEN0trJAy6H3c3Tt/QZx1z4qVrRQ=
Expand Down
10 changes: 5 additions & 5 deletions sessionctx/binloginfo/binloginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package binloginfo

import (
"context"
"fmt"
"math"
"regexp"
"strings"
Expand All @@ -24,14 +23,14 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb-tools/tidb-binlog/node"
pumpcli "github.com/pingcap/tidb-tools/tidb-binlog/pump_client"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/sessionctx"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tipb/go-binlog"
"go.uber.org/zap"
Expand Down Expand Up @@ -301,16 +300,17 @@ func SetDDLBinlog(client *pumpcli.PumpsClient, txn kv.Transaction, jobID int64,
}

const specialPrefix = `/*!90000 `
const specialVersionPrefix = `/*T!%s `

// AddSpecialComment uses to add comment for table option in DDL query.
// Export for testing.
func AddSpecialComment(ddlQuery string) string {
if strings.Contains(ddlQuery, specialPrefix) || parser.SpecVersionCodePattern.MatchString(ddlQuery) {
if strings.Contains(ddlQuery, specialPrefix) || strings.Contains(ddlQuery, driver.SpecialCommentVersionPrefix) {
return ddlQuery
}
ddlQuery = addSpecialCommentByRegexps(ddlQuery, specialPrefix, shardPat, preSplitPat)
ddlQuery = addSpecialCommentByRegexps(ddlQuery, fmt.Sprintf(specialVersionPrefix, parser.CommentCodeAutoRandom), autoRandomPat)
for featureID, pattern := range driver.FeatureIDPatterns {
ddlQuery = addSpecialCommentByRegexps(ddlQuery, driver.BuildSpecialCommentPrefix(featureID), pattern)
}
return ddlQuery
}

Expand Down
6 changes: 3 additions & 3 deletions sessionctx/binloginfo/binloginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,15 @@ func (s *testBinlogSuite) TestAddSpecialComment(c *C) {
},
{
"create table t1 (id int primary key auto_random(2));",
"create table t1 (id int primary key /*T!40000 auto_random(2) */ );",
"create table t1 (id int primary key /*T![auto_rand] auto_random(2) */ );",
},
{
"create table t1 (id int auto_random ( 4 ) primary key);",
"create table t1 (id int /*T!40000 auto_random ( 4 ) */ primary key);",
"create table t1 (id int /*T![auto_rand] auto_random ( 4 ) */ primary key);",
},
{
"create table t1 (id int auto_random ( 4 ) primary key);",
"create table t1 (id int /*T!40000 auto_random ( 4 ) */ primary key);",
"create table t1 (id int /*T![auto_rand] auto_random ( 4 ) */ primary key);",
},
}
for _, ca := range testCase {
Expand Down
55 changes: 55 additions & 0 deletions types/parser_driver/special_cmt_ctrl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 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 driver

import (
"fmt"
"regexp"

"github.com/pingcap/parser"
)

// To add new features that needs to be downgrade-compatible,
// 1. Define a featureID below and make sure it is unique.
// For example, `const FeatureIDMyFea = "my_fea"`.
// 2. Register the new featureID in init().
// Only the registered parser can parse the comment annotated with `my_fea`.
// Now, the parser treats `/*T![my_fea] what_ever */` and `what_ever` equivalent.
// In other word, the parser in old-version TiDB will ignores these comments.
// 3. [optional] Add a pattern into FeatureIDPatterns.
// This is only required if the new feature is contained in DDL,
// and we want to comment out this part of SQL in binlog.
func init() {
parser.SpecialCommentsController.Register(string(FeatureIDAutoRandom))
}

// SpecialCommentVersionPrefix is the prefix of TiDB executable comments.
const SpecialCommentVersionPrefix = `/*T!`

// BuildSpecialCommentPrefix returns the prefix of `featureID` special comment.
func BuildSpecialCommentPrefix(featureID featureID) string {
return fmt.Sprintf("%s[%s]", SpecialCommentVersionPrefix, featureID)
}

type featureID string

const (
// FeatureIDAutoRandom is the `auto_random` feature.
FeatureIDAutoRandom featureID = "auto_rand"
)

// FeatureIDPatterns is used to record special comments patterns.
var FeatureIDPatterns = map[featureID]*regexp.Regexp{
FeatureIDAutoRandom: regexp.MustCompile(`(?i)AUTO_RANDOM\s*(\(\s*\d+\s*\))?\s*`),
}