Skip to content

Commit

Permalink
type/compatibility: check KEY option for generated column. (#9529)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingyu authored and alivxxx committed Apr 10, 2019
1 parent f616af1 commit f778a0c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions planner/core/errors.go
Expand Up @@ -75,6 +75,7 @@ const (
codeWindowRangeBoundNotConstant = mysql.ErrWindowRangeBoundNotConstant
codeWindowRowsIntervalUse = mysql.ErrWindowRowsIntervalUse
codeWindowFunctionIgnoresFrame = mysql.ErrWindowFunctionIgnoresFrame
codeUnsupportedOnGeneratedColumn = mysql.ErrUnsupportedOnGeneratedColumn
)

// error definitions.
Expand Down Expand Up @@ -136,6 +137,7 @@ var (
ErrWindowRangeBoundNotConstant = terror.ClassOptimizer.New(codeWindowRangeBoundNotConstant, mysql.MySQLErrName[mysql.ErrWindowRangeBoundNotConstant])
ErrWindowRowsIntervalUse = terror.ClassOptimizer.New(codeWindowRowsIntervalUse, mysql.MySQLErrName[mysql.ErrWindowRowsIntervalUse])
ErrWindowFunctionIgnoresFrame = terror.ClassOptimizer.New(codeWindowFunctionIgnoresFrame, mysql.MySQLErrName[mysql.ErrWindowFunctionIgnoresFrame])
ErrUnsupportedOnGeneratedColumn = terror.ClassOptimizer.New(codeUnsupportedOnGeneratedColumn, mysql.MySQLErrName[mysql.ErrUnsupportedOnGeneratedColumn])
ErrNoSuchThread = terror.ClassOptimizer.New(mysql.ErrNoSuchThread, mysql.MySQLErrName[mysql.ErrNoSuchThread])
// Since we cannot know if user loggined with a password, use message of ErrAccessDeniedNoPassword instead
ErrAccessDenied = terror.ClassOptimizer.New(mysql.ErrAccessDenied, mysql.MySQLErrName[mysql.ErrAccessDeniedNoPassword])
Expand Down Expand Up @@ -190,6 +192,7 @@ func init() {
codeWindowRangeBoundNotConstant: mysql.ErrWindowRangeBoundNotConstant,
codeWindowRowsIntervalUse: mysql.ErrWindowRowsIntervalUse,
codeWindowFunctionIgnoresFrame: mysql.ErrWindowFunctionIgnoresFrame,
codeUnsupportedOnGeneratedColumn: mysql.ErrUnsupportedOnGeneratedColumn,

mysql.ErrNoSuchThread: mysql.ErrNoSuchThread,
mysql.ErrAccessDenied: mysql.ErrAccessDenied,
Expand Down
26 changes: 21 additions & 5 deletions planner/core/preprocess.go
Expand Up @@ -331,7 +331,12 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) {
p.err = err
return
}
countPrimaryKey += isPrimary(colDef.Options)
isPrimary, err := checkColumnOptions(colDef.Options)
if err != nil {
p.err = err
return
}
countPrimaryKey += isPrimary
if countPrimaryKey > 1 {
p.err = infoschema.ErrMultiplePriKey
return
Expand Down Expand Up @@ -428,13 +433,24 @@ func isTableAliasDuplicate(node ast.ResultSetNode, tableAliases map[string]inter
return nil
}

func isPrimary(ops []*ast.ColumnOption) int {
func checkColumnOptions(ops []*ast.ColumnOption) (int, error) {
isPrimary, isGenerated, isStored := 0, 0, false

for _, op := range ops {
if op.Tp == ast.ColumnOptionPrimaryKey {
return 1
switch op.Tp {
case ast.ColumnOptionPrimaryKey:
isPrimary = 1
case ast.ColumnOptionGenerated:
isGenerated = 1
isStored = op.Stored
}
}
return 0

if isPrimary > 0 && isGenerated > 0 && !isStored {
return isPrimary, ErrUnsupportedOnGeneratedColumn.GenWithStackByArgs("Defining a virtual generated column as primary key")
}

return isPrimary, nil
}

func (p *preprocessor) checkCreateIndexGrammar(stmt *ast.CreateIndexStmt) {
Expand Down
5 changes: 5 additions & 0 deletions planner/core/preprocess_test.go
Expand Up @@ -198,6 +198,11 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"select * from (select 1 ) a , (select 2) b, (select * from (select 3) a join (select 4) b) c;", false, nil},

{"CREATE VIEW V (a,b,c) AS SELECT 1,1,3;", false, nil},

// issue 9464
{"CREATE TABLE t1 (id INT NOT NULL, c1 VARCHAR(20) AS ('foo') VIRTUAL KEY NULL, PRIMARY KEY (id));", false, core.ErrUnsupportedOnGeneratedColumn},
{"CREATE TABLE t1 (id INT NOT NULL, c1 VARCHAR(20) AS ('foo') VIRTUAL KEY NOT NULL, PRIMARY KEY (id));", false, core.ErrUnsupportedOnGeneratedColumn},
{"create table t (a DOUBLE NULL, b_sto DOUBLE GENERATED ALWAYS AS (a + 2) STORED UNIQUE KEY NOT NULL PRIMARY KEY);", false, nil},
}

store, dom, err := newStoreWithBootstrap()
Expand Down

0 comments on commit f778a0c

Please sign in to comment.