From 72b06dc730194d9b97d64135592e7a964f86cde4 Mon Sep 17 00:00:00 2001 From: Dennis Zhuang Date: Sun, 18 Oct 2026 01:24:00 +0800 Subject: [PATCH] fix(promql): drop retained samples when an anchored window is skipped Signed-off-by: Dennis Zhuang --- src/promql/src/range_eval.rs | 3 ++- src/promql/src/range_eval/tests.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/promql/src/range_eval.rs b/src/promql/src/range_eval.rs index 0f9ced2316a..38cdcb258c1 100644 --- a/src/promql/src/range_eval.rs +++ b/src/promql/src/range_eval.rs @@ -468,7 +468,8 @@ impl RangeEvaluator { for field in &mut self.fields { // After the last window, samples are still ingested so that the overflow and // histogram checks do not depend on how the series is chunked, but none are kept. - if state.cursor == points_read { + // An anchored selector can skip past the one point it reads. + if state.cursor >= points_read { field.head = field.timestamps.len(); } field.timestamps.drain(..field.head); diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index d6d67ce2686..379e73444d4 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -1182,3 +1182,22 @@ fn rejects_a_subquery_grid_longer_than_u64() { Err(DataFusionError::Plan(_)) )); } + +/// An anchored window that ends before the series starts skips past the one point it reads; +/// later chunks must not pile up either. +#[test] +fn anchored_window_before_the_series_retains_nothing() { + let window = RangeWindow { + range_ms: 10, + offset_ms: 0, + anchor_ms: Some(5), + }; + let mut evaluator = probe_evaluator(1, grid(100, 10, 3), window); + for chunk_index in 0..100 { + let rows: Vec = (0..100) + .map(|row| (50 + chunk_index * 100 + row, vec![Some(1.0)])) + .collect(); + evaluator.push(&chunk(&rows, 1, 0, false)).unwrap(); + assert!(evaluator.fields[0].timestamps.len() <= 100); + } +}