promql: Fix HistogramStatsIterator usage for subqueries

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
beorn7 2025-09-04 14:36:56 +02:00
parent 4e94ee0109
commit 0746f388b0

View File

@ -3891,20 +3891,34 @@ func detectHistogramStatsDecoding(expr parser.Expr) {
return nil return nil
} }
pathLoop:
for i := len(path) - 1; i >= 0; i-- { // Walk backwards up the path. for i := len(path) - 1; i >= 0; i-- { // Walk backwards up the path.
if _, ok := path[i].(*parser.SubqueryExpr); ok {
// If we ever see a subquery in the path, we
// will not skip the buckets. We need the
// buckets for correct counter reset detection.
n.SkipHistogramBuckets = false
break pathLoop
}
call, ok := path[i].(*parser.Call) call, ok := path[i].(*parser.Call)
if !ok { if !ok {
continue continue pathLoop
} }
switch call.Func.Name { switch call.Func.Name {
case "histogram_count", "histogram_sum", "histogram_avg": case "histogram_count", "histogram_sum", "histogram_avg":
// We allow skipping buckets preliminarily. But
// we will continue through the path to see if
// we find a subquery (or a histogram function)
// further up (the latter wouldn't make sense,
// but no harm in detecting it).
n.SkipHistogramBuckets = true n.SkipHistogramBuckets = true
case "histogram_quantile", "histogram_fraction": case "histogram_quantile", "histogram_fraction":
// If we ever see a function that needs the
// whole histogram, we will not skip the
// buckets.
n.SkipHistogramBuckets = false n.SkipHistogramBuckets = false
default: break pathLoop
continue
} }
break
} }
return errors.New("stop") return errors.New("stop")
}) })