@@ -486,11 +486,22 @@ func funcDoubleExponentialSmoothing(vals []parser.Value, args parser.Expressions
486486 return append (enh .Out , Sample {F : s1 }), nil
487487}
488488
489+ // filterFloats filters out histogram samples from the vector in-place.
490+ func filterFloats (v Vector ) Vector {
491+ floats := v [:0 ]
492+ for _ , s := range v {
493+ if s .H == nil {
494+ floats = append (floats , s )
495+ }
496+ }
497+ return floats
498+ }
499+
489500// === sort(node parser.ValueTypeVector) (Vector, Annotations) ===
490501func funcSort (vals []parser.Value , args parser.Expressions , enh * EvalNodeHelper ) (Vector , annotations.Annotations ) {
491502 // NaN should sort to the bottom, so take descending sort with NaN first and
492503 // reverse it.
493- byValueSorter := vectorByReverseValueHeap (vals [0 ].(Vector ))
504+ byValueSorter := vectorByReverseValueHeap (filterFloats ( vals [0 ].(Vector ) ))
494505 sort .Sort (sort .Reverse (byValueSorter ))
495506 return Vector (byValueSorter ), nil
496507}
@@ -499,7 +510,7 @@ func funcSort(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper)
499510func funcSortDesc (vals []parser.Value , args parser.Expressions , enh * EvalNodeHelper ) (Vector , annotations.Annotations ) {
500511 // NaN should sort to the bottom, so take ascending sort with NaN first and
501512 // reverse it.
502- byValueSorter := vectorByValueHeap (vals [0 ].(Vector ))
513+ byValueSorter := vectorByValueHeap (filterFloats ( vals [0 ].(Vector ) ))
503514 sort .Sort (sort .Reverse (byValueSorter ))
504515 return Vector (byValueSorter ), nil
505516}
@@ -631,11 +642,27 @@ func funcRound(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper
631642
632643// === Scalar(node parser.ValueTypeVector) Scalar ===
633644func funcScalar (vals []parser.Value , args parser.Expressions , enh * EvalNodeHelper ) (Vector , annotations.Annotations ) {
634- v := vals [0 ].(Vector )
635- if len (v ) != 1 {
645+ var (
646+ v = vals [0 ].(Vector )
647+ value float64
648+ found bool
649+ )
650+
651+ for _ , s := range v {
652+ if s .H == nil {
653+ if found {
654+ // More than one float found, return NaN.
655+ return append (enh .Out , Sample {F : math .NaN ()}), nil
656+ }
657+ found = true
658+ value = s .F
659+ }
660+ }
661+ // Return the single float if found, otherwise return NaN.
662+ if ! found {
636663 return append (enh .Out , Sample {F : math .NaN ()}), nil
637664 }
638- return append (enh .Out , Sample {F : v [ 0 ]. F }), nil
665+ return append (enh .Out , Sample {F : value }), nil
639666}
640667
641668func aggrOverTime (vals []parser.Value , enh * EvalNodeHelper , aggrFn func (Series ) float64 ) Vector {
@@ -1896,16 +1923,7 @@ func (s vectorByValueHeap) Len() int {
18961923}
18971924
18981925func (s vectorByValueHeap ) Less (i , j int ) bool {
1899- // We compare histograms based on their sum of observations.
1900- // TODO(beorn7): Is that what we want?
19011926 vi , vj := s [i ].F , s [j ].F
1902- if s [i ].H != nil {
1903- vi = s [i ].H .Sum
1904- }
1905- if s [j ].H != nil {
1906- vj = s [j ].H .Sum
1907- }
1908-
19091927 if math .IsNaN (vi ) {
19101928 return true
19111929 }
@@ -1935,16 +1953,7 @@ func (s vectorByReverseValueHeap) Len() int {
19351953}
19361954
19371955func (s vectorByReverseValueHeap ) Less (i , j int ) bool {
1938- // We compare histograms based on their sum of observations.
1939- // TODO(beorn7): Is that what we want?
19401956 vi , vj := s [i ].F , s [j ].F
1941- if s [i ].H != nil {
1942- vi = s [i ].H .Sum
1943- }
1944- if s [j ].H != nil {
1945- vj = s [j ].H .Sum
1946- }
1947-
19481957 if math .IsNaN (vi ) {
19491958 return true
19501959 }
0 commit comments