diff --git a/promql/engine.go b/promql/engine.go index 44985e50f9..3e14a2529d 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -2357,6 +2357,11 @@ func (ev *evaluator) matrixIterSlice( } } + if mint == maxt { + // Empty range: return the empty slices. + return floats, histograms + } + soughtValueType := it.Seek(maxt) if soughtValueType == chunkenc.ValNone { if it.Err() != nil { diff --git a/promql/engine_test.go b/promql/engine_test.go index 9ba93abc78..5c4bb744f5 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -1900,6 +1900,15 @@ func TestSubquerySelector(t *testing.T) { }, Start: time.Unix(35, 0), }, + { + Query: "metric[0:10s]", + Result: promql.Result{ + nil, + promql.Matrix{}, + nil, + }, + Start: time.Unix(10, 0), + }, }, }, { @@ -3199,6 +3208,7 @@ func TestInstantQueryWithRangeVectorSelector(t *testing.T) { load 1m some_metric{env="1"} 0+1x4 some_metric{env="2"} 0+2x4 + some_metric{env="3"} {{count:0}}+{{count:1}}x4 some_metric_with_stale_marker 0 1 stale 3 `) t.Cleanup(func() { require.NoError(t, storage.Close()) }) @@ -3226,6 +3236,13 @@ func TestInstantQueryWithRangeVectorSelector(t *testing.T) { {T: timestamp.FromTime(baseT.Add(2 * time.Minute)), F: 4}, }, }, + { + Metric: labels.FromStrings("__name__", "some_metric", "env", "3"), + Histograms: []promql.HPoint{ + {T: timestamp.FromTime(baseT.Add(time.Minute)), H: &histogram.FloatHistogram{Count: 1, CounterResetHint: histogram.NotCounterReset}}, + {T: timestamp.FromTime(baseT.Add(2 * time.Minute)), H: &histogram.FloatHistogram{Count: 2, CounterResetHint: histogram.NotCounterReset}}, + }, + }, }, }, "matches no series": { @@ -3251,6 +3268,11 @@ func TestInstantQueryWithRangeVectorSelector(t *testing.T) { }, }, }, + "matches series but range is 0": { + expr: "some_metric[0]", + ts: baseT.Add(2 * time.Minute), + expected: promql.Matrix{}, + }, } for name, testCase := range testCases {