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 84db2d0433ca7..b84649d5e4e9e 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("SUPER")