From d73fc6141faf52f5f6b60e2c445314bb4a63acd9 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Wed, 6 Mar 2024 15:31:18 +0800 Subject: [PATCH] address comments --- pkg/ttl/cache/BUILD.bazel | 1 + pkg/ttl/cache/split_test.go | 82 +++++++++++++++++++++++-------------- pkg/ttl/cache/table.go | 23 +++++------ 3 files changed, 64 insertions(+), 42 deletions(-) diff --git a/pkg/ttl/cache/BUILD.bazel b/pkg/ttl/cache/BUILD.bazel index fff21f1344734..5725cd26f53d4 100644 --- a/pkg/ttl/cache/BUILD.bazel +++ b/pkg/ttl/cache/BUILD.bazel @@ -25,6 +25,7 @@ go_library( "//pkg/types", "//pkg/util/chunk", "//pkg/util/codec", + "//pkg/util/collate", "//pkg/util/intest", "//pkg/util/logutil", "@com_github_pingcap_errors//:errors", diff --git a/pkg/ttl/cache/split_test.go b/pkg/ttl/cache/split_test.go index ae48da3127e0f..935b97d342151 100644 --- a/pkg/ttl/cache/split_test.go +++ b/pkg/ttl/cache/split_test.go @@ -986,7 +986,7 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { }, { key: encode(tblID, types.NewIntDatum(math.MaxInt64))[:fixedLen-1], - d: types.NewDatum(math.MaxInt64 - 0xFF), + d: types.NewIntDatum(math.MaxInt64 - 0xFF), }, { key: encode(tblID, types.NewIntDatum(math.MaxInt64), types.NewIntDatum(0)), @@ -1009,32 +1009,39 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { d: types.NewIntDatum(math.MinInt64 + 1), }, { - key: encode(tblID, types.NewUintDatum(0)), - d: types.NewUintDatum(0), + key: encode(tblID, types.NewUintDatum(0)), + d: types.NewUintDatum(0), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(1)), - d: types.NewUintDatum(1), + key: encode(tblID, types.NewUintDatum(1)), + d: types.NewUintDatum(1), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(1024)), - d: types.NewUintDatum(1024), + key: encode(tblID, types.NewUintDatum(1024)), + d: types.NewUintDatum(1024), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(math.MaxInt64)), - d: types.NewUintDatum(math.MaxInt64), + key: encode(tblID, types.NewUintDatum(math.MaxInt64)), + d: types.NewUintDatum(math.MaxInt64), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(math.MaxInt64+1)), - d: types.NewUintDatum(math.MaxInt64 + 1), + key: encode(tblID, types.NewUintDatum(math.MaxInt64+1)), + d: types.NewUintDatum(math.MaxInt64 + 1), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(math.MaxUint64)), - d: types.NewUintDatum(math.MaxUint64), + key: encode(tblID, types.NewUintDatum(math.MaxUint64)), + d: types.NewUintDatum(math.MaxUint64), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(math.MaxUint64))[:fixedLen-1], - d: types.NewUintDatum(math.MaxUint64 - 0xFF), + key: encode(tblID, types.NewUintDatum(math.MaxUint64))[:fixedLen-1], + d: types.NewUintDatum(math.MaxUint64 - 0xFF), + unsigned: true, }, { @@ -1042,32 +1049,37 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { d: nullDatum, }, { - key: encode(tblID, types.NewUintDatum(math.MaxUint64-1), types.NewIntDatum(0)), - d: types.NewUintDatum(math.MaxUint64), + key: encode(tblID, types.NewUintDatum(math.MaxUint64-1), types.NewIntDatum(0)), + d: types.NewUintDatum(math.MaxUint64), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(123), types.NewIntDatum(0)), - d: types.NewUintDatum(124), + key: encode(tblID, types.NewUintDatum(123), types.NewIntDatum(0)), + d: types.NewUintDatum(124), + unsigned: true, }, { - key: encode(tblID, types.NewUintDatum(0), types.NewIntDatum(0)), - d: types.NewUintDatum(1), + key: encode(tblID, types.NewUintDatum(0), types.NewIntDatum(0)), + d: types.NewUintDatum(1), + unsigned: true, }, { key: []byte{}, d: types.NewIntDatum(math.MinInt64), }, { - key: []byte{}, - d: types.NewUintDatum(0), + key: []byte{}, + d: types.NewUintDatum(0), + unsigned: true, }, { key: tablecodec.GenTableRecordPrefix(tblID), d: types.NewIntDatum(math.MinInt64), }, { - key: tablecodec.GenTableRecordPrefix(tblID), - d: types.NewUintDatum(0), + key: tablecodec.GenTableRecordPrefix(tblID), + d: types.NewUintDatum(0), + unsigned: true, }, { // 3 is encoded intFlag @@ -1076,8 +1088,9 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { }, { // 3 is encoded intFlag - key: append(tablecodec.GenTableRecordPrefix(tblID), []byte{3}...), - d: types.NewUintDatum(0), + key: append(tablecodec.GenTableRecordPrefix(tblID), []byte{3}...), + d: types.NewUintDatum(0), + unsigned: true, }, { // 4 is encoded uintFlag @@ -1086,8 +1099,9 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { }, { // 4 is encoded uintFlag - key: append(tablecodec.GenTableRecordPrefix(tblID), []byte{4}...), - d: types.NewUintDatum(0), + key: append(tablecodec.GenTableRecordPrefix(tblID), []byte{4}...), + d: types.NewUintDatum(0), + unsigned: true, }, { // 5 @@ -1103,7 +1117,15 @@ func TestGetNextIntDatumFromCommonHandle(t *testing.T) { } for _, c := range cases { - d := cache.GetNextIntDatumFromCommonHandle(c.key, tablecodec.GenTableRecordPrefix(tblID), c.d.Kind() == types.KindUint64 || c.unsigned) + if !c.d.IsNull() { + if c.unsigned { + require.Equal(t, types.KindUint64, c.d.Kind()) + } else { + require.Equal(t, types.KindInt64, c.d.Kind()) + } + } + + d := cache.GetNextIntDatumFromCommonHandle(c.key, tablecodec.GenTableRecordPrefix(tblID), c.unsigned) require.Equal(t, c.d, d) } } diff --git a/pkg/ttl/cache/table.go b/pkg/ttl/cache/table.go index a0fdaf87c87c2..4e726158c443b 100644 --- a/pkg/ttl/cache/table.go +++ b/pkg/ttl/cache/table.go @@ -34,6 +34,7 @@ import ( "github.com/pingcap/tidb/pkg/types" "github.com/pingcap/tidb/pkg/util/chunk" "github.com/pingcap/tidb/pkg/util/codec" + "github.com/pingcap/tidb/pkg/util/collate" "github.com/pingcap/tidb/pkg/util/intest" "github.com/tikv/client-go/v2/tikv" ) @@ -314,16 +315,6 @@ func (t *PhysicalTable) splitCommonHandleRanges( return []ScanRange{newFullRange()}, nil } - greaterOrEqual := func(a, b types.Datum) bool { - if isInt { - if unsigned { - return a.GetUint64() >= b.GetUint64() - } - return a.GetInt64() >= b.GetInt64() - } - return kv.Key(a.GetBytes()).Cmp(b.GetBytes()) >= 0 - } - scanRanges := make([]ScanRange, 0, len(keyRanges)) curScanStart := nullDatum() for i, keyRange := range keyRanges { @@ -340,8 +331,16 @@ func (t *PhysicalTable) splitCommonHandleRanges( } } - if !curScanStart.IsNull() && !curScanEnd.IsNull() && greaterOrEqual(curScanStart, curScanEnd) { - continue + if !curScanStart.IsNull() && !curScanEnd.IsNull() { + cmp, err := curScanStart.Compare(types.StrictContext, &curScanEnd, collate.GetBinaryCollator()) + intest.AssertNoError(err) + if err != nil { + return nil, err + } + + if cmp >= 0 { + continue + } } scanRanges = append(scanRanges, newDatumRange(curScanStart, curScanEnd))