Skip to content

refactor(query-engine): add Scalar and Table QueryResult variants - #938

Draft
milindsrivastava1997 wants to merge 1 commit into
refactor/query-requestfrom
refactor/query-result-variants
Draft

milindsrivastava1997 wants to merge 1 commit into
refactor/query-requestfrom
refactor/query-result-variants

Conversation

@milindsrivastava1997

@milindsrivastava1997 milindsrivastava1997 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Change

  • QueryResult gains Scalar(Sample), one timestamped value (the PromQL scalar), and Table(Table { columns, rows }), where cells are TableValue::{String, Float}. Table::is_rectangular() checks that every row has one value per column.
  • QueryResultType (promql_utilities) gains Scalar and Table, so QueryResult::result_type() stays total.
  • Each response adapter formats what its protocol can express and errors on the rest:
Adapter Vector Matrix Scalar Table
Prometheus instant (convert_query_result_to_prometheus) as before error → 500 (as before) {"resultType":"scalar","result":[ts,"v"]} error → 500
Prometheus range (convert_range_result_to_prometheus) error → 500 (as before) as before error → 500 error → 500
ClickHouse (TSV) as before 501 (as before) 501 one tab-separated line per row
Elastic (hits) as before 501 (as before) 501 one hit per row, _source keyed by column

A non-rectangular Table returns a 500 from ClickHouse and Elastic; it is never formatted after silently truncating or padding rows.

Scope choices to review

  • Prometheus formats Scalar rather than erroring. It is a native Prometheus result type, the formatting is fixed by the protocol, and new engines will produce it for scalar(...) and numeric literals. Erroring instead is a one-line change.
  • Table cells are String | Float. That covers today's SQL flattening (label values plus one value). Add more types when an engine produces them. The real cell types (ints, NULLs, bools from the executor's Arrow batches) are revisited when the executor result is converted to QueryResult, in the executor QueryEngine / wiring steps.

Behavior

No change: nothing produces Scalar or Table yet, and the Vector/Matrix paths are untouched.

Tests

  • New, 9 tests: Prometheus scalar formatting; Prometheus rejects Table (instant) and Scalar/Table (range); ClickHouse and Elastic format Table, return 500 for a ragged table, and return 501 for Scalar.
  • Changed beyond imports: 9 test-helper matches on QueryResult (stage_e_instant_range_equivalence_tests, one simple_engine test helper, e2e_precompute_equivalence) gain an other => panic!(..) arm so they stay exhaustive. Their existing arms are untouched.
  • cargo fmt, cargo clippy --workspace --all-targets --all-features -D warnings and cargo test --workspace --locked pass: 1546 passed (1537 + 9).

🤖 Generated with Claude Code

QueryResult gains Scalar (one timestamped value) and Table {columns,
rows}. Response adapters format what their protocol can express and
error on the rest: Prometheus formats Scalar as resultType "scalar" and
rejects Table; ClickHouse (TSV) and Elastic (hits) format Table and
return NOT_IMPLEMENTED for Scalar, as they do for Matrix. A table whose
rows don't match its columns is a 500, not silently truncated.

Nothing produces the new variants yet. Exhaustive test matches gain a
panic arm for them.

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/clickhouse_http.rs (Table branch): string cells are written to TabSeparated without escaping. A cell "a\tb" with float 1.0 becomes a\tb\t1\n (3 columns instead of 2); an embedded newline splits the row. Escape \, \t, \n, \r, \0 as ClickHouse does. The Vector branch writes label values the same way.
  2. asap-adapters/src/drivers/query/adapters/elastic_http.rs (Table branch): cells go into a serde_json::Map keyed by column name, so duplicate names silently overwrite. SELECT host, max(v) AS x, min(v) AS x returns only min. Reject duplicates with a 500 (like the ragged-table check) or disambiguate.

Nits
3. elastic_http.rs: json!(f) turns NaN/±Inf into null, indistinguishable from a missing value (e.g. p99 over an empty group).
4. asap-query-engine/src/utils/http.rs (new Scalar path): value.to_string() emits inf/-inf; Prometheus clients expect +Inf/-Inf. A shared Prometheus float formatter would fix Scalar and Vector (which already has this).
5. clickhouse_http.rs: non-finite Table floats are written as NaN/inf/-inf; ClickHouse TSV uses nan. Same in the Vector branch.

Pre-existing / latent
6. asap-query-engine/src/engines/simple_engine/sql.rs (apply): only QueryResult::Vector is sorted/truncated; other => other passes a Table through, so ORDER BY / LIMIT would be silently ignored for Tables. The doc comment still says only matrices pass through. Sort/truncate Tables or error until supported.

🤖 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