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

txn: add table name in dup entry error message #38243

Merged
merged 10 commits into from Oct 19, 2022
4 changes: 2 additions & 2 deletions ddl/db_integration_test.go
Expand Up @@ -2930,7 +2930,7 @@ func TestAutoIncrementForce(t *testing.T) {
require.Equal(t, uint64(1), getNextGlobalID())
// inserting new rows can overwrite the existing data.
tk.MustExec("insert into t values (3);")
require.Equal(t, "[kv:1062]Duplicate entry '2' for key 'PRIMARY'", tk.ExecToErr("insert into t values (3);").Error())
require.Equal(t, "[kv:1062]Duplicate entry '2' for key 't.PRIMARY'", tk.ExecToErr("insert into t values (3);").Error())
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3"))

// Rebase auto_increment.
Expand Down Expand Up @@ -3144,7 +3144,7 @@ func TestDuplicateErrorMessage(t *testing.T) {
fields[i] = strings.Replace(val, "'", "", -1)
}
tk.MustGetErrMsg("alter table t add unique index t_idx(id1,"+index+")",
fmt.Sprintf("[kv:1062]Duplicate entry '1-%s' for key 't_idx'", strings.Join(fields, "-")))
fmt.Sprintf("[kv:1062]Duplicate entry '1-%s' for key 't.t_idx'", strings.Join(fields, "-")))
}
}
restoreConfig()
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_test.go
Expand Up @@ -237,7 +237,7 @@ func TestJsonUnmarshalErrWhenPanicInCancellingPath(t *testing.T) {
tk.MustExec("create table test_add_index_after_add_col(a int, b int not null default '0');")
tk.MustExec("insert into test_add_index_after_add_col values(1, 2),(2,2);")
tk.MustExec("alter table test_add_index_after_add_col add column c int not null default '0';")
tk.MustGetErrMsg("alter table test_add_index_after_add_col add unique index cc(c);", "[kv:1062]Duplicate entry '0' for key 'cc'")
tk.MustGetErrMsg("alter table test_add_index_after_add_col add unique index cc(c);", "[kv:1062]Duplicate entry '0' for key 'test_add_index_after_add_col.cc'")
}

