PromQL

PromQL is parsed, type-checked and evaluated inside the Prometheus wire by its own engine over TimeSeriesStorage. It does not build a LogicalPlan and is not reachable from the SQL wires. The function table, query-API limits, error codes, storage layout and proof evidence are in Prometheus: implementation reference; this page describes the language engine and its translation path.

Translation path

flowchart LR H["/api/v1/query, /api/v1/query_range"] --> P["parser::parse"] P --> C["parser check (static types)"] C --> E["eval::Engine"] E --> TS["TimeSeriesStorage (head in memory)"] TS -->|"older than head window"| KV["KV records __prom__:d:"] E --> J["instant_query_json, range_query_json"]

(source: src/protocol/prometheus/mod.rs, handle_instant_query, handle_range_query; src/protocol/prometheus/promql/parser.rs, parse, check; src/protocol/prometheus/promql/eval.rs, Engine; src/protocol/prometheus/promql/mod.rs, instant_query_json, range_query_json; src/protocol/prometheus/storage.rs, head_window_from_env; src/protocol/prometheus/persist.rs)

  1. Lexer and parser. lexer.rs tokenizes; parser.rs builds an Expr tree with a recursion limit of 256 (MAX_DEPTH) (source: src/protocol/prometheus/promql/lexer.rs, Tok; src/protocol/prometheus/promql/parser.rs, MAX_DEPTH).
  2. Static checks. check applies argument type and arity rules, restricts set operators to vector operands, requires bool for comparisons between scalars, and refuses selectors whose matchers all match the empty string (source: src/protocol/prometheus/promql/parser.rs, check, make_selector).
  3. Evaluation. Engine::new(storage, expr, start, end, step, DEFAULT_LOOKBACK_MS) evaluates instant queries with eval and range queries with run_range. A range query must have a scalar or instant-vector result type (source: src/protocol/prometheus/promql/mod.rs, range_query_json; src/protocol/prometheus/promql/eval.rs, Engine).
  4. Storage reads. The engine reads TimeSeriesStorage, which keeps a head window in memory (3 hours by default, SYNERGYDB_PROMETHEUS_HEAD_HOURS) and reads older samples back from the __prom__:d: key-value records (source: src/protocol/prometheus/storage.rs, head_window_from_env; src/protocol/prometheus/persist.rs).

eval_instant and eval_range are a second, float-only API returning QueryValue; the HTTP handlers use the JSON functions, and the integration tests in tests/protocol_prometheus.rs use eval_instant and eval_range (source: src/protocol/prometheus/promql/mod.rs, eval_instant, eval_range).

Grammar

Expression forms (Expr): number and string literals, vector selectors, matrix selectors sel[range], subqueries expr[range:step], binary expressions, unary minus or plus, aggregations, function calls and parentheses (source: src/protocol/prometheus/promql/ast.rs, Expr).

Element Accepted
Label matchers =, !=, =~, !~ (source: src/protocol/prometheus/promql/parser.rs, matcher_matches)
Arithmetic +, -, *, /, %, ^, atan2
Comparison ==, !=, <, <=, >, >=, with optional bool
Set and, or, unless
Vector matching on(...), ignoring(...), group_left(...), group_right(...); cardinalities one-to-one, many-to-one, one-to-many, many-to-many
Modifiers offset <duration> (negative allowed), @ <timestamp>, @ start(), @ end() on selectors and subqueries
Aggregations sum, avg, count, min, max, group, stddev, stdvar, topk, bottomk, count_values, quantile, limitk, limit_ratio, with by (...) or without (...)
Functions The 77 names in FUNCS; any other name is a parse error, "unknown function with name …"

(source: src/protocol/prometheus/promql/ast.rs, BinOp, Card, VectorMatching, AtModifier, AggOp::from_name; src/protocol/prometheus/promql/functions.rs, FUNCS, lookup, call; src/protocol/prometheus/promql/parser.rs, parse_call)

limitk and limit_ratio are experimental in Prometheus and accepted here without a feature flag (source: src/protocol/prometheus/promql/ast.rs, AggOp::from_name). info() and the ts_of_*_over_time functions are not in FUNCS (source: src/protocol/prometheus/promql/functions.rs, FUNCS).

Evaluation rules

Rule Value
Lookback for instant selectors 5 minutes, left-open window (DEFAULT_LOOKBACK_MS)
Default subquery step 1 minute (DEFAULT_SUBQUERY_STEP_MS)
Points per series in a range query at most 11,000 (MAX_POINTS_PER_SERIES)
Subquery steps at most 1,000,000 (MAX_SUBQUERY_STEPS)
Timestamps within plus or minus 2^60 ms (MAX_ABS_TS)
Staleness marker NaN with bits 0x7ff0000000000002 ends a series (STALE_NAN_BITS)
Histograms Classic bucket series and native float histograms (histogram::FloatHistogram)

(source: src/protocol/prometheus/promql/eval.rs, DEFAULT_LOOKBACK_MS, DEFAULT_SUBQUERY_STEP_MS, MAX_POINTS_PER_SERIES, MAX_SUBQUERY_STEPS, STALE_NAN_BITS; src/protocol/prometheus/promql/mod.rs, MAX_ABS_TS, check_range; src/protocol/prometheus/promql/histogram.rs, FloatHistogram)

The module comment claims Prometheus 3.x semantics, Kahan-compensated aggregation and extrapolated rate, increase and delta; the compliance run listed below is the evidence for those claims, not the comment (source: src/protocol/prometheus/promql/mod.rs, module comment).

Parse and static-type errors are HTTP 400 bad_data; evaluation errors are 422 execution (source: src/protocol/prometheus/mod.rs, promql_error). /api/v1/query_exemplars parses its query and always answers an empty list, because exemplars are not stored (source: src/protocol/prometheus/mod.rs, route_request).

Not supported

The timeout, lookback_delta, limit and stats query parameters are not read, so a query has no time limit (source: reference, section 7). Recording and alerting rules are not evaluated: /api/v1/rules and /api/v1/alerts are stubs (source: src/protocol/prometheus/mod.rs, handle_rules, handle_alerts).

Tests

  • src/protocol/prometheus/promql/tests.rs: parses_full_grammar, rejects_invalid_queries, scalar_arithmetic_and_precedence, lookback_and_offset, staleness_marker_ends_series, rate_handles_reset_and_extrapolation, vector_matching_and_aggregation, subquery_and_range_query, bad_ranges_and_timestamps_error, classic_histogram_quantile.
  • src/protocol/prometheus/promql/gomath.rs (1 test) and src/protocol/prometheus/promql/mod.rs (1 test).
  • tests/protocol_prometheus.rs calls eval_instant and eval_range directly, not over HTTP.
  • The official prometheus/compliance PromQL suite: 661 of 661 cases passed against Prometheus 3.13.5, recorded in docs/proof/2026-11-30/proof-pack-a3232c9/promql-compliance-run.txt.gz (source: reference, section 10).

The status note docs/status/2026-11-24-promql-engine.md says storage downsamples samples older than a day. The current storage code says samples are never downsampled, and the later docs/status/2026-11-27-prometheus-durable-storage.md replaced the storage design that note describes (source: src/protocol/prometheus/storage.rs, run_maintenance).