Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#49808
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
qw4990 authored and ti-chi-bot committed Dec 27, 2023
1 parent 20c3e7d commit 27a035e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/statistics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ go_test(
data = glob(["testdata/**"]),
embed = [":statistics"],
flaky = True,
<<<<<<< HEAD
shard_count = 33,
=======
shard_count = 35,
>>>>>>> 1fb5a9ae14a (planner: a better way to round scale factor when collecting TopN stats (#49808))
deps = [
"//pkg/config",
"//pkg/parser/ast",
Expand Down
4 changes: 4 additions & 0 deletions pkg/statistics/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func BuildHistAndTopN(
if err != nil {
return nil, nil, errors.Trace(err)
}
<<<<<<< HEAD
// For debugging invalid sample data.
var (
foundTwice bool
Expand Down Expand Up @@ -418,12 +419,15 @@ func BuildHistAndTopN(
continue
}
}
=======
>>>>>>> 1fb5a9ae14a (planner: a better way to round scale factor when collecting TopN stats (#49808))
}

for i := 0; i < len(topNList); i++ {
topNList[i].Count *= uint64(sampleFactor)
}
topn := &TopN{TopN: topNList}
topn.Scale(sampleFactor)

if uint64(count) <= topn.TotalCount() || int(hg.NDV) <= len(topn.TopN) {
// TopN includes all sample data
Expand Down
7 changes: 7 additions & 0 deletions pkg/statistics/cmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ type TopN struct {
TopN []TopNMeta
}

// Scale scales the TopN by the given factor.
func (c *TopN) Scale(scaleFactor float64) {
for i := range c.TopN {
c.TopN[i].Count = uint64(float64(c.TopN[i].Count) * scaleFactor)
}
}

// AppendTopN appends a topn into the TopN struct.
func (c *TopN) AppendTopN(data []byte, count uint64) {
if c == nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/statistics/cmsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,23 @@ func TestSortTopnMeta(t *testing.T) {
SortTopnMeta(data)
require.Equal(t, uint64(2), data[0].Count)
}

func TestTopNScale(t *testing.T) {
for _, scaleFactor := range []float64{0.9999, 1.00001, 1.9999, 4.9999, 5.001, 9.99} {
var data []TopNMeta
sumCount := uint64(0)
for i := 0; i < 20; i++ {
cnt := uint64(rand.Intn(100000))
data = append(data, TopNMeta{
Count: cnt,
})
sumCount += cnt
}
topN := TopN{TopN: data}
topN.Scale(scaleFactor)
scaleCount := float64(sumCount) * scaleFactor
delta := math.Abs(float64(topN.TotalCount()) - scaleCount)
roundErrorRatio := delta / scaleCount
require.Less(t, roundErrorRatio, 0.0001)
}
}

0 comments on commit 27a035e

Please sign in to comment.