From 52fa3a2e166c2d520d0cdfe9555d985347de9303 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Tue, 20 Oct 2026 15:14:23 +0800 Subject: [PATCH] docs(promql): write down what an anchored window reads, and cover a pre-epoch anchor An `@`-anchored window reads `(anchor - offset - range, anchor - offset]`, which can lie entirely outside the statement's own range, so whoever binds the scan has to derive it from the window rather than from `stmt.start` and `stmt.end`. A scan bound to the statement alone leaves such a window with no samples, and the query returns empty without an error. That contract was nowhere in the code. The anchor cases only covered non-negative anchors, so a helper that defaulted a pre-epoch anchor to zero would have kept the suite green. `rate(x[5m] @ -1.5)` now pins -1500, which is the `Err(earlier)` branch of the conversion. The `assert_ne!` at the end of the `end()` test was implied by the two equalities above it and could not fail on its own. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/query/src/promql/native_compile.rs | 7 +++++++ src/query/src/promql/native_compile/tests.rs | 8 +++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 1d5a0801e84..831f10eab6d 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -270,6 +270,13 @@ fn lookback_millis(lookback: u128) -> Result { /// range they name, so they are resolved against `stmt.start` and `stmt.end` here. The offset is not /// folded in: [`RangeWindow`] keeps anchor and offset apart and ends the window at /// `anchor_ms - offset_ms` at run time. +/// +/// An anchor decouples the window's read range from the statement's: it reads +/// `(anchor_ms - offset_ms - range_ms, anchor_ms - offset_ms]`, which for an anchor like `@ 1234` can +/// lie entirely outside `[stmt.start - range_ms, stmt.end]`. Whoever binds the scan therefore has to +/// derive its range from the window's anchor, offset and range rather than from `stmt.start` and +/// `stmt.end`; a scan bound to the statement alone leaves such a window with no samples, and the +/// query silently returns empty instead of failing. fn selector_anchor_ms(at: &Option, stmt: &EvalStmt) -> Result> { match at { None => Ok(None), diff --git a/src/query/src/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs index 59cac16f6df..1918ce3ec2e 100644 --- a/src/query/src/promql/native_compile/tests.rs +++ b/src/query/src/promql/native_compile/tests.rs @@ -269,6 +269,9 @@ fn an_anchor_on_a_window_resolves_to_absolute_milliseconds() { ("rate(x[5m] @ 1234)", 1_234_000), // A sub-second anchor resolves to the millisecond it names. ("rate(x[5m] @ 1.234)", 1_234), + // Prometheus allows a pre-epoch anchor: `signed_millis_since_epoch` takes `Err(earlier)`, + // where a `duration_since(UNIX_EPOCH).unwrap_or_default()` helper would silently give 0. + ("rate(x[5m] @ -1.5)", -1_500), // The statement here is UNIX_EPOCH..100s, which is what `start()` and `end()` mean. ("rate(x[5m] @ start())", 0), ("rate(x[5m] @ end())", 100_000), @@ -309,11 +312,6 @@ fn end_resolves_to_the_statement_end_not_the_last_grid_point() { last_point, 100_000, "the test's own grid does not stop before the statement end" ); - assert_ne!( - op.window.anchor_ms, - Some(last_point), - "`@ end()` bound the last grid point rather than the statement's end" - ); } /// An anchor and an offset are bound apart: the window ends at `anchor - offset`, which the