Skip to content

feat(promql): preserve and plan topk over sum_over_time/count_over_time (ASAPQuery #706 parity) #342

Closed

Description

@zzylol

Summary

Add semantic planning parity with ProjectASAP/ASAPQuery#706 for Top-K over sum_over_time and count_over_time.

Reproduction

Both queries lower successfully through frontend-promql, but the normal summary-selection path used by ASAPQuery-backend currently produces no legal summary candidate for at least the value-ranked case:

topk(5, sum_over_time(m[1m]))
topk by (job) (5, count_over_time(m[1m]))

ASAPQuery #706 defines an important semantic distinction:

  • topk(sum_over_time(...)) ranks each series by the sum of sample values (value-weighted heap);
  • topk(count_over_time(...)) ranks each series by the number of samples (count-weighted heap).

The current AggIntent::TopK / SketchQuery::TopK representation carries k but does not carry the ranking/weighting basis, so downstream physical compilation cannot preserve this distinction without re-parsing PromQL.

Scope

  • Represent the Top-K ranking basis in canonical pre-ASAP and post-ASAP IR using language-neutral semantics.
  • Lower the two fix(asap-physical-operators): seed each CountSketch row independently #706 query shapes to distinct semantic plans.
  • Generate legal heap-bearing summary candidates for both nested temporal cases.
  • Preserve by(...) grouping separately from the in-summary item key.
  • Reject non-collapsible shapes (topk(rate(...)), topk(avg_over_time(...)), etc.) rather than silently selecting a wrong summary.
  • Keep SQL/other frontends source-compatible by requiring them to choose the appropriate ranking basis explicitly.

Acceptance criteria

  • PromQL lowering tests pin the distinct ranking basis for both queries.
  • ASAP-aware mapping tests produce heap-bearing candidates with count vs value update semantics.
  • End-to-end planner tests cover grouped and ungrouped forms plus negative non-collapsible cases.
  • Serialized IR round-trips without dropping the ranking basis.

Downstream execution tracking: ProjectASAP/ASAPQuery-backend#502.
Parent compatibility audit: ProjectASAP/ASAPQuery-backend#501.

Activity

  1. added
    enhancementNew feature or request
    irPre-ASAP or post-ASAP intermediate representation
    optimizerWorkload optimization and plan selection
    promqlPromQL front-end lowering (L1→L2)
    on Sep 4, 2026
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

    enhancementNew feature or requestirPre-ASAP or post-ASAP intermediate representationnested queriesoptimizerWorkload optimization and plan selectionpromqlPromQL front-end lowering (L1→L2)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions