Skip to content

Commit b4257b0

Browse files
authored
Fix TestNativeHistogramFuzz to cover queries that contain count_values (#6751)
Signed-off-by: SungJin1212 <[email protected]>
1 parent 4d6715f commit b4257b0

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

integration/query_fuzz_test.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13+
"regexp"
1314
"sort"
1415
"strconv"
1516
"strings"
@@ -970,15 +971,34 @@ var comparer = cmp.Comparer(func(x, y model.Value) bool {
970971
return l == r || (compareFloats(float64(l.Count), float64(r.Count)) && compareFloats(float64(l.Sum), float64(r.Sum)) && compareHistogramBuckets(l.Buckets, r.Buckets))
971972
}
972973

974+
fetchValuesFromNH := func(nhString string) []float64 {
975+
// Regex to match float numbers
976+
re := regexp.MustCompile(`-?\d+(\.\d+)?`)
977+
978+
matches := re.FindAllString(nhString, -1)
979+
980+
var ret []float64
981+
for _, match := range matches {
982+
f, err := strconv.ParseFloat(match, 64)
983+
if err != nil {
984+
continue
985+
}
986+
ret = append(ret, f)
987+
}
988+
return ret
989+
}
990+
973991
// count_values returns a metrics with one label {"value": "1.012321"}
992+
// or {"value": "{count:114, sum:226.93333333333334, [-4,-2.82842712474619):12.333333333333332, [-2.82842712474619,-2):12.333333333333332, [-1.414213562373095,-1):13.333333333333334, [-1,-0.7071067811865475):12.333333333333332, [-0.001,0.001]:13.333333333333334, (0.7071067811865475,1]:12.333333333333332, (1,1.414213562373095]:13.333333333333334, (2,2.82842712474619]:12.333333333333332, (2.82842712474619,4]:12.333333333333332}"}}
974993
compareValueMetrics := func(l, r model.Metric) (valueMetric bool, equals bool) {
975994
lLabels := model.LabelSet(l).Clone()
976995
rLabels := model.LabelSet(r).Clone()
977996
var (
978-
lVal, rVal model.LabelValue
979-
lFloat, rFloat float64
980-
ok bool
981-
err error
997+
lVal, rVal model.LabelValue
998+
lFloats, rFloats []float64 // when NH, these contain float64 values in NH
999+
lFloat, rFloat float64
1000+
ok bool
1001+
err error
9821002
)
9831003

9841004
if lVal, ok = lLabels["value"]; !ok {
@@ -989,6 +1009,24 @@ var comparer = cmp.Comparer(func(x, y model.Value) bool {
9891009
return false, false
9901010
}
9911011

1012+
if strings.Contains(string(lVal), "count") && strings.Contains(string(rVal), "count") {
1013+
// the values are histograms
1014+
lFloats = fetchValuesFromNH(string(lVal))
1015+
rFloats = fetchValuesFromNH(string(rVal))
1016+
1017+
if len(lFloats) != len(rFloats) {
1018+
return true, false
1019+
}
1020+
1021+
for i := 0; i < len(lFloats); i++ {
1022+
if !compareFloats(lFloats[i], rFloats[i]) {
1023+
return true, false
1024+
}
1025+
}
1026+
1027+
return true, true
1028+
}
1029+
9921030
if lFloat, err = strconv.ParseFloat(string(lVal), 64); err != nil {
9931031
return false, false
9941032
}

0 commit comments

Comments
 (0)