Skip to content

Add SampleQuantiles trait and Quantile type for histogram quantile queries#5

Merged
thinkingfish merged 8 commits intomainfrom
claude/histogram-query-analysis-ia2Vw
Apr 6, 2026
Merged

Add SampleQuantiles trait and Quantile type for histogram quantile queries#5
thinkingfish merged 8 commits intomainfrom
claude/histogram-query-analysis-ia2Vw

Conversation

@thinkingfish
Copy link
Copy Markdown
Member

@thinkingfish thinkingfish commented Apr 4, 2026

Summary

This PR introduces a new SampleQuantiles trait and Quantile type to provide a more robust and ergonomic API for computing quantiles from histograms. The old percentiles() and percentile() methods are deprecated in favor of the new trait-based approach.

Key Changes

  • New Quantile type (src/quantile.rs): A validated newtype wrapper for quantile values in the range [0.0, 1.0]. Ensures only valid quantiles (finite, non-NaN, within range) are used, enabling safe implementations of Eq and Ord traits.

  • New QuantilesResult struct: Replaces the simple Vec<(f64, Bucket)> return type with a richer result type that includes:

    • Quantile-to-bucket mappings stored in a sorted BTreeMap<Quantile, Bucket>
    • Total sample count
    • Min and max buckets (p0 and p100)
    • Convenient accessor methods
  • New SampleQuantiles trait: Provides a unified interface for computing quantiles across different histogram types:

    • quantiles(&self, quantiles: &[f64]) -> Result<Option<QuantilesResult>, Error>
    • quantile(&self, quantile: f64) -> Result<Option<QuantilesResult>, Error> (convenience method)
    • Implemented for Histogram and SparseHistogram
  • Deprecated old API: The percentiles() and percentile() methods on Histogram and SparseHistogram are now deprecated with a note to use the SampleQuantiles trait instead. They are reimplemented as thin wrappers around the new trait methods for backward compatibility.

  • Implementation improvements:

    • Automatic deduplication of duplicate quantile requests via BTreeMap
    • Single-pass algorithm for finding all quantiles (no regression in performance)
    • Consistent behavior between Histogram and SparseHistogram

Notable Implementation Details

  • The Quantile type uses to_bits() comparison for equality to safely handle the validated f64 values
  • Both Histogram and SparseHistogram implementations use the same quantile-finding algorithm with a single pass through buckets
  • The trait is not implemented for AtomicHistogram (users should call load() first to get a snapshot)
  • Comprehensive test coverage including validation, ordering, empty histograms, and agreement between histogram types

https://claude.ai/code/session_01YQAY65y5zF3PdR2uuaypMj

claude added 3 commits April 4, 2026 05:13
Implements histogram#4: adds a shared SampleQuantiles trait for
Histogram and SparseHistogram with richer quantile query results.

- New Quantile newtype (validated f64 in 0.0..=1.0, implements Ord/Eq)
- QuantilesResult with BTreeMap<Quantile, Bucket>, total_count, min, max
- SampleQuantiles trait with quantiles() and quantile() methods
- Deprecate old percentiles()/percentile() methods (delegate to new API)
- Bump version to 1.1.0

https://claude.ai/code/session_01YQAY65y5zF3PdR2uuaypMj
New code uses Error::InvalidQuantile. Deprecated percentiles()/percentile()
wrappers map it back to InvalidPercentile for backwards compatibility.

https://claude.ai/code/session_01YQAY65y5zF3PdR2uuaypMj
@thinkingfish thinkingfish requested a review from brayniac April 4, 2026 05:31
claude added 5 commits April 4, 2026 07:22
Replace deprecated percentile()/percentiles() calls with
quantile()/quantiles() and use Quantile newtype for lookups.

https://claude.ai/code/session_01YQAY65y5zF3PdR2uuaypMj
Feature PRs use <version>-alpha.<revision> format only.
Release version bumps are reserved for release PRs.

https://claude.ai/code/session_01YQAY65y5zF3PdR2uuaypMj
@thinkingfish thinkingfish merged commit 2d0ca41 into main Apr 6, 2026
6 checks passed
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.

2 participants