Skip to content

Commit

Permalink
statistics: use slices.SortStableFunc to simple SortSampleItems (#47009)
Browse files Browse the repository at this point in the history
ref #45933
  • Loading branch information
hawkingrei authored Sep 15, 2023
1 parent 533de3b commit beb2a36
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions statistics/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package statistics

import (
"context"
"sort"
"slices"
"time"
"unsafe"

Expand Down Expand Up @@ -65,32 +65,16 @@ func CopySampleItems(items []*SampleItem) []*SampleItem {
func SortSampleItems(sc *stmtctx.StatementContext, items []*SampleItem) ([]*SampleItem, error) {
sortedItems := make([]*SampleItem, len(items))
copy(sortedItems, items)
sorter := sampleItemSorter{items: sortedItems, sc: sc}
sort.Stable(&sorter)
return sortedItems, sorter.err
}

type sampleItemSorter struct {
err error
sc *stmtctx.StatementContext
items []*SampleItem
}

func (s *sampleItemSorter) Len() int {
return len(s.items)
}

func (s *sampleItemSorter) Less(i, j int) bool {
var cmp int
cmp, s.err = s.items[i].Value.Compare(s.sc, &s.items[j].Value, collate.GetBinaryCollator())
if s.err != nil {
return true
}
return cmp < 0
}

func (s *sampleItemSorter) Swap(i, j int) {
s.items[i], s.items[j] = s.items[j], s.items[i]
var err error
slices.SortStableFunc(sortedItems, func(i, j *SampleItem) int {
var cmp int
cmp, err = i.Value.Compare(sc, &j.Value, collate.GetBinaryCollator())
if err != nil {
return -1
}
return cmp
})
return sortedItems, err
}

// SampleCollector will collect Samples and calculate the count and ndv of an attribute.
Expand Down

0 comments on commit beb2a36

Please sign in to comment.