Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: add missing table lock check for fast plan (#20948) #21002

Merged
merged 6 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ func (s *testPlanSuite) TestFastPlanContextTables(c *C) {
false,
},
}
s.ctx.GetSessionVars().SnapshotInfoschema = s.is
for _, tt := range tests {
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil)
Expand Down
19 changes: 14 additions & 5 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,24 @@ func newPointGetPlan(ctx sessionctx.Context, dbName string, schema *expression.S

func checkFastPlanPrivilege(ctx sessionctx.Context, dbName, tableName string, checkTypes ...mysql.PrivilegeType) error {
pm := privilege.GetPrivilegeManager(ctx)
if pm == nil {
return nil
}
visitInfos := []visitInfo{}
for _, checkType := range checkTypes {
if !pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, dbName, tableName, "", checkType) {
if pm != nil && !pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, dbName, tableName, "", checkType) {
return errors.New("privilege check fail")
}
// This visitInfo is only for table lock check, so we do not need column field,
// just fill it empty string.
visitInfos = append(visitInfos, visitInfo{
privilege: checkType,
db: dbName,
table: tableName,
column: "",
err: nil,
})
}
return nil

infoSchema := infoschema.GetInfoSchema(ctx)
return CheckTableLock(ctx, infoSchema, visitInfos)
}

func buildSchemaFromFields(
Expand Down
18 changes: 18 additions & 0 deletions planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
Expand Down Expand Up @@ -449,6 +450,23 @@ func (s *testPointGetSuite) TestIssue19141(c *C) {
tk.MustQuery("select * from t19141 order by c_int").Check(testkit.Rows("1", "2", "3", "4"))
}

func (s *testPointGetSuite) TestUpdateWithTableReadLockWillFail(c *C) {
gcfg := config.GetGlobalConfig()
etl := gcfg.EnableTableLock
gcfg.EnableTableLock = true
defer func() {
gcfg.EnableTableLock = etl
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table tbllock(id int, c int);")
tk.MustExec("insert into tbllock values(1, 2), (2, 2);")
tk.MustExec("lock table tbllock read;")
_, err := tk.Exec("update tbllock set c = 3 where id = 2;")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[schema:1099]Table 'tbllock' was locked with a READ lock and can't be updated")
}

func (s *testPointGetSuite) TestSelectInMultiColumns(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down