From 434df767e50ded41ba0415a8c126393211f8ec28 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 11 Jul 2019 14:01:32 +0800 Subject: [PATCH 1/2] executor: fix a bug of 'insert on duplicate update' statement on partitioned table In this statement: insert into t1 set a=1,b=1 on duplicate key update a=1,b=1 Batch checker find a=1,b=1 is duplicated, then in the "on duplicate update" step, it use non-partitioned table to get the old row, which is a bug. getOldRow returns this error: (1105, u'can not be duplicated row, due to old row not found. handle 1 not found') --- executor/insert.go | 2 +- executor/insert_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/executor/insert.go b/executor/insert.go index 133b882a81d2a..c541ddce08a1b 100644 --- a/executor/insert.go +++ b/executor/insert.go @@ -162,7 +162,7 @@ func (e *InsertExec) Open(ctx context.Context) error { // updateDupRow updates a duplicate row to a new row. func (e *InsertExec) updateDupRow(row toBeCheckedRow, handle int64, onDuplicate []*expression.Assignment) error { - oldRow, err := e.getOldRow(e.ctx, e.Table, handle, e.GenExprs) + oldRow, err := e.getOldRow(e.ctx, row.t, handle, e.GenExprs) if err != nil { logutil.BgLogger().Error("get old row failed when insert on dup", zap.Int64("handle", handle), zap.String("toBeInsertedRow", types.DatumsToStrNoErr(row.row))) return err diff --git a/executor/insert_test.go b/executor/insert_test.go index 8c3510b044126..e26834572f12b 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -286,3 +286,17 @@ func (s *testSuite3) TestAllowInvalidDates(c *C) { runWithMode("STRICT_TRANS_TABLES,ALLOW_INVALID_DATES") runWithMode("ALLOW_INVALID_DATES") } + +func (s *testSuite3) TestPartitionInsertOnDuplicate(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec(`use test`) + tk.MustExec(`create table t1 (a int,b int,primary key(a,b)) partition by range(a) (partition p0 values less than (100),partition p1 values less than (1000))`) + tk.MustExec(`insert into t1 set a=1, b=1`) + tk.MustExec(`insert into t1 set a=1,b=1 on duplicate key update a=1,b=1`) + tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 1")) + + tk.MustExec(`create table t2 (a int,b int,primary key(a,b)) partition by hash(a) partitions 4`) + tk.MustExec(`insert into t2 set a=1,b=1;`) + tk.MustExec(`insert into t2 set a=1,b=1 on duplicate key update a=1,b=1`) + tk.MustQuery(`select * from t2`).Check(testkit.Rows("1 1")) +} From bdae5b0c8ce946b5b704a1831d411323d45c3940 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 11 Jul 2019 14:59:41 +0800 Subject: [PATCH 2/2] fix a data race --- executor/simple_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/executor/simple_test.go b/executor/simple_test.go index 440224cc494e5..0de9f78d714a0 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -413,14 +413,19 @@ func (s *testFlushSuite) TestFlushPrivilegesPanic(c *C) { c.Assert(err, IsNil) defer store.Close() - config.GetGlobalConfig().Security.SkipGrantTable = true + saveConf := config.GetGlobalConfig() + conf := config.NewConfig() + conf.Security.SkipGrantTable = true + config.StoreGlobalConfig(conf) + dom, err := session.BootstrapSession(store) c.Assert(err, IsNil) defer dom.Close() tk := testkit.NewTestKit(c, store) tk.MustExec("FLUSH PRIVILEGES") - config.GetGlobalConfig().Security.SkipGrantTable = false + + config.StoreGlobalConfig(saveConf) } func (s *testSuite3) TestDropStats(c *C) {