Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pingcap/parser into table…
Browse files Browse the repository at this point in the history
…_lock
  • Loading branch information
crazycs520 committed Apr 28, 2019
2 parents 9437760 + f3ecae0 commit 3d3170b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
25 changes: 25 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ const (
ColumnOptionCollate
)

var (
invalidOptionForGeneratedColumn = map[ColumnOptionType]struct{}{
ColumnOptionAutoIncrement: {},
ColumnOptionOnUpdate: {},
ColumnOptionDefaultValue: {},
}
)

// ColumnOption is used for parsing column constraint info from SQL.
type ColumnOption struct {
node
Expand Down Expand Up @@ -672,6 +680,23 @@ func (n *ColumnDef) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// Validate checks if a column definition is legal.
// For example, generated column definitions that contain such
// column options as `ON UPDATE`, `AUTO_INCREMENT`, `DEFAULT`
// are illegal.
func (n *ColumnDef) Validate() bool {
generatedCol := false
illegalOpt4gc := false
for _, opt := range n.Options {
if opt.Tp == ColumnOptionGenerated {
generatedCol = true
}
_, found := invalidOptionForGeneratedColumn[opt.Tp]
illegalOpt4gc = illegalOpt4gc || found
}
return !(generatedCol && illegalOpt4gc)
}

// CreateTableStmt is a statement to create a table.
// See https://dev.mysql.com/doc/refman/5.7/en/create-table.html
type CreateTableStmt struct {
Expand Down
7 changes: 6 additions & 1 deletion parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,12 @@ ColumnDefList:
ColumnDef:
ColumnName Type ColumnOptionListOpt
{
$$ = &ast.ColumnDef{Name: $1.(*ast.ColumnName), Tp: $2.(*types.FieldType), Options: $3.([]*ast.ColumnOption)}
colDef := &ast.ColumnDef{Name: $1.(*ast.ColumnName), Tp: $2.(*types.FieldType), Options: $3.([]*ast.ColumnOption)}
if !colDef.Validate() {
yylex.AppendError(yylex.Errorf("Invalid column definition"))
return 1
}
$$ = colDef
}

ColumnName:
Expand Down
10 changes: 10 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,16 @@ func (s *testParserSuite) TestDDL(c *C) {
{"CREATE TABLE bar (m INT) IGNORE SELECT n FROM foo;", true, "CREATE TABLE `bar` (`m` INT) IGNORE AS SELECT `n` FROM `foo`"},
{"CREATE TABLE bar (m INT) REPLACE SELECT n FROM foo;", true, "CREATE TABLE `bar` (`m` INT) REPLACE AS SELECT `n` FROM `foo`"},

// for generated column definition
{"create table t (a timestamp, b timestamp as (a) not null on update current_timestamp);", false, ""},
{"create table t (a bigint, b bigint as (a) primary key auto_increment);", false, ""},
{"create table t (a bigint, b bigint as (a) not null default 10);", false, ""},
{"create table t (a bigint, b bigint as (a+1) not null);", true, "CREATE TABLE `t` (`a` BIGINT,`b` BIGINT GENERATED ALWAYS AS(`a`+1) VIRTUAL NOT NULL)"},
{"create table t (a bigint, b bigint as (a+1) not null);", true, "CREATE TABLE `t` (`a` BIGINT,`b` BIGINT GENERATED ALWAYS AS(`a`+1) VIRTUAL NOT NULL)"},
{"create table t (a bigint, b bigint as (a+1) not null comment 'ttt');", true, "CREATE TABLE `t` (`a` BIGINT,`b` BIGINT GENERATED ALWAYS AS(`a`+1) VIRTUAL NOT NULL COMMENT 'ttt')"},
{"alter table t add column (f timestamp as (a+1) default '2019-01-01 11:11:11');", false, ""},
{"alter table t modify column f int as (a+1) default 55;", false, ""},

// for recover table
{"recover table by job 11", true, "RECOVER TABLE BY JOB 11"},
{"recover table by job 11,12,13", false, ""},
Expand Down

0 comments on commit 3d3170b

Please sign in to comment.