Skip to content

Rewrite avg_over_time as sum over count #410

Open

Description

@milindsrivastava1997

What to build

Add a semantic-equivalent rewrite for PromQL avg_over_time that exposes mergeable sum and count work, then divides them to preserve the original result.

The existing grouped avg rewrite follows this algebra:

avg(x)  →  float(sum(x)) / count(*)

avg_over_time(x[5m]) is lowered as a Reduction::PerEntity aggregate over the range. It is currently excluded from the rewrite:

let Reduction::Reduce(by) = reduction else {
    return None;
};

After the per-entity multi-measure/schema capability is available, implement the analogous range rewrite while preserving PromQL series identity, labels, timestamps, output name, Float64 type, and null/denominator semantics. The rewrite should make the sum and count portions independently available to existing reuse/realization strategies and return exactly the same observable result as avg_over_time.

Acceptance criteria

  • Eligible avg_over_time expressions produce a semantic-equivalent sum/count/divide rewrite candidate.
  • The rewritten result preserves input-series identity, labels, timestamps, output name, and Float64 output schema.
  • The rewrite is not offered when it could change null or denominator semantics.
  • Tests demonstrate equivalence for a representative range query and confirm that the rewrite exposes reusable sum and count work.

Blocked by

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions