From 65376ebdf9eb564abcf4ff147d2c2cc4e83a378e Mon Sep 17 00:00:00 2001 From: winkyao Date: Mon, 27 Aug 2018 16:26:30 +0800 Subject: [PATCH 1/2] tables: make duplicate error in update clearer --- executor/insert_test.go | 12 ++++++++++++ table/tables/tables.go | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/executor/insert_test.go b/executor/insert_test.go index 6d928ae1420c5..b6fa315ba2699 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -83,6 +83,18 @@ func (s *testSuite) TestInsertOnDuplicateKey(c *C) { tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400")) } +func (s *testSuite) TestUpdateDuplicateKey(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + + tk.MustExec(`drop table if exists t;`) + tk.MustExec(`create table c(i int,j int,k int,primary key(i,j,k));`) + tk.MustExec(`insert into c values(1,2,3);`) + tk.MustExec(`insert into c values(1,2,4);`) + _, err := tk.Exec(`update c set i=1,j=2,k=4 where i=1 and j=2 and k=3;`) + c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'") +} + func (s *testSuite) TestInsertWrongValueForField(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/table/tables/tables.go b/table/tables/tables.go index 37bd31a5ce788..4cfc7a85303a1 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -748,6 +748,16 @@ func (t *tableCommon) removeRowIndex(sc *stmtctx.StatementContext, rm kv.Retriev // buildIndexForRow implements table.Table BuildIndexForRow interface. func (t *tableCommon) buildIndexForRow(ctx sessionctx.Context, rm kv.RetrieverMutator, h int64, vals []types.Datum, idx table.Index) error { if _, err := idx.Create(ctx, rm, vals, h); err != nil { + if kv.ErrKeyExists.Equal(err) { + // Make error message consistent with MySQL. + entryKey, err1 := t.genIndexKeyStr(vals) + if err1 != nil { + // if genIndexKeyStr failed, return the original error. + return errors.Trace(err) + } + + return kv.ErrKeyExists.FastGen("Duplicate entry '%s' for key '%s'", entryKey, idx.Meta().Name) + } return errors.Trace(err) } return nil From 1be5f428d0960d445e333ffc46ee027fd8aff09b Mon Sep 17 00:00:00 2001 From: winkyao Date: Mon, 27 Aug 2018 16:44:48 +0800 Subject: [PATCH 2/2] fix ci --- executor/write_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/write_test.go b/executor/write_test.go index 9d97a4ba5d7d0..3ebdcbff86076 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -997,7 +997,7 @@ func (s *testSuite) TestUpdate(c *C) { _, err = tk.Exec("update ignore t set a = 1 where a = 2;") c.Assert(err, IsNil) r = tk.MustQuery("SHOW WARNINGS;") - r.Check(testkit.Rows("Warning 1062 key already exist")) + r.Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'I_uniq'")) tk.MustQuery("select * from t").Check(testkit.Rows("1", "2")) tk.MustExec("drop table if exists t")