Repository: pingcap/tidb (Go)

Issue:
bigint unsigned primary key duplicate error info is not correct
## Bug Report

Please answer these questions before submitting your issue. Thanks!

1. What did you do?
If possible, provide a recipe for reproducing the error.

Check the below symptoms.

2. What did you expect to see?
On MySQL:

```
mysql> create table t(a bigint unsigned primary key);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into t values(18446744073709551615);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t values(18446744073709551615);
ERROR 1062 (23000): Duplicate entry '18446744073709551615' for key 'PRIMARY'
```


3. What did you see instead?
On TiDB:

```
mysql> create table t(a bigint unsigned primary key);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t values(18446744073709551615);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t values(18446744073709551615);
ERROR 1062 (23000): Duplicate entry '-1' for key 'PRIMARY'
mysql> select * from t;
+----------------------+
| a                    |
+----------------------+
| 18446744073709551615 |
+----------------------+
1 row in set (0.00 sec)
```

4. What version of TiDB are you using (`tidb-server -V` or run `select tidb_version();` on TiDB)?

```
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-alpha-367-gde2df75a3-dirty
Git Commit Hash: de2df75a3e3cbebfa03e0d1ab33d6b70e6228eee
Git Branch: master
UTC Build Time: 2019-09-26 09:29:39
GoVersion: go version go1.12.9 darwin/amd64
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)
```


Discussion:
Found this bug during review https://github.com/pingcap/tidb/issues/10056 which has been fixed by https://github.com/pingcap/tidb/pull/10113. Hope this might help.
> Found this bug during review #10056 which has been fixed by #10113. Hope this might help.

So can we close issue?
> > Found this bug during review #10056 which has been fixed by #10113. Hope this might help.
> 
> So can we close issue?

Sry if I haven't cleared this out. I mean https://github.com/pingcap/tidb/pull/10113 fixed https://github.com/pingcap/tidb/issues/10056. But it doesn't fix this bug. They may related to this one.
Confirming issue still exists against master (it's minor):
```sql
drop table if exists t;
create table t(a bigint unsigned primary key);
insert into t values(18446744073709551615);
insert into t values(18446744073709551615);

..


mysql> insert into t values(18446744073709551615);
ERROR 1062 (23000): Duplicate entry '-1' for key 'PRIMARY'
mysql> 
mysql> SELECT tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-821-gfe07417e6
Edition: Community
Git Commit Hash: fe07417e6b2f28eb334117d44640d0760d7e9866
Git Branch: master
UTC Build Time: 2020-07-22 12:51:37
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)
```
## Please edit this comment or add a new comment to complete the following information

### Not a bug

1. Remove the 'type/bug' label
2. Add notes to indicate why it is not a bug

### Duplicate bug

1. Add the 'type/duplicate' label
2. Add the link to the original bug

### Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: https://github.com/pingcap/tidb/issues/20100

#### 1. Root Cause Analysis (RCA) (optional) 
<!-- Write down the reason why this bug occurs -->

#### 2. Symptom (optional)

<!-- What will the user see when this bug occurs. The error message may be in the terminal, log or monitoring -->

#### 3. All Trigger Conditions (optional)

<!-- All the user scenarios that may trigger this bug -->

#### 4. Workaround (optional)

#### 5. Affected versions

<!--
In the format of [start_version:end_version], multiple version ranges are
accepted. If the bug only affects the unreleased version, please input:
"unreleased". For example:

Notes:
  1. Do not use any white spaces in '[]'.
  2. The range in '[]' is a closed interval
  3. The version format is `v$Major.$Minor.$Patch`, the $Majoy and $Minor
     number in a version range should be the same. [v3.0.1:v3.1.2] is
     invalid because the $Minor number of the version range is different.

Example 1: [v3.0.1:v3.0.5], [v4.0.1:v4.0.5]
Example 2: unreleased
-->
 [v3.0.0:v3.0.20], [v4.0.0:v4.0.10], [v5.0.0-rc]

#### 6. Fixed versions

<!--
The first released version that contains this fix in each minor version. If the bug's affected version has been released, the fixed version should be a detailed version number; If the bug doesn't affect any released version, the fixed version can be "master";  

Example 1: v3.0.13, v4.0.5
Example 2: master
-->
master
The values in **( FixedVersions )** fields are incorrect.
The values in **( FixedVersions )** fields are incorrect.
The values in **( FixedVersions )** fields are incorrect.



Patch:
diff --git a/store/tikv/2pc.go b/store/tikv/2pc.go
index fcb7a5e6916ac..424aabd19c7cc 100644
--- a/store/tikv/2pc.go
+++ b/store/tikv/2pc.go
@@ -17,6 +17,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/hex"
+	"fmt"
 	"math"
 	"math/rand"
 	"strings"
@@ -403,6 +404,12 @@ func (c *twoPhaseCommitter) extractKeyExistsErrFromHandle(key kv.Key, value []by
 	}
 
 	if handle.IsInt() {
+		if pkInfo := tblInfo.GetPkColInfo(); pkInfo != nil {
+			if mysql.HasUnsignedFlag(pkInfo.Flag) {
+				handleStr := fmt.Sprintf("%d", uint64(handle.IntValue()))
+				return c.genKeyExistsError(name, handleStr, nil)
+			}
+		}
 		return c.genKeyExistsError(name, handle.String(), nil)
 	}
 


Test patch:
diff --git a/executor/insert_test.go b/executor/insert_test.go
index b2ddb3a784cbe..73f2914756209 100644
--- a/executor/insert_test.go
+++ b/executor/insert_test.go
@@ -1506,6 +1506,13 @@ func (s *testSerialSuite) TestDuplicateEntryMessage(c *C) {
 		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.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'")
 	}
 }