Repository: pingcap/tidb (Go)

Issue:
Revoke role from current_user results error.
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```sql
create role r1;
grant r1 to current_user();
revoke r1 from current_user();
drop role r1;
```

### 2. What did you expect to see? (Required)
```sql
mysql> create role r1;
Query OK, 0 rows affected (0.01 sec)

mysql> grant r1 to current_user();
Query OK, 0 rows affected (0.00 sec)

mysql> revoke r1 from current_user();
Query OK, 0 rows affected (0.00 sec)

mysql> drop role r1;
Query OK, 0 rows affected (0.00 sec)
```
### 3. What did you see instead (Required)
```sql
mysql> create role r1;
Query OK, 0 rows affected (0.01 sec)

mysql> grant r1 to current_user();
Query OK, 0 rows affected (0.00 sec)

mysql> revoke r1 from current_user();
ERROR 1396 (HY000): Operation REVOKE ROLE failed for @
mysql> drop role r1;
Query OK, 0 rows affected (0.01 sec)
```
### 4. What is your TiDB version? (Required)
```sql
| Release Version: v6.0.0-alpha-62-g15ddcd085
Edition: Community
Git Commit Hash: 15ddcd085a8be0f97c1b5ce1e5397369c3061372
Git Branch: master
UTC Build Time: 2022-03-15 15:49:18
GoVersion: go1.17.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
```




Discussion:

Patch:
diff --git a/executor/simple.go b/executor/simple.go
index a3fa289373eb8..33fd448fb0ae8 100644
--- a/executor/simple.go
+++ b/executor/simple.go
@@ -644,8 +644,22 @@ func (e *SimpleExec) executeReleaseSavepoint(s *ast.ReleaseSavepointStmt) error
 	return nil
 }
 
+func (e *SimpleExec) setCurrentUser(users []*auth.UserIdentity) {
+	sessionVars := e.ctx.GetSessionVars()
+	for i, user := range users {
+		if user.CurrentUser {
+			users[i].Username = sessionVars.User.AuthUsername
+			users[i].Hostname = sessionVars.User.AuthHostname
+		}
+	}
+}
+
 func (e *SimpleExec) executeRevokeRole(ctx context.Context, s *ast.RevokeRoleStmt) error {
 	internalCtx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnPrivilege)
+
+	//Fix revoke role from current_user results error.
+	e.setCurrentUser(s.Users)
+
 	for _, role := range s.Roles {
 		exists, err := userExists(ctx, e.ctx, role.Username, role.Hostname)
 		if err != nil {
@@ -1069,13 +1083,8 @@ func (e *SimpleExec) executeAlterUser(ctx context.Context, s *ast.AlterUserStmt)
 
 func (e *SimpleExec) executeGrantRole(ctx context.Context, s *ast.GrantRoleStmt) error {
 	internalCtx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnPrivilege)
-	sessionVars := e.ctx.GetSessionVars()
-	for i, user := range s.Users {
-		if user.CurrentUser {
-			s.Users[i].Username = sessionVars.User.AuthUsername
-			s.Users[i].Hostname = sessionVars.User.AuthHostname
-		}
-	}
+
+	e.setCurrentUser(s.Users)
 
 	for _, role := range s.Roles {
 		exists, err := userExists(ctx, e.ctx, role.Username, role.Hostname)


Test patch:
diff --git a/executor/simpletest/simple_test.go b/executor/simpletest/simple_test.go
index 5018a5fa97996..1f886a18c4323 100644
--- a/executor/simpletest/simple_test.go
+++ b/executor/simpletest/simple_test.go
@@ -467,6 +467,30 @@ func inTxn(ctx sessionctx.Context) bool {
 	return (ctx.GetSessionVars().Status & mysql.ServerStatusInTrans) > 0
 }
 
+func TestIssue33144(t *testing.T) {
+	store := testkit.CreateMockStore(t)
+	tk := testkit.NewTestKit(t, store)
+
+	//Create role
+	tk.MustExec("create role 'r1' ;")
+
+	sessionVars := tk.Session().GetSessionVars()
+	sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", AuthUsername: "root", AuthHostname: "%"}
+
+	//Grant role to current_user()
+	tk.MustExec("grant 'r1' to current_user();")
+	//Revoke role from current_user()
+	tk.MustExec("revoke 'r1' from current_user();")
+
+	//Grant role to current_user(),current_user()
+	tk.MustExec("grant 'r1' to current_user(),current_user();")
+	//Revoke role from current_user(),current_user()
+	tk.MustExec("revoke 'r1' from current_user(),current_user();")
+
+	//Drop role
+	tk.MustExec("drop role 'r1' ;")
+}
+
 func TestRole(t *testing.T) {
 	store := testkit.CreateMockStore(t)
 	tk := testkit.NewTestKit(t, store)