Skip to content

Support Avg (and other multi-statistic aggregations) in query engine #463

Description

@milindsrivastava1997

Background

Avg requires two precomputed accumulators — one for Sum and one for Count — and a divide at read time. The planner already handles this correctly: both the SQL and Elastic DSL planners map `Avg` → `[Statistic::Sum, Statistic::Count]`, producing two separate `MultipleSum` aggregation configs.

The query engine does not support this yet. All three paths return `None` (Prometheus fallback) for Avg:

  • Elastic (fixed in Elastic Avg: engine queries Rate but planner precomputes Sum+Count #456): was returning `Statistic::Rate` (wrong); now returns `None`
  • PromQL: `get_statistics_to_compute()` returns `[Sum, Count]` for `avg`/`avg_over_time`; the `len != 1` guard in `build_promql_execution_context_tail` returns `None`
  • SQL: `parse_single_statistic("avg")` returns `None` because `AggregationOperator::Avg.to_statistics()` yields two statistics

Root cause

`QueryMetadata.statistic_to_compute` is a single `Statistic`. Avg needs two lookups (Sum agg ID + Count agg ID) and a divide. `get_aggregation_id_info` also rejects query configs with two value-type aggregations ("Query config has two value-type aggregations (expected at most one)"), so even if we had the right statistic, the agg resolution would fail.

What a fix needs

  1. A way for the engine to look up two value-type aggregation IDs from a query config (Sum and Count), distinguished by `aggregation_sub_type` ("sum" vs "count")
  2. Execute both store queries, then divide element-wise (sum / count) to produce the final result
  3. Handle count == 0 (emit NaN or skip)

This applies to all three query paths (Elastic, PromQL, SQL).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions