Repository: pingcap/tidb (Go)

Issue:
The clustered index primary key conflict prompt information is not an index value, but a complete field value
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

create table t1(a int, b varchar(20), primary key(a,b(3)) clustered);
insert into t1 values(1,'aaaaa');
insert into t1 values(1,'aaaaa');

### 2. What did you expect to see? (Required)

insert fail，the error message is as follows：
ERROR 1062 (23000): Duplicate entry '1-aaa' for key 'PRIMARY'

### 3. What did you see instead (Required)

insert fail，the error message is as follows：
ERROR 1062 (23000): Duplicate entry '1-aaaaa' for key 'PRIMARY'

### 4. What is your TiDB version? (Required)

Release Version: v5.0.1
Edition: Community
Git Commit Hash: 1145e347d3469d8e89f88dce86f6926ca44b3cd8
Git Branch: heads/refs/tags/v5.0.1
UTC Build Time: 2021-04-23 05:51:17
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false




Discussion:
/assign
## 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
-->

#### 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
-->



Patch:
diff --git a/store/driver/txn/error.go b/store/driver/txn/error.go
index 39931357567be..cde513e9f1e7d 100644
--- a/store/driver/txn/error.go
+++ b/store/driver/txn/error.go
@@ -94,6 +94,9 @@ func extractKeyExistsErrFromHandle(key kv.Key, value []byte, tblInfo *model.Tabl
 		if err != nil {
 			return genKeyExistsError(name, key.String(), err)
 		}
+		if col.Length > 0 {
+			str = str[:col.Length]
+		}
 		valueStr = append(valueStr, str)
 	}
 	return genKeyExistsError(name, strings.Join(valueStr, "-"), nil)


Test patch:
diff --git a/executor/ddl_test.go b/executor/ddl_test.go
index c55908066de62..7d50b66e5a65c 100644
--- a/executor/ddl_test.go
+++ b/executor/ddl_test.go
@@ -1526,3 +1526,14 @@ func (s *testRecoverTable) TestRenameMultiTables(c *C) {
 	tk.MustExec("drop database rename2")
 	tk.MustExec("drop database rename3")
 }
+
+// See https://github.com/pingcap/tidb/issues/24582
+func (s *testSuite6) TestDuplicatedEntryErr(c *C) {
+	tk := testkit.NewTestKit(c, s.store)
+	tk.MustExec("use test")
+	tk.MustExec("drop table if exists t1;")
+	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.Exec("insert into t1 values(1,'aaaaa');")
+	c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-aaa' for key 'PRIMARY'")
+}