Skip to content

Commit

Permalink
statistics: handle topn reference if it is nil (#35328)
Browse files Browse the repository at this point in the history
ref #34052
  • Loading branch information
Yisaer authored Jun 17, 2022
1 parent 255dc8c commit 945d7ef
Showing 1 changed file with 29 additions and 23 deletions.
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

0 comments on commit 945d7ef

Please sign in to comment.