Skip to content

Commit

Permalink
tablecodec: fix write wrong prefix index value when collation is asci…
Browse files Browse the repository at this point in the history
…i_bin/latin1_bin (#24578) (#24680)
  • Loading branch information
ti-srebot authored May 26, 2021
1 parent 41c0f17 commit 2c66561
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 19 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3931,6 +3931,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

0 comments on commit 2c66561

Please sign in to comment.