Skip to content

fix(query-engine): Sliding range queries no longer sum overlapping windows - #621

Merged
milindsrivastava1997 merged 2 commits into
mainfrom
worktree-608-sliding-range-exact-fetch
Aug 26, 2026
Merged

fix(query-engine): Sliding range queries no longer sum overlapping windows#621
milindsrivastava1997 merged 2 commits into
mainfrom
worktree-608-sliding-range-exact-fetch

Conversation

@milindsrivastava1997

Copy link
Copy Markdown
Contributor

What was wrong

A PromQL range query over a Sliding-window aggregation (e.g. sum_over_time(metric[5s]) stepped out over time) could silently return the wrong numbers.

Each stored Sliding-window bucket is already a complete, correct answer for its own window — the precompute worker pre-merges everything before storing it. The range query's per-step logic didn't know that: at each output timestamp, it walked back across every bucket in the lookback window and added them all together. That's the right thing to do for Tumbling windows (genuinely separate, non-overlapping chunks that are meant to be summed), but for Sliding windows it meant adding several overlapping windows on top of each other — double- or triple-counting the same underlying data.

Example: with a 3-second Sliding window, one step's correct answer was 111, but the buggy code returned 12321 (111 + 1110 + 11100 — three overlapping windows summed instead of one).

The fix

The per-step logic now checks whether the aggregation is Sliding or Tumbling:

  • Sliding: look up exactly the one bucket that matches this step's window. No more, no less.
  • Tumbling: unchanged — still sums every bucket in the lookback window, which is correct there.

This applies to both the main data being queried and (for metrics that track keys/labels separately) the keys side too, since keys can also be configured as Sliding.

No extra work needed to fetch this data — it was already being pulled from the store in one batch beforehand; the fix is entirely about how that already-fetched data gets combined per step.

Testing

  • Two existing tests that pinned this exact bug are now passing.
  • Added a new test covering the same bug on the keys/labels side.
  • Verified the new test genuinely fails without the fix (not a vacuous pass) by temporarily reverting just that part of the fix, confirming the expected failure, then restoring it.
  • Full test suite passes (499 tests, 0 failures).

Fixes #608.

…ndows

execute_range_query_pipeline's per-step composition (scan_window) walked
every grid position in a step's lookback span and summed whatever it
found there. That's correct for Tumbling, where each stored bucket is a
genuinely disjoint slice -- but wrong for Sliding, where each stored
bucket is already a complete, pre-merged window (worker.rs pre-merges
before storing). Summing several of those together double/triple-counted
overlapping data.

Both value-side and keys-side per-step composition now branch on the
aggregation's WindowType: Sliding takes a single lookup at the step's
exact window position (the data was already fetched by the existing wide
fetch, so no extra store round-trips); Tumbling is unchanged.

Fixes #608.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@milindsrivastava1997
milindsrivastava1997 marked this pull request as ready for review August 26, 2026 02:40
…ck invariant

Code review on #621 flagged two follow-ups:
- The Sliding-vs-Tumbling branch (single_window vs scan_window) was
  duplicated near-verbatim between the value-side and keys-side loops.
  Extracted into one window_buckets_for_step helper, used by both.
- single_window's correctness for Sliding depends on lookback_ms ==
  window_size_ms, previously documented but not checked anywhere.
  Threaded window_size_ms/keys_window_size_ms through
  RangeQueryExecutionContext and added active asserts (not
  debug_assert!) guarding the equality, matching scan_window's existing
  precedent of asserting its own precondition -- a broken invariant here
  means silently wrong data, the same failure mode #608 fixed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@milindsrivastava1997
milindsrivastava1997 merged commit b352f88 into main Aug 26, 2026
5 checks passed
@milindsrivastava1997
milindsrivastava1997 deleted the worktree-608-sliding-range-exact-fetch branch August 26, 2026 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Range queries over Sliding-window aggregations use overlap-scan fetch, not exact-window fetch

1 participant