Skip to content

Commit

Permalink
planner: cut prefix index according the character set (pingcap#30694)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Dec 30, 2021
1 parent 8085c99 commit 24e7e5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
13 changes: 13 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5842,6 +5842,19 @@ func (s *testSuiteWithCliBaseCharset) TestCharsetFeatureCollation(c *C) {
tk.MustGetErrCode("select collation(concat(ascii_char collate ascii_bin, gbk_char)) from t;", mysql.ErrCantAggregate2collations)
}

func (s *testSuiteWithCliBaseCharset) TestCharsetWithPrefixIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20) charset gbk, b char(20) charset gbk, primary key (a(2)));")
tk.MustExec("insert into t values ('a', '中文'), ('中文', '中文'), ('一二三', '一二三'), ('b', '一二三');")
tk.MustQuery("select * from t").Check(testkit.Rows("a 中文", "中文 中文", "一二三 一二三", "b 一二三"))
tk.MustExec("drop table t")
tk.MustExec("create table t(a char(20) charset gbk, b char(20) charset gbk, unique index idx_a(a(2)));")
tk.MustExec("insert into t values ('a', '中文'), ('中文', '中文'), ('一二三', '一二三'), ('b', '一二三');")
tk.MustQuery("select * from t").Check(testkit.Rows("a 中文", "中文 中文", "一二三 一二三", "b 一二三"))
}

func (s *testSerialSuite2) TestIssue23567(c *C) {
tk := testkit.NewTestKit(c, s.store)
oriProbability := statistics.FeedbackProbability.Load()
Expand Down
26 changes: 14 additions & 12 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,21 +1300,23 @@ 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())
colValue := v.GetBytes()
if tblCol.Charset == charset.CharsetBin || tblCol.Charset == charset.CharsetASCII {
// Count character length by bytes if charset is binary or ascii.
if len(colValue) > idxCol.Length {
// truncate value and limit its length
if v.Kind() == types.KindBytes {
v.SetBytes(colValue[:idxCol.Length])
} else {
v.SetString(v.GetString()[:idxCol.Length], tblCol.Collate)
}
}
} else if utf8.RuneCount(colValue) > idxCol.Length {
// Count character length by characters for other rune-based charsets, they are all internally encoded as UTF-8.
rs := bytes.Runes(colValue)
truncateStr := string(rs[:idxCol.Length])
// truncate value and limit its length
v.SetString(truncateStr, tblCol.Collate)
if v.Kind() == types.KindBytes {
v.SetBytes(v.GetBytes())
}
} else if !isUTF8Charset && len(v.GetBytes()) > idxCol.Length {
v.SetBytes(v.GetBytes()[:idxCol.Length])
if originalKind == types.KindString {
v.SetString(v.GetString(), tblCol.Collate)
}
}
}

Expand Down

0 comments on commit 24e7e5d

Please sign in to comment.