@@ -2601,6 +2601,128 @@ func TestEdgeCases(t *testing.T) {
26012601 }
26022602}
26032603
2604+ func TestXFunctionsRangeQuery (t * testing.T ) {
2605+ // Negative offset and at modifier are enabled by default
2606+ // since Prometheus v2.33.0, so we also enable them.
2607+ opts := promql.EngineOpts {
2608+ Timeout : 1 * time .Hour ,
2609+ MaxSamples : 1e10 ,
2610+ EnableNegativeOffset : true ,
2611+ EnableAtModifier : true ,
2612+ }
2613+
2614+ cases := []struct {
2615+ name string
2616+ load string
2617+ query string
2618+ startTime time.Time
2619+ endTime time.Time
2620+ step time.Duration
2621+
2622+ expected promql.Matrix
2623+ }{
2624+ {
2625+ name : "gaps between steps" ,
2626+ load : `load 10s
2627+ http_requests 1 5 10 20 _ 40` ,
2628+ query : "xincrease(http_requests[10s])" ,
2629+
2630+ startTime : time .Unix (0 , 0 ),
2631+ endTime : time .Unix (60 , 0 ),
2632+ step : 20 * time .Second ,
2633+
2634+ expected : promql.Matrix {
2635+ promql.Series {
2636+ Metric : labels .New (),
2637+ Floats : []promql.FPoint {
2638+ {T : 00_000 , F : 1 },
2639+ {T : 20_000 , F : 9 }, // TODO: this seems odd, feels like it should be 5
2640+ {T : 40_000 , F : 0 },
2641+ {T : 60_000 , F : 0 },
2642+ },
2643+ },
2644+ },
2645+ },
2646+ {
2647+ name : "back to back steps" ,
2648+ load : `load 10s
2649+ http_requests 1 5 10 20 _ 40` ,
2650+ query : "xincrease(http_requests[10s])" ,
2651+
2652+ startTime : time .Unix (0 , 0 ),
2653+ endTime : time .Unix (60 , 0 ),
2654+ step : 10 * time .Second ,
2655+
2656+ expected : promql.Matrix {
2657+ promql.Series {
2658+ Metric : labels .New (),
2659+ Floats : []promql.FPoint {
2660+ {T : 00_000 , F : 1 },
2661+ {T : 10_000 , F : 4 },
2662+ {T : 20_000 , F : 5 },
2663+ {T : 30_000 , F : 10 },
2664+ {T : 40_000 , F : 0 },
2665+ {T : 50_000 , F : 20 },
2666+ {T : 60_000 , F : 0 },
2667+ },
2668+ },
2669+ },
2670+ },
2671+ {
2672+ name : "overlapping steps" ,
2673+ load : `load 10s
2674+ http_requests 1 5 10 20 _ 40` ,
2675+ query : "xincrease(http_requests[20s])" ,
2676+
2677+ startTime : time .Unix (0 , 0 ),
2678+ endTime : time .Unix (60 , 0 ),
2679+ step : 10 * time .Second ,
2680+
2681+ expected : promql.Matrix {
2682+ promql.Series {
2683+ Metric : labels .New (),
2684+ Floats : []promql.FPoint {
2685+ {T : 00_000 , F : 1 },
2686+ {T : 10_000 , F : 4 },
2687+ {T : 20_000 , F : 9 },
2688+ {T : 30_000 , F : 15 },
2689+ {T : 40_000 , F : 10 },
2690+ {T : 50_000 , F : 20 },
2691+ {T : 60_000 , F : 20 },
2692+ },
2693+ },
2694+ },
2695+ },
2696+ }
2697+
2698+ for _ , tc := range cases {
2699+ t .Run (tc .name , func (t * testing.T ) {
2700+ storage := promqltest .LoadedStorage (t , tc .load )
2701+ defer storage .Close ()
2702+
2703+ ctx := context .Background ()
2704+ newEngine := engine .New (engine.Opts {
2705+ EngineOpts : opts ,
2706+ LogicalOptimizers : logicalplan .AllOptimizers ,
2707+ EnableXFunctions : true ,
2708+ })
2709+ query , err := newEngine .NewRangeQuery (ctx , storage , nil , tc .query , tc .startTime , tc .endTime , tc .step )
2710+ testutil .Ok (t , err )
2711+ defer query .Close ()
2712+
2713+ engineResult := query .Exec (ctx )
2714+ testutil .Ok (t , engineResult .Err )
2715+
2716+ gotMatrix , err := engineResult .Matrix ()
2717+ require .NoError (t , err )
2718+
2719+ for i := range tc .expected {
2720+ testutil .WithGoCmp (comparer ).Equals (t , tc .expected [i ].Floats , gotMatrix [i ].Floats , queryExplanation (query ))
2721+ }
2722+ })
2723+ }
2724+ }
2725+
26042726func TestXFunctionsWithNativeHistograms (t * testing.T ) {
26052727 defaultQueryTime := time .Unix (50 , 0 )
26062728
0 commit comments