Skip to content

Commit

Permalink
executor: flush statement should trigger implicit commit (#34134)
Browse files Browse the repository at this point in the history
close #34180
  • Loading branch information
tiancaiamao authored Apr 24, 2022
1 parent db07c2e commit d3a02f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,8 +1608,20 @@ func (e *SimpleExec) executeDropStats(s *ast.DropStatsStmt) (err error) {
}

func (e *SimpleExec) autoNewTxn() bool {
// Some statements cause an implicit commit
// See https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html
switch e.Statement.(type) {
case *ast.CreateUserStmt, *ast.AlterUserStmt, *ast.DropUserStmt, *ast.RenameUserStmt:
// Data definition language (DDL) statements that define or modify database objects.
// (handled in DDL package)
// Statements that implicitly use or modify tables in the mysql database.
case *ast.CreateUserStmt, *ast.AlterUserStmt, *ast.DropUserStmt, *ast.RenameUserStmt, *ast.RevokeRoleStmt, *ast.GrantRoleStmt:
return true
// Transaction-control and locking statements. BEGIN, LOCK TABLES, SET autocommit = 1 (if the value is not already 1), START TRANSACTION, UNLOCK TABLES.
// (handled in other place)
// Data loading statements. LOAD DATA
// (handled in other place)
// Administrative statements. TODO: ANALYZE TABLE, CACHE INDEX, CHECK TABLE, FLUSH, LOAD INDEX INTO CACHE, OPTIMIZE TABLE, REPAIR TABLE, RESET (but not RESET PERSIST).
case *ast.FlushStmt:
return true
}
return false
Expand Down
26 changes: 26 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,29 @@ func TestUserWithSetNames(t *testing.T) {

tk.MustExec("drop user '\xd2\xbb';")
}

func TestStatementsCauseImplicitCommit(t *testing.T) {
// Test some of the implicit commit statements.
// See https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("create table ic (id int primary key)")

cases := []string{
"create table xx (id int)",
"create user 'xx'@'127.0.0.1'",
"grant SELECT on test.ic to 'xx'@'127.0.0.1'",
"flush privileges",
"analyze table ic",
}
for i, sql := range cases {
tk.MustExec("begin")
tk.MustExec("insert into ic values (?)", i)
tk.MustExec(sql)
tk.MustQuery("select * from ic where id = ?", i).Check(testkit.Rows(strconv.FormatInt(int64(i), 10)))
// Clean up data
tk.MustExec("delete from ic")
}
}

0 comments on commit d3a02f4

Please sign in to comment.