Skip to content

fix(promql): histogram_fraction first bucket heuristic consistent with histogram_quantile#18474

Draft
krajorama wants to merge 2 commits intomainfrom
fix-histogram-fraction-first-bucket
Draft

fix(promql): histogram_fraction first bucket heuristic consistent with histogram_quantile#18474
krajorama wants to merge 2 commits intomainfrom
fix-histogram-fraction-first-bucket

Conversation

@krajorama
Copy link
Copy Markdown
Member

@krajorama krajorama commented Apr 7, 2026

Summary

For classic histograms and NHCB (custom buckets) where the first bucket has a non-positive upper boundary (-Inf, x] with x<=0, both BucketQuantile and HistogramQuantile place all observations at the upper boundary x. BucketFraction and HistogramFraction were inconsistent: they treated observations as being at -Inf (contributing to every fraction query whose lower bound fell inside the bucket).

The upshot is that if there's a bucket (-Inf, x] where x<=0, for example (-Inf, -3] and there are 10 observations in it out of 100 observations total, then the 10% quantile is -3, since quantile assumes that observations are at the upper boundary. However without the fix the histogram_fraction treats them as being at -Inf, meaning that the fraction from for example -4 to -3 is zero. Which is a contradiction with quantile at -3.

  • Fix BucketFraction (classic histograms) to match BucketQuantile: observations in the first bucket with upperBound <= 0 are assumed to be at the upper boundary.
  • Fix HistogramFraction (native histograms) to match HistogramQuantile: same heuristic for the first NHCB bucket with b.Upper <= 0.
  • Observations in the first NHCB bucket with a positive upper boundary retain the existing behaviour (assume lower boundary of 0).
  • Add missing test cases for histogram_fraction on NHCB histograms with non-positive first bucket upper boundaries.
  • Add cross-consistency checks between histogram_quantile and histogram_fraction for exponential native histograms.

Test plan

  • go test ./promql/... passes
  • New tests in histograms.test cover classic histogram first bucket with non-positive upper boundary
  • New tests in native_histograms.test cover NHCB first bucket with non-positive upper boundary
  • Consistency cross-checks added for exponential native histograms (single_histogram, negative_histogram)
[BUGFIX] PromQL: `histogram_fraction` first-bucket heuristic is now consistent with `histogram_quantile`: observations in the first bucket with a non-positive upper boundary are assumed to be at the upper boundary, not at -Inf.

Coded with Claude Sonnet 4.6.

Signed-off-by: György Krajcsovits [email protected]

…h histogram_quantile

For classic histograms and NHCB (custom buckets) where the first bucket
has a non-positive upper boundary (-Inf, x] with x<=0, both
BucketQuantile and HistogramQuantile place all observations at the upper
boundary x. BucketFraction and HistogramFraction were inconsistent: they
treated observations as being at -Inf (contributing to every fraction
query whose lower bound fell inside the bucket).

Fix BucketFraction and HistogramFraction to use the same heuristic as
their quantile counterparts: observations in the first bucket with a
non-positive upper boundary are assumed to be at x (the upper boundary).
Observations in the first NHCB bucket with a positive upper boundary
retain the existing behaviour of assuming a lower boundary of 0.

Add missing test cases for histogram_fraction on NHCB histograms with
non-positive first bucket boundaries, and add cross-checks between
histogram_quantile and histogram_fraction for exponential native
histograms.

Coded with Claude Sonnet 4.6.

Signed-off-by: György Krajcsovits <[email protected]>
@krajorama krajorama requested review from a team and roidelapluie as code owners April 7, 2026 14:17
@krajorama krajorama requested a review from beorn7 April 7, 2026 15:50
@krajorama krajorama marked this pull request as draft April 7, 2026 15:51
@krajorama
Copy link
Copy Markdown
Member Author

First version made with AI. I'm refining it and discussing the merits with Beorn.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant