From 2d5b0e486d6cece1085be886bf25054a34297732 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 17 Oct 2023 18:27:30 +0800 Subject: [PATCH] This is an automated cherry-pick of #47598 Signed-off-by: ti-chi-bot --- statistics/cmsketch.go | 55 +++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index 9406d9eb7a5b2..a6ae5d6af503f 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -431,28 +431,59 @@ func DecodeCMSketchAndTopN(data []byte, topNRows []chunk.Row) (*CMSketch, *TopN, if data == nil && len(topNRows) == 0 { return nil, nil, nil } - pbTopN := make([]*tipb.CMSketchTopN, 0, len(topNRows)) + if len(data) == 0 { + return nil, DecodeTopN(topNRows), nil + } + cm, err := DecodeCMSketch(data) + if err != nil { + return nil, nil, errors.Trace(err) + } + return cm, DecodeTopN(topNRows), nil +} + +<<<<<<< HEAD:statistics/cmsketch.go +======= +// DecodeTopN decodes a TopN from the given byte slice. +func DecodeTopN(topNRows []chunk.Row) *TopN { + if len(topNRows) == 0 { + return nil + } + topN := NewTopN(len(topNRows)) for _, row := range topNRows { data := make([]byte, len(row.GetBytes(0))) copy(data, row.GetBytes(0)) - pbTopN = append(pbTopN, &tipb.CMSketchTopN{ - Data: data, - Count: row.GetUint64(1), - }) + topN.AppendTopN(data, row.GetUint64(1)) } + topN.Sort() + return topN +} + +// DecodeCMSketch encodes the given CMSketch to byte slice. +func DecodeCMSketch(data []byte) (*CMSketch, error) { if len(data) == 0 { - return nil, TopNFromProto(pbTopN), nil + return nil, nil } - p := &tipb.CMSketch{} - err := p.Unmarshal(data) + protoSketch := &tipb.CMSketch{} + err := protoSketch.Unmarshal(data) if err != nil { - return nil, nil, errors.Trace(err) + return nil, errors.Trace(err) + } + if len(protoSketch.Rows) == 0 { + return nil, nil + } + c := NewCMSketch(int32(len(protoSketch.Rows)), int32(len(protoSketch.Rows[0].Counters))) + for i, row := range protoSketch.Rows { + c.count = 0 + for j, counter := range row.Counters { + c.table[i][j] = counter + c.count = c.count + uint64(counter) + } } - p.TopN = pbTopN - cm, topN := CMSketchAndTopNFromProto(p) - return cm, topN, nil + c.defaultValue = protoSketch.DefaultValue + return c, nil } +>>>>>>> f69f37a9fb5 (statistics: avoid unnecessary copy at CMSketchAndTopNFromProto (#47598)):pkg/statistics/cmsketch.go // TotalCount returns the total count in the sketch, it is only used for test. func (c *CMSketch) TotalCount() uint64 { if c == nil {