From 886689cf066c2956a35211a0783c0ac3baaeac26 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 16 Sep 2021 15:26:43 +0800 Subject: [PATCH] executor: fix table id to partition id mapping in select lock executor (#26380) (#26631) --- executor/executor.go | 5 +++-- executor/partition_table_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 8dab312b09f1a..e02cb46ffc42e 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -929,8 +929,9 @@ func (e *SelectLockExec) Next(ctx context.Context, req *chunk.Chunk) error { if len(e.partitionedTable) > 0 { // Replace the table ID with partition ID. // The partition ID is returned as an extra column from the table reader. - offset := e.tblID2PIDColumnIndex[id] - physicalID = row.GetInt64(offset) + if offset, ok := e.tblID2PIDColumnIndex[id]; ok { + physicalID = row.GetInt64(offset) + } } for _, col := range cols { diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 5e9dcc289e04c..b43c4486c6c51 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3055,3 +3055,35 @@ func (s *partitionTableSuite) TestIssue25528(c *C) { tk.MustExec("INSERT INTO issue25528 (`c1`, `c2`, `c3`, `c4`) VALUES (1, 1, 1, 1) , (3, 3, 3, 3) , (2, 2, 2, 2) , (4, 4, 4, 4);") tk.MustQuery("select * from issue25528 where c1 in (3, 4) order by c2 for update;").Check(testkit.Rows("3 3 3 3", "4 4 4 4")) } + +func (s *partitionTableSuite) TestIssue26251(c *C) { + tk1 := testkit.NewTestKit(c, s.store) + tk1.MustExec("use test") + tk1.MustExec("create table tp (id int primary key) partition by range (id) (partition p0 values less than (100));") + tk1.MustExec("create table tn (id int primary key);") + tk1.MustExec("insert into tp values(1),(2);") + tk1.MustExec("insert into tn values(1),(2);") + + tk2 := testkit.NewTestKit(c, s.store) + tk2.MustExec("use test") + + tk1.MustExec("begin pessimistic") + tk1.MustQuery("select * from tp,tn where tp.id=tn.id and tn.id<=1 for update;").Check(testkit.Rows("1 1")) + + ch := make(chan struct{}, 1) + tk2.MustExec("begin pessimistic") + go func() { + // This query should block. + tk2.MustQuery("select * from tn where id=1 for update;").Check(testkit.Rows("1")) + ch <- struct{}{} + }() + + select { + case <-time.After(100 * time.Millisecond): + // Expected, query blocked, not finish within 100ms. + tk1.MustExec("rollback") + case <-ch: + // Unexpected, test fail. + c.Fail() + } +}