diff --git a/executor/analyze_test.go b/executor/analyze_test.go index fe067c5f46423..f8c574f36d53a 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -371,7 +371,7 @@ func (s *testFastAnalyze) TestAnalyzeFastSample(c *C) { samples := mockExec.Collectors[i].Samples c.Assert(len(samples), Equals, 20) for j := 1; j < 20; j++ { - cmp, err := samples[j].Value.CompareDatum(tk.Se.GetSessionVars().StmtCtx, &samples[j-1].Value) + cmp, err := samples[j].Value.Compare(tk.Se.GetSessionVars().StmtCtx, &samples[j-1].Value, collate.GetBinaryCollator()) c.Assert(err, IsNil) c.Assert(cmp, Greater, 0) } @@ -381,7 +381,7 @@ func (s *testFastAnalyze) TestAnalyzeFastSample(c *C) { func checkHistogram(sc *stmtctx.StatementContext, hg *statistics.Histogram) (bool, error) { for i := 0; i < len(hg.Buckets); i++ { lower, upper := hg.GetLower(i), hg.GetUpper(i) - cmp, err := upper.CompareDatum(sc, lower) + cmp, err := upper.Compare(sc, lower, collate.GetBinaryCollator()) if cmp < 0 || err != nil { return false, err } @@ -389,7 +389,7 @@ func checkHistogram(sc *stmtctx.StatementContext, hg *statistics.Histogram) (boo continue } previousUpper := hg.GetUpper(i - 1) - cmp, err = lower.CompareDatum(sc, previousUpper) + cmp, err = lower.Compare(sc, previousUpper, collate.GetBinaryCollator()) if cmp <= 0 || err != nil { return false, err } diff --git a/statistics/builder.go b/statistics/builder.go index 13edd2457fd75..cff117547f619 100644 --- a/statistics/builder.go +++ b/statistics/builder.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" + "github.com/pingcap/tidb/util/collate" ) // SortedBuilder is used to build histograms for PK and index. @@ -67,7 +68,7 @@ func (b *SortedBuilder) Iterate(data types.Datum) error { b.hist.NDV = 1 return nil } - cmp, err := b.hist.GetUpper(int(b.bucketIdx)).CompareDatum(b.sc, &data) + cmp, err := b.hist.GetUpper(int(b.bucketIdx)).Compare(b.sc, &data, collate.GetBinaryCollator()) if err != nil { return errors.Trace(err) } @@ -158,7 +159,7 @@ func buildHist(sc *stmtctx.StatementContext, hg *Histogram, samples []*SampleIte hg.AppendBucket(&samples[0].Value, &samples[0].Value, int64(sampleFactor), int64(ndvFactor)) for i := int64(1); i < sampleNum; i++ { corrXYSum += float64(i) * float64(samples[i].Ordinal) - cmp, err := hg.GetUpper(bucketIdx).CompareDatum(sc, &samples[i].Value) + cmp, err := hg.GetUpper(bucketIdx).Compare(sc, &samples[i].Value, collate.GetBinaryCollator()) if err != nil { return 0, errors.Trace(err) } diff --git a/util/collate/collate.go b/util/collate/collate.go index 4f73b89b6742d..3cba9d6f572e5 100644 --- a/util/collate/collate.go +++ b/util/collate/collate.go @@ -154,6 +154,11 @@ func GetCollator(collate string) Collator { return binCollatorInstance } +// GetBinaryCollator gets the binary collator, it is often used when we want to apply binary compare. +func GetBinaryCollator() Collator { + return binCollatorInstance +} + // GetCollatorByID get the collator according to id, it will return the binary collator if the corresponding collator doesn't exist. func GetCollatorByID(id int) Collator { if atomic.LoadInt32(&newCollationEnabled) == 1 {