Skip to content

Commit 47444dd

Browse files
committed
address commnet
1 parent 7efcb83 commit 47444dd

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

pkg/ttl/cache/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
"//pkg/infoschema",
1818
"//pkg/kv",
1919
"//pkg/parser/ast",
20+
"//pkg/parser/charset",
2021
"//pkg/parser/model",
2122
"//pkg/parser/mysql",
2223
"//pkg/parser/terror",

pkg/ttl/cache/split_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,9 @@ func TestSplitTTLScanRangesWithBytes(t *testing.T) {
517517
createTTLTable(t, tk, "t3", "varchar(32) CHARACTER SET BINARY"),
518518
createTTLTable(t, tk, "t4", "bit(32)"),
519519
create2PKTTLTable(t, tk, "t5", "binary(32)"),
520-
createTTLTable(t, tk, "t6", "char(32) CHARACTER SET UTF8MB4"),
521-
create2PKTTLTable(t, tk, "t7", "char(32) CHARACTER SET gbk"),
520+
createTTLTable(t, tk, "t6", "char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"),
521+
createTTLTable(t, tk, "t7", "char(32) CHARACTER SET utf8 COLLATE utf8_bin"),
522+
create2PKTTLTable(t, tk, "t8", "char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin"),
522523
}
523524

524525
cases := []struct {
@@ -656,6 +657,8 @@ func TestNoTTLSplitSupportTables(t *testing.T) {
656657
createTTLTable(t, tk, "t2", "date"),
657658
createTTLTable(t, tk, "t3", "datetime"),
658659
createTTLTable(t, tk, "t4", "timestamp"),
660+
createTTLTable(t, tk, "t5", "varchar(32) character set utf8mb4 collate utf8mb4_general_ci"),
661+
createTTLTable(t, tk, "t6", "varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci"),
659662
}
660663

661664
tikvStore := newMockTiKVStore(t)

pkg/ttl/cache/table.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/pingcap/tidb/pkg/expression/contextstatic"
2828
"github.com/pingcap/tidb/pkg/kv"
2929
"github.com/pingcap/tidb/pkg/parser/ast"
30+
"github.com/pingcap/tidb/pkg/parser/charset"
3031
"github.com/pingcap/tidb/pkg/parser/model"
3132
"github.com/pingcap/tidb/pkg/parser/mysql"
3233
"github.com/pingcap/tidb/pkg/parser/terror"
@@ -287,13 +288,31 @@ func (t *PhysicalTable) SplitScanRanges(ctx context.Context, store kv.Storage, s
287288
switch ft.GetType() {
288289
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeInt24:
289290
if len(t.KeyColumns) > 1 {
290-
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, true, mysql.HasUnsignedFlag(ft.GetFlag()), false)
291+
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, true, mysql.HasUnsignedFlag(ft.GetFlag()), nil)
291292
}
292293
return t.splitIntRanges(ctx, tikvStore, splitCnt)
293294
case mysql.TypeBit:
294-
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, false, false, true)
295+
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, false, false, nil)
295296
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar:
296-
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, false, false, mysql.HasBinaryFlag(ft.GetFlag()))
297+
var decode func([]byte) types.Datum
298+
if !mysql.HasBinaryFlag(ft.GetFlag()) {
299+
switch ft.GetCharset() {
300+
case charset.CharsetASCII, charset.CharsetLatin1:
301+
// ASCII and Latin1 are 8-bit charset, we can use GetASCIIPrefixDatumFromBytes to decode it.
302+
decode = GetASCIIPrefixDatumFromBytes
303+
case charset.CharsetUTF8, charset.CharsetUTF8MB4:
304+
switch ft.GetCollate() {
305+
case charset.CollationUTF8, charset.CollationUTF8MB4, "utf8mb4_0900_bin":
306+
// We can only use GetASCIIPrefixDatumFromBytes to decode UTF8 and UTF8MB4 when they are
307+
// "utf8_bin" or "utf8mb4_bin" collation.
308+
decode = GetASCIIPrefixDatumFromBytes
309+
}
310+
}
311+
if decode == nil {
312+
return []ScanRange{newFullRange()}, nil
313+
}
314+
}
315+
return t.splitCommonHandleRanges(ctx, tikvStore, splitCnt, false, false, decode)
297316
}
298317
return []ScanRange{newFullRange()}, nil
299318
}
@@ -363,7 +382,7 @@ func (t *PhysicalTable) splitIntRanges(ctx context.Context, store tikv.Storage,
363382
}
364383

365384
func (t *PhysicalTable) splitCommonHandleRanges(
366-
ctx context.Context, store tikv.Storage, splitCnt int, isInt bool, unsigned bool, binary bool,
385+
ctx context.Context, store tikv.Storage, splitCnt int, isInt bool, unsigned bool, decode func([]byte) types.Datum,
367386
) ([]ScanRange, error) {
368387
recordPrefix := tablecodec.GenTableRecordPrefix(t.ID)
369388
startKey, endKey := recordPrefix, recordPrefix.PrefixNext()
@@ -385,8 +404,8 @@ func (t *PhysicalTable) splitCommonHandleRanges(
385404
curScanEnd = GetNextIntDatumFromCommonHandle(keyRange.EndKey, recordPrefix, unsigned)
386405
} else {
387406
curScanEnd = GetNextBytesHandleDatum(keyRange.EndKey, recordPrefix)
388-
if !binary {
389-
curScanEnd = GetASCIIPrefixDatumFromBytes(curScanEnd.GetBytes())
407+
if decode != nil {
408+
curScanEnd = decode(curScanEnd.GetBytes())
390409
}
391410

392411
// "" is the smallest value for string/[]byte, skip to add it to ranges.

0 commit comments

Comments
 (0)