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 1 commit
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
Next Next commit
cherry pick #20948 to release-4.0
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
blacktear23 authored and ti-srebot committed Nov 12, 2020
commit e1c561d80de5fc3ac392c15be888130a42b0ff4f
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 @@ -787,15 +787,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
86 changes: 86 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 @@ -448,3 +449,88 @@ func (s *testPointGetSuite) TestIssue19141(c *C) {
tk.MustExec("delete from t19141 partition (p0) where c_int in (2,3)") // No data changed
tk.MustQuery("select * from t19141 order by c_int").Check(testkit.Rows("1", "2", "3", "4"))
}
<<<<<<< HEAD
=======

func (s *testPointGetSuite) TestSelectInMultiColumns(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t2(a int, b int, c int, primary key(a, b, c));")
tk.MustExec("insert into t2 values (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)")
tk.MustQuery("select * from t2 where (a, b, c) in ((1, 1, 1));").Check(testkit.Rows("1 1 1"))

_, err := tk.Exec("select * from t2 where (a, b, c) in ((1, 1, 1, 1));")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[expression:1241]Operand should contain 3 column(s)")

_, err = tk.Exec("select * from t2 where (a, b, c) in ((1, 1, 1), (2, 2, 2, 2));")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[expression:1241]Operand should contain 3 column(s)")

_, err = tk.Exec("select * from t2 where (a, b, c) in ((1, 1), (2, 2, 2));")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[expression:1241]Operand should contain 3 column(s)")
}

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) TestIssue20692(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int primary key, v int, vv int, vvv int, unique key u0(id, v, vv));")
tk.MustExec("insert into t values(1, 1, 1, 1);")
se1, err := session.CreateSession(s.store)
c.Assert(err, IsNil)
tk1 := testkit.NewTestKitWithSession(c, s.store, se1)
se2, err := session.CreateSession(s.store)
c.Assert(err, IsNil)
tk2 := testkit.NewTestKitWithSession(c, s.store, se2)
se3, err := session.CreateSession(s.store)
c.Assert(err, IsNil)
tk3 := testkit.NewTestKitWithSession(c, s.store, se3)
tk1.MustExec("begin pessimistic;")
tk1.MustExec("use test")
tk2.MustExec("begin pessimistic;")
tk2.MustExec("use test")
tk3.MustExec("begin pessimistic;")
tk3.MustExec("use test")
tk1.MustExec("delete from t where id = 1 and v = 1 and vv = 1;")
stop1, stop2 := make(chan struct{}), make(chan struct{})
go func() {
tk2.MustExec("insert into t values(1, 2, 3, 4);")
stop1 <- struct{}{}
tk3.MustExec("update t set id = 10, v = 20, vv = 30, vvv = 40 where id = 1 and v = 2 and vv = 3;")
stop2 <- struct{}{}
}()
tk1.MustExec("commit;")
<-stop1

// wait 50ms to ensure tk3 is blocked by tk2
select {
case <-stop2:
c.Fail()
case <-time.After(50 * time.Millisecond):
}

tk2.MustExec("commit;")
<-stop2
tk3.MustExec("commit;")
tk3.MustQuery("select * from t;").Check(testkit.Rows("10 20 30 40"))
}
>>>>>>> 52f696f7e... planner: add missing table lock check for fast plan (#20948)