Skip to content

feat(engine): Phase 3b-2-b — per-segment timeline dispatch for combinable stats - #48

Merged
zzylol merged 1 commit into
mainfrom
feat/phase3-timeline-dispatch
Apr 20, 2026
Merged

zzylol merged 1 commit into
mainfrom
feat/phase3-timeline-dispatch

Conversation

@zzylol

@zzylol zzylol commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Task #34 gap #1 of 3 — the blocker for "queries spanning a reconfigure boundary no longer see a data cliff." The §7 schema-timeline primitive (`SchemaRegistry::timeline_for_metric`) and the cross-schema combiner (`engines::timeline_dispatch::combine_statistic`) landed in #20 / #22 / #25, but `SimpleEngine::handle_query_promql` was still resolving a single `agg_id` via `resolve_agg_info_promql` and running the full query against it. A query whose time range spans a reconfigure boundary (old `agg_id` retired, new one created) saw a data cliff for the pre-boundary slice.

This PR wires the dispatcher.

What's in this PR

  • New `SimpleEngine::try_handle_query_promql_via_timeline`:
    1. Parse + pattern-match the query, extract metric name.
    2. Build a probe `QueryExecutionContext` to read the resolved `[t1, t2]` + `Statistic`.
    3. Call `timeline_for_query(metric, t1, t2)`. Return `None` (fall-through to single-agg path) if fewer than two segments, or if the statistic is non-combinable.
    4. Per segment: reuse `build_query_execution_context_promql_for_agg_id` from refactor(sketch-db): Phase 3b-2-a — extract forced-agg-id context builder #37, clip store plan `[start, end]` to the segment's bounds, execute, collect results.
    5. Group by label-tuple and fold per-group per-segment scalars through `combine_statistic`. Emit the combined scalar as an `InstantVectorElement`. Purged segments and segments whose `agg_id` is no longer in the config go into `unresolved`.
  • `handle_query_promql` now tries the timeline path first; returns immediately on `Some`, falls through to the existing single-agg path on `None`.

Scope

Combinable stats only: Count / Sum / Min / Max. Non-combinable stats (quantile / topk / cardinality / rate / increase) stay on the single-agg path — PR B2 will surface `CombinedResult::Partial` on the HTTP adapter so users see `{covered, missing: [segments]}` explicitly instead of a silent cliff.

Zero behavior change when timeline has 0–1 segments for the query's metric (the common case today).

Validation

  • `cargo test -p query_engine_rust --lib` — 728 pass (baseline unchanged; dispatcher stays dormant when tests only register one schema per metric)
  • `cargo clippy --all-targets -- -D warnings` — clean
  • `cargo fmt --all -- --check` — clean

Follow-ups (explicit non-scope here)

  • Integration test seeding two agg_ids + cross-boundary Sum query. Requires the full `PrecomputeEngine` setup harness the existing e2e tests use; deferred as a dedicated PR so this one stays a focused dispatcher patch.
  • PR B2 — Partial response surface on the HTTP adapter for non-combinable stats.

🤖 Generated with Claude Code

…able stats

Task #34 gap #1 of 3. The §7 schema-timeline primitive
(`SchemaRegistry::timeline_for_metric`) and the cross-schema
combiner (`engines::timeline_dispatch::combine_statistic`)
landed in PRs #20 / #22 / #25, but `SimpleEngine::handle_query_promql`
was still resolving a single `agg_id` via `resolve_agg_info_promql`
and running the full query against it. A query whose time range
spans a reconfigure boundary (old `agg_id` retired, new
`agg_id` created) saw a data cliff for the pre-boundary slice.

This PR wires the dispatcher:

