@@ -751,6 +751,36 @@ func TestProtobufCodecFuzz(t *testing.T) {
751751 runQueryFuzzTestCases (t , ps , c1 , c2 , now , start , end , scrapeInterval , 1000 )
752752}
753753
754+ var sampleNumComparer = cmp .Comparer (func (x , y model.Value ) bool {
755+ if x .Type () != y .Type () {
756+ return false
757+ }
758+
759+ vx , xvec := x .(model.Vector )
760+ vy , yvec := y .(model.Vector )
761+
762+ if xvec && yvec {
763+ return len (vx ) == len (vy )
764+ }
765+
766+ mx , xmat := x .(model.Matrix )
767+ my , ymat := y .(model.Matrix )
768+
769+ mxSamples := 0
770+ mySamples := 0
771+
772+ if xmat && ymat {
773+ for i := 0 ; i < len (mx ); i ++ {
774+ mxSamples += len (mx [i ].Values )
775+ }
776+ for i := 0 ; i < len (my ); i ++ {
777+ mySamples += len (my [i ].Values )
778+ }
779+ }
780+
781+ return mxSamples == mySamples
782+ })
783+
754784// comparer should be used to compare promql results between engines.
755785var comparer = cmp .Comparer (func (x , y model.Value ) bool {
756786 if x .Type () != y .Type () {
@@ -1471,6 +1501,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14711501 break
14721502 }
14731503 }
1504+
14741505 res1 , err1 := c1 .Query (query , queryTime )
14751506 res2 , err2 := c2 .Query (query , queryTime )
14761507 cases = append (cases , & testCase {
@@ -1491,6 +1522,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14911522 break
14921523 }
14931524 }
1525+
14941526 res1 , err1 := c1 .QueryRange (query , start , end , step )
14951527 res2 , err2 := c2 .QueryRange (query , start , end , step )
14961528 cases = append (cases , & testCase {
@@ -1514,6 +1546,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
15141546 t .Logf ("case %d error mismatch.\n %s: %s\n err1: %v\n err2: %v\n " , i , qt , tc .query , tc .err1 , tc .err2 )
15151547 failures ++
15161548 }
1549+ } else if shouldUseSampleNumComparer (tc .query ) {
1550+ if ! cmp .Equal (tc .res1 , tc .res2 , sampleNumComparer ) {
1551+ t .Logf ("case %d # of samples mismatch.\n %s: %s\n res1: %s\n res2: %s\n " , i , qt , tc .query , tc .res1 .String (), tc .res2 .String ())
1552+ failures ++
1553+ }
15171554 } else if ! cmp .Equal (tc .res1 , tc .res2 , comparer ) {
15181555 t .Logf ("case %d results mismatch.\n %s: %s\n res1: %s\n res2: %s\n " , i , qt , tc .query , tc .res1 .String (), tc .res2 .String ())
15191556 failures ++
@@ -1524,6 +1561,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
15241561 }
15251562}
15261563
1564+ func shouldUseSampleNumComparer (query string ) bool {
1565+ if strings .Contains (query , "bottomk" ) || strings .Contains (query , "topk" ) {
1566+ return true
1567+ }
1568+ return false
1569+ }
1570+
15271571func isValidQuery (generatedQuery parser.Expr , maxDepth int ) bool {
15281572 isValid := true
15291573 currentDepth := 0
0 commit comments