#12374·signoz

Make PromQL scalar reduceTo operator configurable on the PromQuery spec

Author: Harsh23KashyapCreated Aug 2, 2026Updated Sep 26, 2026

Problem

pkg/querier/promql_query.go:387-389 carries a TODO(srikanthccv):

"last" mirrors the instant-query semantics we have always followed for PromQL scalar requests, but it should be configurable on the PromQuery spec just like the builder reduceTo.

For every PromQL scalar request, the execute path at pkg/querier/promql_query.go:390-396 reduces each series to its last point with qbv5.FunctionReduceTo(s, qbv5.ReduceToLast). The builder path already honors a configurable operator: pkg/querier/postprocess.go:222-227 reads query.Aggregations[0].ReduceTo and applies it via applyMetricReduceTo when set. The PromQL path has no equivalent field, so the operator is fixed at last.

Motivation

A PromQL scalar request returns one value per series, so the reduce operator is the only thing the caller can tune. The current hardcoded last is the right default for instant-query semantics, but it is not the right default for every dashboard or alert. With the field absent, a caller that wants sum, avg, max, or min has no way to ask for it on the PromQL path. The TODO explicitly asks for the builder parity.

Proposed solution

Add a reduceTo field to the PromQuery spec at pkg/types/querybuildertypes/querybuildertypesv5/prom_query.go, using the existing ReduceTo type from pkg/types/querybuildertypes/querybuildertypesv5/builder_elements.go:297. Wire it into the execute path: when the field is ReduceToUnknown (empty) the existing last behavior is preserved, otherwise the configured operator is used.

Concretely:

  • PromQuery.ReduceTo qbv5.ReduceTo \json:"reduceTo,omitzero"``
  • In Execute, replace the hardcoded qbv5.ReduceToLast with a helper that defaults ReduceToUnknown to ReduceToLast and otherwise applies the configured operator.

The same FunctionReduceTo helper is reused, so the actual reduction logic and the NaN handling are unchanged.

Scope

  • pkg/types/querybuildertypes/querybuildertypesv5/prom_query.go — add the ReduceTo field.
  • pkg/querier/promql_query.go — small helper, updated Execute, remove the TODO comment.
  • pkg/querier/promql_query_test.go — table-driven tests for the new helper covering ReduceToUnknown (default last), ReduceToSum, ReduceToAvg, ReduceToMax.

Acceptance criteria

  • A PromQuery with no reduceTo set produces byte-identical scalar output to the current implementation.
  • A PromQuery with reduceTo: "sum" reduces each series via FunctionReduceTo with ReduceToSum.
  • A PromQuery with reduceTo: "max" reduces each series via FunctionReduceTo with ReduceToMax.
  • The other supported operators (min, avg, count, median) follow the same pattern.
  • An unknown operator (reduceTo: "bogus") falls through to FunctionReduceTo's existing "unrecognized operator returns the original series" behavior.
  • go vet ./pkg/querier/... and go test -race -count=1 -short ./pkg/querier/... pass.
  • The TODO(srikanthccv) comment at pkg/querier/promql_query.go:387 is removed.

Backward compatibility

Adding a new optional field is non-breaking. Existing callers that omit reduceTo get the same last behavior. The JSON tag uses omitzero so the field is omitted from serialised output when not set.

Alternatives considered

  • Add validation that rejects unknown operators. Rejected: the existing FunctionReduceTo already has a graceful fallback (returns the original series for unrecognized operators). Adding an upstream validator would be a behavior change, not a parity change, and the builder path does not validate either.
  • Promote the reduce step to a public helper in pkg/querybuilder/. Rejected: the helper is querier-local, the same pattern lives next to its only caller, and the existing FunctionReduceTo already lives in the shared types package.
  • Add a separate ScalarReducer interface. Rejected: the field is the cleanest mirror of the builder's ReduceTo and the maintainer TODO explicitly references that parity.