diff --git a/executor/write_test.go b/executor/write_test.go index b832e52a9935c..cf7a51985a450 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -3930,6 +3930,25 @@ func (s *testSerialSuite) TestIssue20840(c *C) { tk.MustExec("drop table t1") } +func (s *testSerialSuite) TestIssueInsertPrefixIndexForNonUTF8Collation(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + tk := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1, t2, t3") + tk.MustExec("create table t1 ( c_int int, c_str varchar(40) character set ascii collate ascii_bin, primary key(c_int, c_str(8)) clustered , unique key(c_str))") + tk.MustExec("create table t2 ( c_int int, c_str varchar(40) character set latin1 collate latin1_bin, primary key(c_int, c_str(8)) clustered , unique key(c_str))") + tk.MustExec("insert into t1 values (3, 'fervent brattain')") + tk.MustExec("insert into t2 values (3, 'fervent brattain')") + tk.MustExec("admin check table t1") + tk.MustExec("admin check table t2") + + tk.MustExec("create table t3 (x varchar(40) CHARACTER SET ascii COLLATE ascii_bin, UNIQUE KEY uk(x(4)))") + tk.MustExec("insert into t3 select 'abc '") + tk.MustGetErrCode("insert into t3 select 'abc d'", 1062) +} + func (s *testSerialSuite) TestIssue22496(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/tablecodec/tablecodec.go b/tablecodec/tablecodec.go index 215f8d05c27fa..de766831bc245 100644 --- a/tablecodec/tablecodec.go +++ b/tablecodec/tablecodec.go @@ -1292,6 +1292,7 @@ func TruncateIndexValue(v *types.Datum, idxCol *model.IndexColumn, tblCol *model if notStringType { return } + originalKind := v.Kind() isUTF8Charset := tblCol.Charset == charset.CharsetUTF8 || tblCol.Charset == charset.CharsetUTF8MB4 if isUTF8Charset && utf8.RuneCount(v.GetBytes()) > idxCol.Length { rs := bytes.Runes(v.GetBytes()) @@ -1303,7 +1304,7 @@ func TruncateIndexValue(v *types.Datum, idxCol *model.IndexColumn, tblCol *model } } else if !isUTF8Charset && len(v.GetBytes()) > idxCol.Length { v.SetBytes(v.GetBytes()[:idxCol.Length]) - if v.Kind() == types.KindString { + if originalKind == types.KindString { v.SetString(v.GetString(), tblCol.Collate) } }