Skip to content

Commit

Permalink
*: Support AlterTable with Algorithm option and fix a few things repo…
Browse files Browse the repository at this point in the history
…rted by goword (pingcap#6141)

Improve the compatibility with MySQL.
  • Loading branch information
shenli authored Mar 27, 2018
1 parent defccc1 commit 959c319
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
5 changes: 4 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type ColumnOption struct {
node

Tp ColumnOptionType
// Expr is used for ColumnOptionDefaultValue/ColumnOptionOnUpdateColumnOptionGenerated.
// For ColumnOptionDefaultValue or ColumnOptionOnUpdate, it's the target value.
// For ColumnOptionGenerated, it's the target expression.
Expr ExprNode
Expand Down Expand Up @@ -473,8 +474,9 @@ type RenameTableStmt struct {

OldTable *TableName
NewTable *TableName

// TableToTables is only useful for syncer which depends heavily on tidb parser to do some dirty work for now.
// TODO: Refactor this when you are going to add full support for multiple schema changes.
// Currently it is only useful for syncer which depends heavily on tidb parser to do some dirty work.
TableToTables []*TableToTable
}

Expand Down Expand Up @@ -713,6 +715,7 @@ const (
AlterTableRenameTable
AlterTableAlterColumn
AlterTableLock
AlterTableAlgorithm

// TODO: Add more actions
)
Expand Down
11 changes: 9 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,18 @@ func hasAutoIncrementColumn(tbInfo *model.TableInfo) bool {
return false
}

// isIgnorableSpec checks if the spec type is ignorable.
// Some specs are parsed by ignored. This is for compatibility.
func isIgnorableSpec(tp ast.AlterTableType) bool {
// AlterTableLock/AlterTableAlgorithm are ignored.
return tp == ast.AlterTableLock || tp == ast.AlterTableAlgorithm
}

func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.AlterTableSpec) (err error) {
// Only handle valid specs, AlterTableLock is ignored.
// Only handle valid specs.
validSpecs := make([]*ast.AlterTableSpec, 0, len(specs))
for _, spec := range specs {
if spec.Tp == ast.AlterTableLock {
if isIgnorableSpec(spec.Tp) {
continue
}
validSpecs = append(validSpecs, spec)
Expand Down
27 changes: 27 additions & 0 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,30 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
tblInfo = testTableInfo(c, d, "t1", 3)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
}

func (s *testDDLSuite) TestIgnorableSpec(c *C) {
specs := []ast.AlterTableType{
ast.AlterTableOption,
ast.AlterTableAddColumns,
ast.AlterTableAddConstraint,
ast.AlterTableDropColumn,
ast.AlterTableDropPrimaryKey,
ast.AlterTableDropIndex,
ast.AlterTableDropForeignKey,
ast.AlterTableModifyColumn,
ast.AlterTableChangeColumn,
ast.AlterTableRenameTable,
ast.AlterTableAlterColumn,
}
for _, spec := range specs {
c.Assert(isIgnorableSpec(spec), IsFalse)
}

ignorableSpecs := []ast.AlterTableType{
ast.AlterTableLock,
ast.AlterTableAlgorithm,
}
for _, spec := range ignorableSpecs {
c.Assert(isIgnorableSpec(spec), IsTrue)
}
}
2 changes: 2 additions & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ var tokenMap = map[string]int{
"CONSISTENT": consistent,
"CONSTRAINT": constraint,
"CONVERT": convert,
"COPY": copyKwd,
"COUNT": count,
"CREATE": create,
"CROSS": cross,
Expand Down Expand Up @@ -287,6 +288,7 @@ var tokenMap = map[string]int{
"INDEXES": indexes,
"INFILE": infile,
"INNER": inner,
"INPLACE": inplace,
"INSERT": insert,
"INT": intType,
"INT1": int1Type,
Expand Down
16 changes: 14 additions & 2 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,15 @@ import (
bitOr "BIT_OR"
bitXor "BIT_XOR"
cast "CAST"
copyKwd "COPY"
count "COUNT"
curTime "CURTIME"
dateAdd "DATE_ADD"
dateSub "DATE_SUB"
extract "EXTRACT"
getFormat "GET_FORMAT"
groupConcat "GROUP_CONCAT"
inplace "INPLACE"
min "MIN"
max "MAX"
now "NOW"
Expand Down Expand Up @@ -1019,6 +1021,16 @@ AlterTableSpec:
LockType: $1.(ast.LockType),
}
}
| "ALGORITHM" EqOpt AlterAlgorithm
{
// Parse it and ignore it. Just for compatibility.
$$ = &ast.AlterTableSpec{
Tp: ast.AlterTableAlgorithm,
}
}

AlterAlgorithm:
"DEFAULT" | "INPLACE" | "COPY"

LockClauseOpt:
{}
Expand Down Expand Up @@ -2540,8 +2552,8 @@ TiDBKeyword:
"ADMIN" | "CANCEL" | "DDL" | "JOBS" | "JOB" | "STATS" | "STATS_META" | "STATS_HISTOGRAMS" | "STATS_BUCKETS" | "STATS_HEALTHY" | "TIDB" | "TIDB_HJ" | "TIDB_SMJ" | "TIDB_INLJ"

NotKeywordToken:
"ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT" | "MIN" | "MAX" | "NOW" | "POSITION"
| "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TRIM"
"ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COPY" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT"
| "INPLACE" |"MIN" | "MAX" | "NOW" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TRIM"

/************************************************************************************
*
Expand Down
3 changes: 3 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,9 @@ func (s *testParserSuite) TestDDL(c *C) {
{"ALTER TABLE t ENGINE = '', ADD COLUMN a SMALLINT", true},
{"ALTER TABLE t default COLLATE = utf8_general_ci, ENGINE = '', ADD COLUMN a SMALLINT", true},
{"ALTER TABLE t shard_row_id_bits = 1", true},
{"ALTER TABLE `hello-world@dev`.`User` ADD COLUMN `name` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL , ALGORITHM = DEFAULT;", true},
{"ALTER TABLE `hello-world@dev`.`User` ADD COLUMN `name` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL , ALGORITHM = INPLACE;", true},
{"ALTER TABLE `hello-world@dev`.`User` ADD COLUMN `name` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL , ALGORITHM = COPY;", true},

// For create index statement
{"CREATE INDEX idx ON t (a)", true},
Expand Down

0 comments on commit 959c319

Please sign in to comment.