From b8f054f8b923f073319ac7afe05a6a08b2acf67b Mon Sep 17 00:00:00 2001 From: CbcWestwolf <1004626265@qq.com> Date: Thu, 20 Oct 2022 11:39:54 +0800 Subject: [PATCH] This is an automated cherry-pick of #38461 Signed-off-by: ti-chi-bot --- executor/grant.go | 1 + executor/grant_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/executor/grant.go b/executor/grant.go index cbe15b2c350d0..650ebcc2a1445 100644 --- a/executor/grant.go +++ b/executor/grant.go @@ -126,6 +126,7 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error { // Create internal session to start internal transaction. isCommit := false internalSession, err := e.getSysSession() + internalSession.GetSessionVars().User = e.ctx.GetSessionVars().User if err != nil { return err } diff --git a/executor/grant_test.go b/executor/grant_test.go index 240e9e5e41154..04087d8ad5c49 100644 --- a/executor/grant_test.go +++ b/executor/grant_test.go @@ -595,3 +595,37 @@ func TestNonExistTableIllegalGrant(t *testing.T) { // Column level, not existing table, illegal privilege tk.MustGetErrCode("grant create temporary tables (NotExistsCol) on NotExistsD29302.NotExistsT29302 to u29302;", mysql.ErrWrongUsage) } +<<<<<<< HEAD +======= + +func TestIssue34610(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("DROP DATABASE IF EXISTS d1;") + tk.MustExec("CREATE DATABASE d1;") + tk.MustExec("USE d1;") + tk.MustExec("CREATE USER user_1@localhost;") + defer func() { + tk.MustExec("DROP DATABASE d1;") + tk.MustExec("DROP USER user_1@localhost;") + }() + + tk.MustExec("CREATE TABLE T1(f1 INT);") + tk.MustGetErrCode("CREATE TABLE t1(f1 INT);", mysql.ErrTableExists) + tk.MustExec("GRANT SELECT ON T1 to user_1@localhost;") + tk.MustExec("GRANT SELECT ON t1 to user_1@localhost;") +} + +func TestIssue38293(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.Session().GetSessionVars().User = &auth.UserIdentity{Username: "root", Hostname: "localhost"} + tk.MustExec("DROP USER IF EXISTS test") + tk.MustExec("CREATE USER test") + defer func() { + tk.MustExec("DROP USER test") + }() + tk.MustExec("GRANT SELECT ON `mysql`.`db` TO test") + tk.MustQuery("SELECT `Grantor` FROM `mysql`.`tables_priv` WHERE User = 'test'").Check(testkit.Rows("root@localhost")) +} +>>>>>>> 17c7bcc85e (executor: fix the missing `Grantor` when querying `mysql`.`tables_priv` (#38461))