Skip to content

Commit

Permalink
planner: fix create view grammer check (pingcap#15334)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi authored Mar 15, 2020
1 parent f58d9ff commit 1e32b49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ var (
ErrPrimaryCantHaveNull = terror.ClassDDL.New(mysql.ErrPrimaryCantHaveNull, mysql.MySQLErrName[mysql.ErrPrimaryCantHaveNull])
// ErrErrorOnRename returns error for wrong database name in alter table rename
ErrErrorOnRename = terror.ClassDDL.New(mysql.ErrErrorOnRename, mysql.MySQLErrName[mysql.ErrErrorOnRename])
// ErrViewSelectClause returns error for create view with select into clause
ErrViewSelectClause = terror.ClassDDL.New(mysql.ErrViewSelectClause, mysql.MySQLErrName[mysql.ErrViewSelectClause])

// ErrNotAllowedTypeInPartition returns not allowed type error when creating table partition with unsupported expression type.
ErrNotAllowedTypeInPartition = terror.ClassDDL.New(mysql.ErrFieldTypeNotAllowedAsPartitionField, mysql.MySQLErrName[mysql.ErrFieldTypeNotAllowedAsPartitionField])
Expand Down
26 changes: 26 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
case *ast.CreateViewStmt:
p.flag |= inCreateOrDropTable
p.checkCreateViewGrammar(node)
p.checkCreateViewWithSelectGrammar(node)
case *ast.DropTableStmt:
p.flag |= inCreateOrDropTable
p.checkDropTableGrammar(node)
Expand Down Expand Up @@ -459,6 +460,31 @@ func (p *preprocessor) checkCreateViewGrammar(stmt *ast.CreateViewStmt) {
}
}

func (p *preprocessor) checkCreateViewWithSelect(stmt *ast.SelectStmt) {
if stmt.SelectIntoOpt != nil {
p.err = ddl.ErrViewSelectClause.GenWithStackByArgs("INFO")
return
}
if stmt.LockTp != ast.SelectLockNone {
stmt.LockTp = ast.SelectLockNone
return
}
}

func (p *preprocessor) checkCreateViewWithSelectGrammar(stmt *ast.CreateViewStmt) {
switch stmt := stmt.Select.(type) {
case *ast.SelectStmt:
p.checkCreateViewWithSelect(stmt)
case *ast.UnionStmt:
for _, selectStmt := range stmt.SelectList.Selects {
p.checkCreateViewWithSelect(selectStmt)
if p.err != nil {
return
}
}
}
}

func (p *preprocessor) checkDropSequenceGrammar(stmt *ast.DropSequenceStmt) {
p.checkDropTableNames(stmt.Sequences)
}
Expand Down
3 changes: 3 additions & 0 deletions planner/core/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ 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},
{"CREATE VIEW V AS SELECT 5 INTO OUTFILE 'ttt'", true, ddl.ErrViewSelectClause.GenWithStackByArgs("INFO")},
{"CREATE VIEW V AS SELECT 5 FOR UPDATE", false, nil},
{"CREATE VIEW V AS SELECT 5 LOCK IN SHARE MODE", 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},
Expand Down

0 comments on commit 1e32b49

Please sign in to comment.