Skip to content

Commit

Permalink
parser: set UnionStmt and ShowStmt as readonly (pingcap#755)
Browse files Browse the repository at this point in the history
* add UnionStmt and ShowStmt as readonly

* fix UnionStmt
  • Loading branch information
djshow832 committed Mar 1, 2020
1 parent 73763c7 commit b54f49d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ast/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ func IsReadOnly(node Node) bool {

node.Accept(&checker)
return checker.readOnly
case *ExplainStmt, *DoStmt:
case *ExplainStmt, *DoStmt, *ShowStmt:
return true
case *UnionStmt:
for _, sel := range node.(*UnionStmt).SelectList.Selects {
if !IsReadOnly(sel) {
return false
}
}
return true
default:
return false
Expand Down
38 changes: 38 additions & 0 deletions ast/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ func (s *testCacheableSuite) TestCacheable(c *C) {

stmt = &DoStmt{}
c.Assert(IsReadOnly(stmt), IsTrue)

stmt = &ShowStmt{}
c.Assert(IsReadOnly(stmt), IsTrue)

stmt = &ShowStmt{}
c.Assert(IsReadOnly(stmt), IsTrue)
}

func (s *testCacheableSuite) TestUnionReadOnly(c *C) {
selectReadOnly := &SelectStmt{}
selectForUpdate := &SelectStmt{
LockTp: SelectLockForUpdate,
}
selectForUpdateNoWait := &SelectStmt{
LockTp: SelectLockForUpdateNoWait,
}

unionStmt := &UnionStmt{
SelectList: &UnionSelectList{
Selects: []*SelectStmt{selectReadOnly, selectReadOnly},
},
}
c.Assert(IsReadOnly(unionStmt), IsTrue)

unionStmt.SelectList.Selects = []*SelectStmt{selectReadOnly, selectReadOnly, selectReadOnly}
c.Assert(IsReadOnly(unionStmt), IsTrue)

unionStmt.SelectList.Selects = []*SelectStmt{selectReadOnly, selectForUpdate}
c.Assert(IsReadOnly(unionStmt), IsFalse)

unionStmt.SelectList.Selects = []*SelectStmt{selectReadOnly, selectForUpdateNoWait}
c.Assert(IsReadOnly(unionStmt), IsFalse)

unionStmt.SelectList.Selects = []*SelectStmt{selectForUpdate, selectForUpdateNoWait}
c.Assert(IsReadOnly(unionStmt), IsFalse)

unionStmt.SelectList.Selects = []*SelectStmt{selectReadOnly, selectForUpdate, selectForUpdateNoWait}
c.Assert(IsReadOnly(unionStmt), IsFalse)
}

// CleanNodeText set the text of node and all child node empty.
Expand Down

0 comments on commit b54f49d

Please sign in to comment.