Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: replace compareDatum by compare #30052

Merged
merged 7 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions statistics/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/ranger"
"go.uber.org/atomic"
Expand Down Expand Up @@ -330,14 +331,14 @@ func NonOverlappedFeedbacks(sc *stmtctx.StatementContext, fbs []Feedback) ([]Fee
// with the previous chosen feedbacks.
var existsErr bool
sort.Slice(fbs, func(i, j int) bool {
res, err := fbs[i].Upper.CompareDatum(sc, fbs[j].Upper)
res, err := fbs[i].Upper.Compare(sc, fbs[j].Upper, collate.GetBinaryCollator())
if err != nil {
existsErr = true
}
if existsErr || res != 0 {
return res < 0
}
res, err = fbs[i].Lower.CompareDatum(sc, fbs[j].Lower)
res, err = fbs[i].Lower.Compare(sc, fbs[j].Lower, collate.GetBinaryCollator())
if err != nil {
existsErr = true
}
Expand All @@ -349,7 +350,7 @@ func NonOverlappedFeedbacks(sc *stmtctx.StatementContext, fbs []Feedback) ([]Fee
resFBs := make([]Feedback, 0, len(fbs))
previousEnd := &types.Datum{}
for _, fb := range fbs {
res, err := previousEnd.CompareDatum(sc, fb.Lower)
res, err := previousEnd.Compare(sc, fb.Lower, collate.GetBinaryCollator())
if err != nil {
return fbs, false
}
Expand All @@ -370,14 +371,14 @@ type BucketFeedback struct {

// outOfRange checks if the `val` is between `min` and `max`.
func outOfRange(sc *stmtctx.StatementContext, min, max, val *types.Datum) (int, error) {
result, err := val.CompareDatum(sc, min)
result, err := val.Compare(sc, min, collate.GetBinaryCollator())
if err != nil {
return 0, err
}
if result < 0 {
return result, nil
}
result, err = val.CompareDatum(sc, max)
result, err = val.Compare(sc, max, collate.GetBinaryCollator())
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -457,15 +458,15 @@ func buildBucketFeedback(h *Histogram, feedback *QueryFeedback) (map[int]*Bucket
}
bkt.feedback = append(bkt.feedback, fb)
// Update the bound if necessary.
res, err := bkt.lower.CompareDatum(nil, fb.Lower)
res, err := bkt.lower.Compare(nil, fb.Lower, collate.GetBinaryCollator())
if err != nil {
logutil.BgLogger().Debug("compare datum failed", zap.Any("value1", bkt.lower), zap.Any("value2", fb.Lower), zap.Error(err))
continue
}
if res > 0 {
bkt.lower = fb.Lower
}
res, err = bkt.upper.CompareDatum(nil, fb.Upper)
res, err = bkt.upper.Compare(nil, fb.Upper, collate.GetBinaryCollator())
if err != nil {
logutil.BgLogger().Debug("compare datum failed", zap.Any("value1", bkt.upper), zap.Any("value2", fb.Upper), zap.Error(err))
continue
Expand Down Expand Up @@ -501,7 +502,7 @@ func (b *BucketFeedback) getBoundaries(num int) []types.Datum {
total = 1
// Erase the repeat values.
for i := 1; i < len(vals); i++ {
cmp, err := vals[total-1].CompareDatum(nil, &vals[i])
cmp, err := vals[total-1].Compare(nil, &vals[i], collate.GetBinaryCollator())
if err != nil {
logutil.BgLogger().Debug("compare datum failed", zap.Any("value1", vals[total-1]), zap.Any("value2", vals[i]), zap.Error(err))
continue
Expand Down
24 changes: 12 additions & 12 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func MergeHistograms(sc *stmtctx.StatementContext, lh *Histogram, rh *Histogram,
}
lh.NDV += rh.NDV
lLen := lh.Len()
cmp, err := lh.GetUpper(lLen-1).CompareDatum(sc, rh.GetLower(0))
cmp, err := lh.GetUpper(lLen-1).Compare(sc, rh.GetLower(0), collate.GetBinaryCollator())
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -1134,7 +1134,7 @@ func (c *Column) GetColumnRowCount(sc *stmtctx.StatementContext, ranges []*range
if lowVal.Kind() == types.KindString {
lowVal.SetBytes(collate.GetCollator(lowVal.Collation()).Key(lowVal.GetString()))
}
cmp, err := lowVal.CompareDatum(sc, &highVal)
cmp, err := lowVal.Compare(sc, &highVal, collate.GetBinaryCollator())
if err != nil {
return 0, errors.Trace(err)
}
Expand Down Expand Up @@ -1797,7 +1797,7 @@ func mergeBucketNDV(sc *stmtctx.StatementContext, left *bucket4Merging, right *b
res.NDV = left.NDV
return &res, nil
}
upperCompare, err := right.upper.CompareDatum(sc, left.upper)
upperCompare, err := right.upper.Compare(sc, left.upper, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand All @@ -1811,7 +1811,7 @@ func mergeBucketNDV(sc *stmtctx.StatementContext, left *bucket4Merging, right *b
// ___left__|
// They have the same upper.
if upperCompare == 0 {
lowerCompare, err := right.lower.CompareDatum(sc, left.lower)
lowerCompare, err := right.lower.Compare(sc, left.lower, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1842,7 +1842,7 @@ func mergeBucketNDV(sc *stmtctx.StatementContext, left *bucket4Merging, right *b
// ____right___|
// ____left__|
// right.upper > left.upper
lowerCompareUpper, err := right.lower.CompareDatum(sc, left.upper)
lowerCompareUpper, err := right.lower.Compare(sc, left.upper, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand All @@ -1859,7 +1859,7 @@ func mergeBucketNDV(sc *stmtctx.StatementContext, left *bucket4Merging, right *b
return &res, nil
}
upperRatio := calcFraction4Datums(right.lower, right.upper, left.upper)
lowerCompare, err := right.lower.CompareDatum(sc, left.lower)
lowerCompare, err := right.lower.Compare(sc, left.lower, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1911,7 +1911,7 @@ func mergePartitionBuckets(sc *stmtctx.StatementContext, buckets []*bucket4Mergi
for i := len(buckets) - 1; i >= 0; i-- {
totNDV += buckets[i].NDV
res.Count += buckets[i].Count
compare, err := buckets[i].upper.CompareDatum(sc, res.upper)
compare, err := buckets[i].upper.Compare(sc, res.upper, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1966,7 +1966,7 @@ func MergePartitionHist2GlobalHist(sc *stmtctx.StatementContext, hists []*Histog
minValue = hist.GetLower(0).Clone()
continue
}
res, err := hist.GetLower(0).CompareDatum(sc, minValue)
res, err := hist.GetLower(0).Compare(sc, minValue, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2006,7 +2006,7 @@ func MergePartitionHist2GlobalHist(sc *stmtctx.StatementContext, hists []*Histog
minValue = d.Clone()
continue
}
res, err := d.CompareDatum(sc, minValue)
res, err := d.Compare(sc, minValue, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand All @@ -2028,14 +2028,14 @@ func MergePartitionHist2GlobalHist(sc *stmtctx.StatementContext, hists []*Histog

var sortError error
sort.Slice(buckets, func(i, j int) bool {
res, err := buckets[i].upper.CompareDatum(sc, buckets[j].upper)
res, err := buckets[i].upper.Compare(sc, buckets[j].upper, collate.GetBinaryCollator())
if err != nil {
sortError = err
}
if res != 0 {
return res < 0
}
res, err = buckets[i].lower.CompareDatum(sc, buckets[j].lower)
res, err = buckets[i].lower.Compare(sc, buckets[j].lower, collate.GetBinaryCollator())
if err != nil {
sortError = err
}
Expand All @@ -2055,7 +2055,7 @@ func MergePartitionHist2GlobalHist(sc *stmtctx.StatementContext, hists []*Histog
bucketNDV += buckets[i].NDV
if sum >= totCount*bucketCount/expBucketNumber && sum-prevSum >= gBucketCountThreshold {
for ; i > 0; i-- { // if the buckets have the same upper, we merge them into the same new buckets.
res, err := buckets[i-1].upper.CompareDatum(sc, buckets[i].upper)
res, err := buckets[i-1].upper.Compare(sc, buckets[i].upper, collate.GetBinaryCollator())
if err != nil {
return nil, err
}
Expand Down