* New `SimpleEngine::try_handle_query_promql_via_timeline`:
  1. Parse + pattern-match the query, extract metric name.
  2. Build a probe `QueryExecutionContext` to read the
     resolved `[t1, t2]` + `Statistic`.
  3. Call `timeline_for_query(metric, t1, t2)`. Bail out
     with `None` (fall-through to default single-agg path)
     if fewer than two segments, or if the statistic is
     non-combinable (quantile / topk / cardinality / rate
     / increase — those follow in PR B2 with a Partial
     HTTP response surface).
  4. Per segment: reuse `build_query_execution_context_promql_for_agg_id`
     from PR #37 (the extracted forced-agg-id entry point),
     clip the store plan's `[start, end]` to the segment's
     bounds, execute, collect results.
  5. Group by label-tuple and fold per-group per-segment
     scalars through `combine_statistic`. Emit the combined
     scalar as an `InstantVectorElement`. Purged segments
     or segments whose `agg_id` is no longer in the config
     go into `unresolved` so the combiner sees them.

* `handle_query_promql` now tries the timeline path first;
  returns immediately on `Some`, falls through to the
  existing single-agg path on `None`. Zero behavior change
  when the timeline has 0–1 segments for the query's metric
  (the common case today).

## Scope

Combinable stats only: Count / Sum / Min / Max. Non-combinable
stats still take the single-agg path — PR B2 will surface
`CombinedResult::Partial` on the HTTP response so users see
`{covered, missing: [segments]}` explicitly instead of a
silent data cliff.

## Validation

- `cargo test -p query_engine_rust --lib` — 728 pass (baseline
  unchanged; the dispatcher stays dormant when tests only
  register one schema per metric).
- `cargo clippy --all-targets -- -D warnings` — clean
- `cargo fmt --all -- --check` — clean

## Follow-ups (explicit non-scope here)

- **Integration test** seeding two agg_ids + cross-boundary
  Sum query. Requires the full `PrecomputeEngine` setup
  harness the existing e2e tests use; deferred as a
  dedicated PR so this one stays a focused dispatcher
  patch.
- **PR B2**: Partial response surface for non-combinable
  stats on the HTTP adapter.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@zzylol
zzylol merged commit 9ea51ab into main Apr 20, 2026
@zzylol
zzylol deleted the feat/phase3-timeline-dispatch branch April 20, 2026 16:11
zzylol added a commit that referenced this pull request Apr 20, 2026
Task #34 gap #2 of 3. #48's dispatcher activated per-segment
execution for combinable stats (Count/Sum/Min/Max) but refused
to run for non-combinable stats (Quantile/Topk/Cardinality/
Rate/Increase) — those still fell through to the single-agg
path and saw the same data cliff the dispatcher was written to
prevent. Reason: there was no way to tell the HTTP caller
"this answer is Partial because the query spans a reconfigure
boundary and the statistic can't be combined scalarly."

Changes:

- **`QueryResult::vector_with_warnings` + `warnings()` accessor.**
  `InstantVector` and `RangeVector` gain a
  `#[serde(default, skip_serializing_if = "Vec::is_empty")]`
  `warnings: Vec<String>` field. Default constructors
  (`QueryResult::vector`, `QueryResult::matrix`) stay wire-
  compatible — they still serialise without the field when
  empty, so every existing caller and snapshot test holds.

- **Prometheus adapter** adds a top-level `warnings: []` on
  `PrometheusResponse` (also skip-if-empty), matching
  upstream's native API. `format_success_response` +
  `format_range_success_response` thread through any
  warnings the engine populated; absent → no field.

- **Dispatcher** drops the combinable-only early return.
  The full loop now runs for every statistic whenever the
  timeline has ≥2 segments. `combine_statistic` still
  returns `Full(v)` for cleanly-combinable inputs and
  `Partial { covered, missing }` everywhere else — on
  Partial we accumulate the `covered` scalar (when present),
  flag `any_partial`, and build a human-readable warnings
  list with the metric, range, statistic, dropped-group
  count, and up to three unresolved segments (agg_id,
  clipped range, status, coverage). Over-three are summarised;
  the full set is still inspectable via
  `GET /api/v1/db/timeline`.

5 new unit tests: 3 in `engines::query_result::tests` covering
the default-empty / with-warnings / matrix wire contract;
2 in the Prometheus adapter covering the response-side
serialisation contract. 734 lib tests pass (+5), clippy clean,
fmt clean.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant