From af3da8ed5a0fb57ef302058139c0653a2ec4ee27 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 19 Oct 2023 17:12:10 +0800 Subject: [PATCH 1/4] fix first --- pkg/executor/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/executor/builder.go b/pkg/executor/builder.go index b20006c5f5267..2b71db282b772 100644 --- a/pkg/executor/builder.go +++ b/pkg/executor/builder.go @@ -4678,7 +4678,7 @@ func buildKvRangesForIndexJoin(ctx sessionctx.Context, tableID, indexID int64, l memTracker.Consume(int64(2 * cap(kvRanges[0].StartKey) * len(kvRanges))) } if len(tmpDatumRanges) != 0 && memTracker != nil { - memTracker.Consume(2 * int64(len(tmpDatumRanges)) * types.EstimatedMemUsage(tmpDatumRanges[0].LowVal, len(tmpDatumRanges))) + memTracker.Consume(2 * types.EstimatedMemUsage(tmpDatumRanges[0].LowVal, len(tmpDatumRanges))) } if cwc == nil { slices.SortFunc(kvRanges, func(i, j kv.KeyRange) int { From 456ece23b5a928a17a772ca842b659f68f379e1c Mon Sep 17 00:00:00 2001 From: SeaRise Date: Fri, 20 Oct 2023 12:11:04 +0800 Subject: [PATCH 2/4] update --- pkg/executor/executor_pkg_test.go | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pkg/executor/executor_pkg_test.go b/pkg/executor/executor_pkg_test.go index 7bdf60a0310eb..dff66c846c2db 100644 --- a/pkg/executor/executor_pkg_test.go +++ b/pkg/executor/executor_pkg_test.go @@ -75,6 +75,62 @@ func TestBuildKvRangesForIndexJoinWithoutCwc(t *testing.T) { } } +func TestBuildKvRangesForIndexJoinWithoutCwcAndWithMemoryTracker(t *testing.T) { + indexRanges := make([]*ranger.Range, 0, 6) + indexRanges = append(indexRanges, generateIndexRange(1, 1, 1, 1, 1)) + indexRanges = append(indexRanges, generateIndexRange(1, 1, 2, 1, 1)) + indexRanges = append(indexRanges, generateIndexRange(1, 1, 2, 1, 2)) + indexRanges = append(indexRanges, generateIndexRange(1, 1, 3, 1, 1)) + indexRanges = append(indexRanges, generateIndexRange(2, 1, 1, 1, 1)) + indexRanges = append(indexRanges, generateIndexRange(2, 1, 2, 1, 1)) + + bytesConsumed1 := int64(0) + { + joinKeyRows := make([]*indexJoinLookUpContent, 0, 5) + for i := int64(0); i < 10; i++ { + joinKeyRows = append(joinKeyRows, &indexJoinLookUpContent{keys: generateDatumSlice(1, i)}) + } + + keyOff2IdxOff := []int{1, 3} + ctx := mock.NewContext() + memTracker := memory.NewTracker(memory.LabelForIndexWorker, -1) + kvRanges, err := buildKvRangesForIndexJoin(ctx, 0, 0, joinKeyRows, indexRanges, keyOff2IdxOff, nil, memTracker, nil) + require.NoError(t, err) + // Check the kvRanges is in order. + for i, kvRange := range kvRanges { + require.True(t, kvRange.StartKey.Cmp(kvRange.EndKey) < 0) + if i > 0 { + require.True(t, kvRange.StartKey.Cmp(kvRanges[i-1].EndKey) >= 0) + } + } + bytesConsumed1 = memTracker.BytesConsumed() + } + + bytesConsumed2 := int64(0) + { + joinKeyRows := make([]*indexJoinLookUpContent, 0, 5) + for i := int64(0); i < 20; i++ { + joinKeyRows = append(joinKeyRows, &indexJoinLookUpContent{keys: generateDatumSlice(1, i)}) + } + + keyOff2IdxOff := []int{1, 3} + ctx := mock.NewContext() + memTracker := memory.NewTracker(memory.LabelForIndexWorker, -1) + kvRanges, err := buildKvRangesForIndexJoin(ctx, 0, 0, joinKeyRows, indexRanges, keyOff2IdxOff, nil, memTracker, nil) + require.NoError(t, err) + // Check the kvRanges is in order. + for i, kvRange := range kvRanges { + require.True(t, kvRange.StartKey.Cmp(kvRange.EndKey) < 0) + if i > 0 { + require.True(t, kvRange.StartKey.Cmp(kvRanges[i-1].EndKey) >= 0) + } + } + bytesConsumed2 = memTracker.BytesConsumed() + } + + require.Equal(t, 2*bytesConsumed1, bytesConsumed2) +} + func generateIndexRange(vals ...int64) *ranger.Range { lowDatums := generateDatumSlice(vals...) highDatums := make([]types.Datum, len(vals)) From 61c5ffa85aa8bd4044a4fb59f1d6bbcdfa4d26e2 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Fri, 20 Oct 2023 12:14:06 +0800 Subject: [PATCH 3/4] updoate --- pkg/executor/executor_pkg_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/executor/executor_pkg_test.go b/pkg/executor/executor_pkg_test.go index dff66c846c2db..a08596a378b47 100644 --- a/pkg/executor/executor_pkg_test.go +++ b/pkg/executor/executor_pkg_test.go @@ -86,7 +86,7 @@ func TestBuildKvRangesForIndexJoinWithoutCwcAndWithMemoryTracker(t *testing.T) { bytesConsumed1 := int64(0) { - joinKeyRows := make([]*indexJoinLookUpContent, 0, 5) + joinKeyRows := make([]*indexJoinLookUpContent, 0, 10) for i := int64(0); i < 10; i++ { joinKeyRows = append(joinKeyRows, &indexJoinLookUpContent{keys: generateDatumSlice(1, i)}) } @@ -108,7 +108,7 @@ func TestBuildKvRangesForIndexJoinWithoutCwcAndWithMemoryTracker(t *testing.T) { bytesConsumed2 := int64(0) { - joinKeyRows := make([]*indexJoinLookUpContent, 0, 5) + joinKeyRows := make([]*indexJoinLookUpContent, 0, 20) for i := int64(0); i < 20; i++ { joinKeyRows = append(joinKeyRows, &indexJoinLookUpContent{keys: generateDatumSlice(1, i)}) } From 969c31d6141e4cdd07ddab963206a3f87197cb55 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 23 Oct 2023 14:29:09 +0800 Subject: [PATCH 4/4] add ut --- pkg/executor/executor_pkg_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/executor/executor_pkg_test.go b/pkg/executor/executor_pkg_test.go index a08596a378b47..45a98caba9258 100644 --- a/pkg/executor/executor_pkg_test.go +++ b/pkg/executor/executor_pkg_test.go @@ -129,6 +129,7 @@ func TestBuildKvRangesForIndexJoinWithoutCwcAndWithMemoryTracker(t *testing.T) { } require.Equal(t, 2*bytesConsumed1, bytesConsumed2) + require.Equal(t, int64(20760), bytesConsumed1) } func generateIndexRange(vals ...int64) *ranger.Range {