diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index 1ff5868803..4662618717 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -463,17 +463,15 @@ func DecodeCMSketchAndTopN(data []byte, topNRows []chunk.Row) (*CMSketch, *TopN, } // DecodeTopN decodes a TopN from the given byte slice. -func DecodeTopN(topNRows []chunk.Row) (*TopN, error) { - pbTopN := make([]*tipb.CMSketchTopN, 0, len(topNRows)) +func DecodeTopN(topNRows []chunk.Row) *TopN { + 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)) } - return TopNFromProto(pbTopN), nil + topN.Sort() + return topN } // DecodeCMSketch encodes the given CMSketch to byte slice. diff --git a/statistics/handle/storage/read.go b/statistics/handle/storage/read.go index f84fc1d1e9..eb8605a7b4 100644 --- a/statistics/handle/storage/read.go +++ b/statistics/handle/storage/read.go @@ -134,7 +134,7 @@ func TopNFromStorage(sctx sessionctx.Context, tblID int64, isIndex int, histID i if err != nil || len(rows) == 0 { return nil, err } - return statistics.DecodeTopN(rows) + return statistics.DecodeTopN(rows), nil } // FMSketchFromStorage reads FMSketch from storage