Skip to content

model/histogram: preserve integer cumulative counts in classic conversion - #20330

Merged
bwplotka merged 2 commits into
mainfrom
perfloop-pr-open-wamhyp0601
Nov 30, 2026
Merged

bwplotka merged 2 commits into
mainfrom
perfloop-pr-open-wamhyp0601

Conversation

@perfloop

@perfloop perfloop Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

The NHCB-as-classic queries added in #18048 can lose small bucket increments at large integer counts, leaving +Inf below _count. Accumulate integer prefixes before rounding each classic bucket value to avoid this intermediate precision loss.

Which issue(s) does the PR fix:

Related to #16948.

ClassicBucketCounts now accumulates integer bucket prefixes in uint64. Empty buckets retain the preceding cumulative count, including across sparse spans and after the last populated bucket. The shared helper serves both PromQL compatibility queries and remote-write conversion; floating-histogram summation, validation, labels, count and sum are unchanged.

The precision and integer-boundary cases are in model/histogram/convert_test.go, alongside the existing conversion tests. They check dense and sparse layouts through MaxUint64, floating-histogram controls, and prefilled output buffers. An empty bucket between the small increments exercises an inter-span gap while the exact prefix is odd.

Go tests passed for histogram conversion and precision, classic storage selectors, remote-write conversion, and PromQL compatibility. Scoped Go lint passed.

Prefixes not representable as float64 still round. PromQL conversion still requires promql-native-histograms-as-classic to be enabled.

Related work: #19881 and #19886 also change the compatibility conversion path.

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

[BUGFIX] Histograms: Preserve integer cumulative bucket counts when converting native histograms with custom buckets to classic histograms.

Case and evidence


Made by Perfloop.

Comment thread promql/engine_test.go Outdated
}
}

func TestEngine_NativeHistogramsAsClassicIntegerPrecision(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the fix is entirely in histogram.ClassicBucketCounts in model/histogram/convert.go, could we move these precision and uint64 boundary test cases into model/histogram/convert_test.go (where ClassicBucketCounts and dirty buf reuse are already unit-tested) instead of spinning up a TSDB and PromQL engine per subtest here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the precision and uint64 boundary cases to model/histogram/convert_test.go. They now call ClassicBucketCounts directly with prefilled buffers and floating-histogram controls, without constructing a TSDB or PromQL engine per subtest.

Comment thread promql/engine_test.go Outdated
{"empty", []int64{0, 0, 0}},
{"small", []int64{0, 10, 0, 1, 0}},
{"below_float_precision", []int64{1<<53 - 1, 1, 1, 1, 0}},
{"at_float_precision", []int64{1 << 53, 1, 1, 0}},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the 1<<53 cases all non-zero counts are contiguous, so sparse=true only trims the trailing +Inf bucket and never exercises an inter-span s.Offset > 0 gap while holding an odd prefix like 1<<53 + 1. Worth putting an empty bucket between the +1 increments here too:

Suggested change
{"at_float_precision", []int64{1 << 53, 1, 1, 0}},
{"at_float_precision", []int64{1 << 53, 1, 0, 1, 0}},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the suggested empty bucket between the +1 increments. The sparse case now crosses an inter-span gap with an exact prefix of 1<<53 + 1 before the next increment.

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-wamhyp0601 branch from 964ce65 to b9facde Compare November 13, 2026 20:57

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@saswatamcode saswatamcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good afaict

@bwplotka
bwplotka merged commit a5d89a8 into main Nov 30, 2026
40 checks passed
@bwplotka
bwplotka deleted the perfloop-pr-open-wamhyp0601 branch November 30, 2026 10:14
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.

3 participants