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

tablecodec: fix write wrong prefix index value when collation is ascii_bin/latin1_bin #24578

Merged
merged 6 commits into from
May 17, 2021
19 changes: 19 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
}
}
Expand Down