From 4afe8e4a89b1b6a6ddf662a3972ccca8fcc5dd86 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 27 Nov 2019 20:41:41 +0800 Subject: [PATCH] planner/core: raise 'No database selected' error for grant statement (#13745) --- planner/core/integration_test.go | 11 +++++++++++ planner/core/planbuilder.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 68ee6aede09f4..2beb39b4b5bd3 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -15,8 +15,10 @@ package core_test import ( . "github.com/pingcap/check" + "github.com/pingcap/errors" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/util/testkit" "github.com/pingcap/tidb/util/testutil" ) @@ -253,3 +255,12 @@ func (s *testIntegrationSuite) TestPartitionTableStats(c *C) { tk.MustQuery(tt).Check(testkit.Rows(output[i].Result...)) } } + +func (s *testIntegrationSuite) TestErrNoDB(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create user test") + _, err := tk.Exec("grant select on test1111 to test@'%'") + c.Assert(errors.Cause(err), Equals, core.ErrNoDB) + tk.MustExec("use test") + tk.MustExec("grant select on test1111 to test@'%'") +} diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 479c1c2c99a61..7bda8b45dc120 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1359,6 +1359,11 @@ func (b *PlanBuilder) buildSimple(node ast.StmtNode) (Plan, error) { err := ErrSpecificAccessDenied.GenWithStackByArgs("CREATE USER") b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreateUserPriv, "", "", "", err) case *ast.GrantStmt: + if b.ctx.GetSessionVars().CurrentDB == "" && raw.Level.DBName == "" { + if raw.Level.Level == ast.GrantLevelTable { + return nil, ErrNoDB + } + } b.visitInfo = collectVisitInfoFromGrantStmt(b.ctx, b.visitInfo, raw) case *ast.GrantRoleStmt: err := ErrSpecificAccessDenied.GenWithStackByArgs("GRANT ROLE")