Skip to content

refactor(query-engine): replace ParsedQueryRequest with QueryRequest - #937

Draft
milindsrivastava1997 wants to merge 1 commit into
refactor/asap-adapters-cratefrom
refactor/query-request
Draft

milindsrivastava1997 wants to merge 1 commit into
refactor/asap-adapters-cratefrom
refactor/query-request

Conversation

@milindsrivastava1997

@milindsrivastava1997 milindsrivastava1997 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Plan step 2 of .design_docs/asapplanner-engine-boundaries.md, stacked on #936 (step 1).

Change

  • New query_engine_rust::engine_api::QueryRequest { language, query, eval }, where eval is QueryEval::Instant { time_ms } or QueryEval::Range { start_ms, end_ms, step_ms }, in epoch milliseconds. It replaces ParsedQueryRequest and ParsedRangeQueryRequest. It lives in the engine crate because the future QueryEngine::execute takes it. engine_api/ is the module the plan puts the interfaces in, and step 5 adds to it.
  • Adapters (Prometheus, ClickHouse, Elastic) fill language from their config and convert protocol seconds with secs_to_ms.
  • FallbackClient now has a single execute_query(&QueryRequest) (plus _with_headers). execute_range_query* is gone. Prometheus forwards to /api/v1/query or /api/v1/query_range depending on eval. ClickHouse and Elastic return 501 for range queries, which is what the removed default returned.
  • HttpServer takes the evaluation times out of eval and passes float seconds (ms_to_secs) to SimpleEngine and the query tracker, whose APIs don't change. If an adapter returns the wrong eval kind for an endpoint, the server logs it and returns a protocol error; it never falls back silently.

Deliberate behavior change (the only one)

A range step under 0.5 ms now fails validation with step must be positive, because it rounds to 0 ms. Before, such a step was accepted and forwarded. Everything else is bit-identical for ms-aligned input (see Behavior).

Behavior

Unchanged for millisecond-aligned input:

  • secs_to_ms rounds to the nearest ms. Truncation would break the round trip: 1095211880.820 * 1000.0 = 1095211880819.9998. With rounding, ms_to_secs(secs_to_ms(t)) is the exact f64 that parsing t gave before, so SimpleEngine, the tracker and the fallback strings see the same values. This held for 2M random ms values (years 1970–2100) and is covered by unit tests.
  • Sub-ms precision is rounded to ms. Prometheus evaluates at ms precision anyway.
  • Range validation (start < end, step > 0) runs on the ms values, so a step under 0.5 ms is rejected instead of reaching the engine as about 0.

Pre-existing, not changed here: SimpleEngine::convert_query_time_to_data_time truncates t * 1000.0, so about 0.6% of ms-aligned query times map to a data time 1 ms early.

Tests

  • New: engine_api round-trip and rounding; the Prometheus adapter fills language, converts time to ms and rejects a sub-ms step; the Prometheus fallback forwards the same time/start/end/step strings as f64::to_string() did; ClickHouse and Elastic fallbacks return 501 for range queries.
  • Changed beyond imports, mechanically, because the type replaced: adapter and fallback tests that built ParsedQueryRequest literals or read .time/.start. Their assertions are the same, expressed in ms.
  • cargo fmt, cargo clippy --workspace --all-targets --all-features -D warnings and cargo test --workspace --locked pass: 1537 passed (1529 + 8 new).

Docs: adding-fallback-backend.md and adding-protocol-adapter.md now name QueryRequest.

🤖 Generated with Claude Code

One QueryRequest {language, query, eval: Instant | Range} in the engine
crate's new engine_api module replaces ParsedQueryRequest and
ParsedRangeQueryRequest. Times are epoch ms: adapters round protocol
seconds to the nearest ms, so ms-aligned inputs round-trip to the exact
f64 SimpleEngine, the tracker and the Prometheus fallback saw before.

FallbackClient takes one execute_query(&QueryRequest); ClickHouse and
Elastic return NOT_IMPLEMENTED for range evaluation, as the removed
execute_range_query default did. Range validation runs on ms, so a step
under 0.5 ms is now rejected instead of reaching the engine as ~0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milindsrivastava1997

Copy link
Copy Markdown
Contributor Author

Code review

Important

  1. asap-adapters/src/drivers/query/adapters/prometheus_http.rs (secs_to_ms / parse_params / parse_range_params): non-finite times are not rejected. .round() as i64 maps NaN → 0 and ±Inf → i64::MIN/MAX, and only the ms values are validated. query_range?query=up&start=NaN&end=1700001000&step=60 yields start_ms = 0, passes start_ms < end_ms, and asks for ~28M steps from 1970 (also forwarded to the fallback). time=NaN runs an instant query at epoch 0. Fix: reject !t.is_finite() before converting, or make secs_to_ms return Result.

Nits
2. prometheus_http.rs: the start<end check now compares rounded ms instead of raw f64 seconds, so ranges < 0.5 ms that used to pass (e.g. start=1700000000.0001&end=1700000000.0004) are now rejected. Validate the raw values before converting.
3. asap-adapters/src/drivers/query/servers/http.rs (wrong_eval_response): an adapter returning the wrong eval kind is a server bug but is reported as AdapterError::ProtocolError (client-style). Consider a 500. Typo: "an range query".
4. asap-adapters/src/drivers/query/fallback/prometheus.rs test comment: "must equal what the f64-seconds path sent" refers to a path that no longer exists (history narration). State the invariant instead: for ms-aligned input, the forwarded value equals f64::to_string() of the parsed seconds.
5. asap-query-engine/docs/adding-fallback-backend.md: sample signature now returns Result<FallbackResponse, StatusCode>, but the trait-methods bullet still says execute_query "Returns Json<Value> response" and doesn't mention NOT_IMPLEMENTED for range queries.

Pre-existing
6. prometheus_http.rs: start_ms >= end_ms rejects start == end; Prometheus accepts it and returns a single point. Consider rejecting only start_ms > end_ms.

Findings from a diff-only review; not verified by building/running.

🤖 Generated with Claude Code

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