diff --git a/statistics/feedback.go b/statistics/feedback.go index c6806a9c99189..88824afae0e08 100644 --- a/statistics/feedback.go +++ b/statistics/feedback.go @@ -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" @@ -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 } @@ -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 } @@ -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 } @@ -457,7 +458,7 @@ 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 @@ -465,7 +466,7 @@ func buildBucketFeedback(h *Histogram, feedback *QueryFeedback) (map[int]*Bucket 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 @@ -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 diff --git a/statistics/histogram.go b/statistics/histogram.go index 0fdbfe7fd68f4..a61f1d1405f59 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -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) } @@ -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) } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 }