Skip to content

Commit

Permalink
*: replace compareDatum by compare (#30048)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Nov 23, 2021
1 parent 0f86a53 commit fb5eb1f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -381,15 +381,15 @@ 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
}
if i == 0 {
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
}
Expand Down
5 changes: 3 additions & 2 deletions statistics/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions util/collate/collate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fb5eb1f

Please sign in to comment.