Skip to content

Commit

Permalink
utils: IsReadOnly returns wrong result when checking SQLs like `exp…
Browse files Browse the repository at this point in the history
…lain analyze insert ...` (pingcap#381)

* update IsReadOnly

* add UT

* fixup
  • Loading branch information
qw4990 authored and kennytm committed Jul 10, 2019
1 parent bd83023 commit fadeae6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ast/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func IsReadOnly(node Node) bool {

node.Accept(&checker)
return checker.readOnly
case *ExplainStmt, *DoStmt:
case *ExplainStmt:
return !st.Analyze || IsReadOnly(st.Stmt)
case *DoStmt:
return true
default:
return false
Expand Down
23 changes: 23 additions & 0 deletions ast/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ func (s *testCacheableSuite) TestCacheable(c *C) {

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

stmt = &ExplainStmt{
Stmt: &InsertStmt{},
}
c.Assert(IsReadOnly(stmt), IsTrue)

stmt = &ExplainStmt{
Analyze: true,
Stmt: &InsertStmt{},
}
c.Assert(IsReadOnly(stmt), IsFalse)

stmt = &ExplainStmt{
Stmt: &SelectStmt{},
}
c.Assert(IsReadOnly(stmt), IsTrue)

stmt = &ExplainStmt{
Analyze: true,
Stmt: &SelectStmt{},
}
c.Assert(IsReadOnly(stmt), IsTrue)

}

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

0 comments on commit fadeae6

Please sign in to comment.