func TestIssue22819(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ddl/index.go
Expand Up @@ -1389,7 +1389,7 @@ func (w *addIndexWorker) checkHandleExists(key kv.Key, value []byte, handle kv.H
if err != nil {
return err
}
indexName := w.index.Meta().Name.String()
indexName := fmt.Sprintf("%s.%s", w.index.TableMeta().Name.String(), w.index.Meta().Name.String())
valueStr := make([]string, 0, idxColLen)
for i, val := range values[:idxColLen] {
d, err := tablecodec.DecodeColumnValue(val, colInfos[i].Ft, time.Local)
Expand Down
4 changes: 2 additions & 2 deletions ddl/index_modify_test.go
Expand Up @@ -374,7 +374,7 @@ func TestAddIndexForGeneratedColumn(t *testing.T) {
func TestAddPrimaryKeyRollback1(t *testing.T) {
idxName := "PRIMARY"
addIdxSQL := "alter table t1 add primary key c3_index (c3);"
errMsg := "[kv:1062]Duplicate entry '" + strconv.Itoa(defaultBatchSize*2-10) + "' for key 'PRIMARY'"
errMsg := "[kv:1062]Duplicate entry '" + strconv.Itoa(defaultBatchSize*2-10) + "' for key 't1.PRIMARY'"
testAddIndexRollback(t, idxName, addIdxSQL, errMsg, false)
}

Expand All @@ -389,7 +389,7 @@ func TestAddPrimaryKeyRollback2(t *testing.T) {
func TestAddUniqueIndexRollback(t *testing.T) {
idxName := "c3_index"
addIdxSQL := "create unique index c3_index on t1 (c3)"
errMsg := "[kv:1062]Duplicate entry '" + strconv.Itoa(defaultBatchSize*2-10) + "' for key 'c3_index'"
errMsg := "[kv:1062]Duplicate entry '" + strconv.Itoa(defaultBatchSize*2-10) + "' for key 't1.c3_index'"
testAddIndexRollback(t, idxName, addIdxSQL, errMsg, false)
}

Expand Down
5 changes: 3 additions & 2 deletions executor/batch_checker.go
Expand Up @@ -16,6 +16,7 @@ package executor

import (
"context"
"fmt"
"strings"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -141,7 +142,7 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
}
handleKey = &keyValueWithDupInfo{
newKey: tablecodec.EncodeRecordKey(t.RecordPrefix(), handle),
dupErr: kv.ErrKeyExists.FastGenByArgs(stringutil.MemoizeStr(fn), "PRIMARY"),
dupErr: kv.ErrKeyExists.FastGenByArgs(stringutil.MemoizeStr(fn), t.Meta().Name.String()+".PRIMARY"),
}
}

Expand Down Expand Up @@ -185,7 +186,7 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
}
uniqueKeys = append(uniqueKeys, &keyValueWithDupInfo{
newKey: key,
dupErr: kv.ErrKeyExists.FastGenByArgs(colValStr, v.Meta().Name),
dupErr: kv.ErrKeyExists.FastGenByArgs(colValStr, fmt.Sprintf("%s.%s", v.TableMeta().Name.String(), v.Meta().Name.String())),
commonHandle: t.Meta().IsCommonHandle,
})
}
Expand Down
2 changes: 1 addition & 1 deletion executor/ddl_test.go
Expand Up @@ -80,7 +80,7 @@ func TestInTxnExecDDLFail(t *testing.T) {
tk.MustExec("begin;")
tk.MustExec("insert into t values (1);")
_, err := tk.Exec("truncate table t;")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 't.PRIMARY'")
result := tk.MustQuery("select count(*) from t")
result.Check(testkit.Rows("1"))
}
Expand Down
6 changes: 3 additions & 3 deletions executor/executor_test.go
Expand Up @@ -2825,7 +2825,7 @@ func TestInsertIntoGivenPartitionSet(t *testing.T) {
tk.MustExec("insert into t1 partition(p0, p1) values(3, 'c'), (4, 'd')")
tk.MustQuery("select * from t1 partition(p1)").Check(testkit.Rows())

tk.MustGetErrMsg("insert into t1 values(1, 'a')", "[kv:1062]Duplicate entry '1' for key 'idx_a'")
tk.MustGetErrMsg("insert into t1 values(1, 'a')", "[kv:1062]Duplicate entry '1' for key 't1.idx_a'")
tk.MustGetErrMsg("insert into t1 partition(p0, p_non_exist) values(1, 'a')", "[table:1735]Unknown partition 'p_non_exist' in table 't1'")
tk.MustGetErrMsg("insert into t1 partition(p0, p1) values(40, 'a')", "[table:1748]Found a row not matching the given partition set")

Expand Down Expand Up @@ -2856,7 +2856,7 @@ func TestInsertIntoGivenPartitionSet(t *testing.T) {
tk.MustQuery("select * from t1 partition(p1) order by a").Check(testkit.Rows())
tk.MustQuery("select * from t1 partition(p0) order by a").Check(testkit.Rows("1 a", "2 b", "3 c", "4 d"))

tk.MustGetErrMsg("insert into t1 select 1, 'a'", "[kv:1062]Duplicate entry '1' for key 'idx_a'")
tk.MustGetErrMsg("insert into t1 select 1, 'a'", "[kv:1062]Duplicate entry '1' for key 't1.idx_a'")
tk.MustGetErrMsg("insert into t1 partition(p0, p_non_exist) select 1, 'a'", "[table:1735]Unknown partition 'p_non_exist' in table 't1'")
tk.MustGetErrMsg("insert into t1 partition(p0, p1) select 40, 'a'", "[table:1748]Found a row not matching the given partition set")

Expand Down Expand Up @@ -3022,7 +3022,7 @@ func TestPrevStmtDesensitization(t *testing.T) {
tk.MustExec("begin")
tk.MustExec("insert into t values (1),(2)")
require.Equal(t, "insert into `t` values ( ? ) , ( ? )", tk.Session().GetSessionVars().PrevStmt.String())
tk.MustGetErrMsg("insert into t values (1)", `[kv:1062]Duplicate entry '?' for key 'a'`)
tk.MustGetErrMsg("insert into t values (1)", `[kv:1062]Duplicate entry '?' for key 't.a'`)
}

func TestIssue19372(t *testing.T) {
Expand Down
32 changes: 16 additions & 16 deletions executor/insert_test.go
Expand Up @@ -349,7 +349,7 @@ func TestUpdateDuplicateKey(t *testing.T) {
tk.MustExec(`insert into c values(1,2,3);`)
tk.MustExec(`insert into c values(1,2,4);`)
tk.MustGetErrMsg(`update c set i=1,j=2,k=4 where i=1 and j=2 and k=3;`,
"[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'")
"[kv:1062]Duplicate entry '1-2-4' for key 'c.PRIMARY'")
}

func TestIssue37187(t *testing.T) {
Expand Down Expand Up @@ -1394,7 +1394,7 @@ func TestIssue16366(t *testing.T) {
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(c numeric primary key);`)
tk.MustExec("insert ignore into t values(null);")
tk.MustContainErrMsg(`insert into t values(0);`, "Duplicate entry '0' for key 'PRIMARY'")
tk.MustContainErrMsg(`insert into t values(0);`, "Duplicate entry '0' for key 't.PRIMARY'")
}

func TestClusterPrimaryTablePlainInsert(t *testing.T) {
Expand Down Expand Up @@ -1556,60 +1556,60 @@ func TestDuplicateEntryMessage(t *testing.T) {
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b char(10), unique key(b)) collate utf8mb4_general_ci;")
tk.MustExec("insert into t value (34, '12Ak');")
tk.MustGetErrMsg("insert into t value (34, '12Ak');", "[kv:1062]Duplicate entry '12Ak' for key 'b'")
tk.MustGetErrMsg("insert into t value (34, '12Ak');", "[kv:1062]Duplicate entry '12Ak' for key 't.b'")

tk.MustExec("begin optimistic;")
tk.MustExec("insert into t value (34, '12ak');")
tk.MustExec("delete from t where b = '12ak';")
tk.MustGetErrMsg("commit;", "previous statement: delete from t where b = '12ak';: [kv:1062]Duplicate entry '12ak' for key 'b'")
tk.MustGetErrMsg("commit;", "previous statement: delete from t where b = '12ak';: [kv:1062]Duplicate entry '12ak' for key 't.b'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a datetime primary key);")
tk.MustExec("insert into t values ('2020-01-01');")
tk.MustGetErrMsg("insert into t values ('2020-01-01');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 'PRIMARY'")
tk.MustGetErrMsg("insert into t values ('2020-01-01');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 't.PRIMARY'")

tk.MustExec("begin optimistic;")
tk.MustExec("insert into t values ('2020-01-01');")
tk.MustExec("delete from t where a = '2020-01-01';")
tk.MustGetErrMsg("commit;", "previous statement: delete from t where a = '2020-01-01';: [kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 'PRIMARY'")
tk.MustGetErrMsg("commit;", "previous statement: delete from t where a = '2020-01-01';: [kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 't.PRIMARY'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int primary key );")
tk.MustExec("insert into t value (1);")
tk.MustGetErrMsg("insert into t value (1);", "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
tk.MustGetErrMsg("insert into t value (1);", "[kv:1062]Duplicate entry '1' for key 't.PRIMARY'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a datetime unique);")
tk.MustExec("insert into t values ('2020-01-01');")
tk.MustGetErrMsg("insert into t values ('2020-01-01');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 'a'")
tk.MustGetErrMsg("insert into t values ('2020-01-01');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00' for key 't.a'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a datetime, b int, c varchar(10), primary key (a, b, c)) collate utf8mb4_general_ci;")
tk.MustExec("insert into t values ('2020-01-01', 1, 'aSDd');")
tk.MustGetErrMsg("insert into t values ('2020-01-01', 1, 'ASDD');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00-1-ASDD' for key 'PRIMARY'")
tk.MustGetErrMsg("insert into t values ('2020-01-01', 1, 'ASDD');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00-1-ASDD' for key 't.PRIMARY'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a datetime, b int, c varchar(10), unique key (a, b, c)) collate utf8mb4_general_ci;")
tk.MustExec("insert into t values ('2020-01-01', 1, 'aSDd');")
tk.MustGetErrMsg("insert into t values ('2020-01-01', 1, 'ASDD');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00-1-ASDD' for key 'a'")
tk.MustGetErrMsg("insert into t values ('2020-01-01', 1, 'ASDD');", "[kv:1062]Duplicate entry '2020-01-01 00:00:00-1-ASDD' for key 't.a'")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a char(10) collate utf8mb4_unicode_ci, b char(20) collate utf8mb4_general_ci, c int(11), primary key (a, b, c), unique key (a));")
tk.MustExec("insert ignore into t values ('$', 'C', 10);")
tk.MustExec("insert ignore into t values ('$', 'C', 10);")
tk.MustQuery("show warnings;").Check(testkit.RowsWithSep("|", "Warning|1062|Duplicate entry '$-C-10' for key 'PRIMARY'"))
tk.MustQuery("show warnings;").Check(testkit.RowsWithSep("|", "Warning|1062|Duplicate entry '$-C-10' for key 't.PRIMARY'"))

tk.MustExec("begin pessimistic;")
tk.MustExec("insert into t values ('a7', 'a', 10);")
tk.MustGetErrMsg("insert into t values ('a7', 'a', 10);", "[kv:1062]Duplicate entry 'a7-a-10' for key 'PRIMARY'")
tk.MustGetErrMsg("insert into t values ('a7', 'a', 10);", "[kv:1062]Duplicate entry 'a7-a-10' for key 't.PRIMARY'")
tk.MustExec("rollback;")

// Test for large unsigned integer handle.
// See https://github.com/pingcap/tidb/issues/12420.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a bigint unsigned primary key);")
tk.MustExec("insert into t values(18446744073709551615);")
tk.MustGetErrMsg("insert into t values(18446744073709551615);", "[kv:1062]Duplicate entry '18446744073709551615' for key 'PRIMARY'")
tk.MustGetErrMsg("insert into t values(18446744073709551615);", "[kv:1062]Duplicate entry '18446744073709551615' for key 't.PRIMARY'")
}
}

Expand Down Expand Up @@ -1675,12 +1675,12 @@ func TestDuplicatedEntryErr(t *testing.T) {
tk.MustExec("create table t1(a int, b varchar(20), primary key(a,b(3)) clustered);")
tk.MustExec("insert into t1 values(1,'aaaaa');")
err := tk.ExecToErr("insert into t1 values(1,'aaaaa');")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-aaa' for key 'PRIMARY'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-aaa' for key 't1.PRIMARY'")
err = tk.ExecToErr("insert into t1 select 1, 'aaa'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-aaa' for key 'PRIMARY'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-aaa' for key 't1.PRIMARY'")
tk.MustExec("insert into t1 select 1, 'bb'")
err = tk.ExecToErr("insert into t1 select 1, 'bb'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-bb' for key 'PRIMARY'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '1-bb' for key 't1.PRIMARY'")
}

func TestBinaryLiteralInsertToEnum(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions executor/union_scan_test.go
Expand Up @@ -223,7 +223,7 @@ func TestUnionScanForMemBufferReader(t *testing.T) {
tk.MustExec("create table t (a int,b int, unique index idx(b))")
tk.MustExec("insert t values (1,1),(2,2)")
tk.MustExec("begin")
tk.MustGetErrMsg("update t set b=b+1", "[kv:1062]Duplicate entry '2' for key 'idx'")
tk.MustGetErrMsg("update t set b=b+1", "[kv:1062]Duplicate entry '2' for key 't.idx'")
// update with unchange index column.
tk.MustExec("update t set a=a+1")
tk.MustQuery("select * from t use index (idx)").Check(testkit.Rows("2 1", "3 2"))
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestForUpdateUntouchedIndex(t *testing.T) {
tk.MustExec("begin")
_, err := tk.Exec("insert into t values (1, 1), (2, 2), (1, 3) on duplicate key update a = a + 1;")
require.NotNil(t, err)
require.EqualError(t, err, "[kv:1062]Duplicate entry '2' for key 'a'")
require.EqualError(t, err, "[kv:1062]Duplicate entry '2' for key 't.a'")
tk.MustExec("commit")
tk.MustExec("admin check table t")
}
Expand Down