From 702bed1ae4c371d3203ce8801efa59cc1394dac6 Mon Sep 17 00:00:00 2001 From: lysu Date: Fri, 13 Aug 2021 02:05:16 +0800 Subject: [PATCH] tables: fix the wrong result for "insert ignore duplicate up" on partition table when handle changed (#25859) --- executor/write_test.go | 6 ++++++ table/tables/tables.go | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/executor/write_test.go b/executor/write_test.go index 0334430b7a3c9..b97eccabb79f2 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -806,6 +806,12 @@ func (s *testSuite4) TestInsertIgnoreOnDup(c *C) { tk.MustQuery("select * from t6").Check(testkit.Rows("100 10 20")) tk.MustExec("insert ignore into t6 set a = 200, b= 10 on duplicate key update c = 1000") tk.MustQuery("select * from t6").Check(testkit.Rows("100 10 1000")) + + tk.MustExec("drop table if exists t7") + tk.MustExec("CREATE TABLE t7 (`col_334` mediumint(9) NOT NULL DEFAULT '-3217641', `col_335` mediumint(8) unsigned NOT NULL DEFAULT '2002468', `col_336` enum('alice','bob','charlie','david') COLLATE utf8_general_ci NOT NULL DEFAULT 'alice', PRIMARY KEY (`col_334`,`col_336`,`col_335`) CLUSTERED, UNIQUE KEY `idx_116` (`col_334`,`col_335`), UNIQUE KEY `idx_117` (`col_336`,`col_334`), KEY `idx_118` (`col_336`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci PARTITION BY HASH( `col_334` ) PARTITIONS 6;") + tk.MustExec("insert into t7(col_335, col_336) values(7685969, 'alice'),(2002468, 'bob')") + tk.MustExec("insert ignore into t7(col_335, col_336) values(2002468, 'david') on duplicate key update col_335 = 7685969") + tk.MustQuery("select * from t7").Check(testkit.Rows("-3217641 7685969 alice", "-3217641 2002468 bob")) } func (s *testSuite4) TestInsertSetWithDefault(c *C) { diff --git a/table/tables/tables.go b/table/tables/tables.go index 0746f0e6f33e5..913de778a4eb8 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -1451,6 +1451,7 @@ func FindIndexByColName(t table.Table, name string) table.Index { // otherwise return kv.ErrKeyExists error. func CheckHandleOrUniqueKeyExistForUpdateIgnoreOrInsertOnDupIgnore(ctx context.Context, sctx sessionctx.Context, t table.Table, recordID kv.Handle, newRow []types.Datum, modified []bool) error { physicalTableID := t.Meta().ID + idxs := t.Indices() if pt, ok := t.(*partitionedTable); ok { info := t.Meta().GetPartitionInfo() pid, err := pt.locatePartition(sctx, info, newRow) @@ -1459,6 +1460,7 @@ func CheckHandleOrUniqueKeyExistForUpdateIgnoreOrInsertOnDupIgnore(ctx context.C } partition := pt.GetPartition(pid) physicalTableID = partition.GetPhysicalID() + idxs = partition.Indices() } txn, err := sctx.Txn(true) if err != nil { @@ -1492,7 +1494,7 @@ func CheckHandleOrUniqueKeyExistForUpdateIgnoreOrInsertOnDupIgnore(ctx context.C return true } - for _, idx := range t.Indices() { + for _, idx := range idxs { if shouldSkipIgnoreCheck(idx) { continue }