Repository: prometheus/prometheus (language: go)

Issue:
Inconsistent behaviour of range vector selectors and subqueries with 0 range
### What did you do?

I ran two queries: `metric[0]` (range vector selector) and `metric[0:30s]` (subquery).

I ran both queries as instant queries at a timestamp T where `metric` had a sample, and T was a integer multiple of 30s from the epoch.

### What did you expect to see?

I expected both to return no results, or both to return results.

### What did you see instead? Under which circumstances?

`metric[0]` returned the sample at timestamp T, while `metric[0:30s]` returned no results.

### System information

_No response_

### Prometheus version

```text
Prometheus at commit 0a19f1268e2dbb9bae9568f007a9809e8fba0402
```

### Prometheus configuration file

```yaml
n/a
```

### Alertmanager version

```text
n/a
```

### Alertmanager configuration file

```yaml
n/a
```

### Logs

```text
n/a
```


Discussion / hints:

Fix patch:
diff --git a/promql/engine.go b/promql/engine.go
index 44985e50f90..3e14a2529d9 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 {


Test patch:
diff --git a/promql/engine_test.go b/promql/engine_test.go
index 9ba93abc780..5c4bb744f52 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 {