github.com/dolthub/go-mysql-server@v0.18.0/sql/stats/helper.go (about)

     1  package stats
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/dolthub/go-mysql-server/sql"
     7  )
     8  
     9  func Empty(s sql.Statistic) bool {
    10  	return s == nil || len(s.Histogram()) == 0
    11  }
    12  
    13  func InterpolateNewCounts(from, to sql.Statistic) sql.Statistic {
    14  	if Empty(from) {
    15  		return to
    16  	} else if Empty(from) {
    17  		return to
    18  	}
    19  	if from.Qualifier().String() == to.Qualifier().String() {
    20  		return to
    21  	}
    22  
    23  	if to.DistinctCount() < from.DistinctCount() {
    24  		// invalid use of interpolate
    25  		return to
    26  	}
    27  
    28  	filterSelectivity := float64(to.DistinctCount()) / float64(from.DistinctCount())
    29  
    30  	newHist := make([]*Bucket, len(from.Histogram()))
    31  	for i, h := range from.Histogram() {
    32  		newMcvs := make([]uint64, len(h.McvCounts()))
    33  		for i, cnt := range h.McvCounts() {
    34  			newMcvs[i] = uint64(math.Max(1, float64(cnt)*filterSelectivity))
    35  		}
    36  		newHist[i] = NewHistogramBucket(
    37  			uint64(math.Max(1, float64(h.RowCount())*filterSelectivity)),
    38  			uint64(math.Max(1, float64(h.DistinctCount())*filterSelectivity)),
    39  			uint64(math.Max(1, float64(h.NullCount())*filterSelectivity)),
    40  			uint64(math.Max(1, float64(h.BoundCount())*filterSelectivity)),
    41  			h.UpperBound(),
    42  			h.McvCounts(),
    43  			h.Mcvs())
    44  	}
    45  	return UpdateCounts(NewStatistic(0, 0, 0, from.AvgSize(), from.CreatedAt(), from.Qualifier(), from.Columns(), from.Types(), newHist, from.IndexClass(), from.LowerBound()))
    46  }