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: handle topn reference if it is nil #35328

Merged
merged 12 commits into from
Jun 17, 2022
52 changes: 29 additions & 23 deletions statistics/cmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,6 @@ func (c *CMSketch) MemoryUsage() (sum int64) {
return
}

// queryAddTopN TopN adds count to CMSketch.topN if exists, and returns the count of such elements after insert.
// If such elements does not in topn elements, nothing will happen and false will be returned.
func (c *TopN) updateTopNWithDelta(d []byte, delta uint64, increase bool) bool {
if c == nil || c.TopN == nil {
return false
}
idx := c.findTopN(d)
if idx >= 0 {
if increase {
c.TopN[idx].Count += delta
} else {
c.TopN[idx].Count -= delta
}
return true
}
return false
}

// InsertBytes inserts the bytes value into the CM Sketch.
func (c *CMSketch) InsertBytes(bytes []byte) {
c.InsertBytesByCount(bytes, 1)
Expand Down Expand Up @@ -480,11 +462,6 @@ func (c *CMSketch) Copy() *CMSketch {
return &CMSketch{count: c.count, width: c.width, depth: c.depth, table: tbl, defaultValue: c.defaultValue}
}

// AppendTopN appends a topn into the TopN struct.
func (c *TopN) AppendTopN(data []byte, count uint64) {
c.TopN = append(c.TopN, TopNMeta{data, count})
}

// GetWidthAndDepth returns the width and depth of CM Sketch.
func (c *CMSketch) GetWidthAndDepth() (int32, int32) {
return c.width, c.depth
Expand All @@ -501,6 +478,14 @@ type TopN struct {
TopN []TopNMeta
}

// AppendTopN appends a topn into the TopN struct.
func (c *TopN) AppendTopN(data []byte, count uint64) {
if c == nil {
return
}
c.TopN = append(c.TopN, TopNMeta{data, count})
}

func (c *TopN) String() string {
if c == nil {
return "EmptyTopN"
Expand Down Expand Up @@ -530,6 +515,9 @@ func (c *TopN) Num() int {

// DecodedString returns the value with decoded result.
func (c *TopN) DecodedString(ctx sessionctx.Context, colTypes []byte) (string, error) {
if c == nil {
return "", nil
}
builder := &strings.Builder{}
fmt.Fprintf(builder, "TopN{length: %v, ", len(c.TopN))
fmt.Fprint(builder, "[")
Expand Down Expand Up @@ -699,6 +687,24 @@ func (c *TopN) MemoryUsage() (sum int64) {
return
}

// queryAddTopN TopN adds count to CMSketch.topN if exists, and returns the count of such elements after insert.
// If such elements does not in topn elements, nothing will happen and false will be returned.
func (c *TopN) updateTopNWithDelta(d []byte, delta uint64, increase bool) bool {
if c == nil || c.TopN == nil {
return false
}
idx := c.findTopN(d)
if idx >= 0 {
if increase {
c.TopN[idx].Count += delta
} else {
c.TopN[idx].Count -= delta
}
return true
}
return false
}

// NewTopN creates the new TopN struct by the given size.
func NewTopN(n int) *TopN {
return &TopN{TopN: make([]TopNMeta, 0, n)}
Expand Down