@ -1466,7 +1466,7 @@ func intersection(ls1, ls2 labels.Labels) labels.Labels {
type groupedAggregation struct {
labels labels . Labels
value float64
valuesSquaredSum float64
mean float64
groupCount int
heap vectorByValueHeap
reverseHeap vectorByReverseValueHeap
@ -1542,7 +1542,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
result [ groupingKey ] = & groupedAggregation {
labels : m ,
value : s . V ,
valuesSquaredSum : s . V * s . V ,
mean : s . V ,
groupCount : 1 ,
}
inputVecLen := int64 ( len ( vec ) )
@ -1550,7 +1550,9 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
if k > inputVecLen {
resultSize = inputVecLen
}
if op == itemTopK || op == itemQuantile {
if op == itemStdvar || op == itemStddev {
result [ groupingKey ] . value = 0.0
} else if op == itemTopK || op == itemQuantile {
result [ groupingKey ] . heap = make ( vectorByValueHeap , 0 , resultSize )
heap . Push ( & result [ groupingKey ] . heap , & Sample {
Point : Point { V : s . V } ,
@ -1571,8 +1573,8 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
group . value += s . V
case itemAvg :
group . value += s . V
group . groupCount ++
group . mean += ( s . V - group . mean ) / float64 ( group . groupCount )
case itemMax :
if group . value < s . V || math . IsNaN ( group . value ) {
@ -1588,9 +1590,10 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
group . groupCount ++
case itemStdvar , itemStddev :
group . value += s . V
group . valuesSquaredSum += s . V * s . V
group . groupCount ++
delta := s . V - group . mean
group . mean += delta / float64 ( group . groupCount )
group . value += delta * ( s . V - group . mean )
case itemTopK :
if int64 ( len ( group . heap ) ) < k || group . heap [ 0 ] . V < s . V || math . IsNaN ( group . heap [ 0 ] . V ) {
@ -1626,18 +1629,16 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
for _ , aggr := range result {
switch op {
case itemAvg :
aggr . value = aggr . value / float64 ( aggr . groupCount )
aggr . value = aggr . mean
case itemCount , itemCountValues :
aggr . value = float64 ( aggr . groupCount )
case itemStdvar :
avg := aggr . value / float64 ( aggr . groupCount )
aggr . value = aggr . valuesSquaredSum / float64 ( aggr . groupCount ) - avg * avg
aggr . value = aggr . value / float64 ( aggr . groupCount )
case itemStddev :
avg := aggr . value / float64 ( aggr . groupCount )
aggr . value = math . Sqrt ( aggr . valuesSquaredSum / float64 ( aggr . groupCount ) - avg * avg )
aggr . value = math . Sqrt ( aggr . value / float64 ( aggr . groupCount ) )
case itemTopK :
// The heap keeps the lowest value on top, so reverse it.