notfilippo commented on code in PR #400: URL: https://github.com/apache/datasketches-rust/pull/400#discussion_r4659200657
########## datasketches/src/ddsketch/sketch.rs: ########## @@ -0,0 +1,977 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt; + +use super::DEFAULT_RELATIVE_ACCURACY; +use super::iter::DdSketchIter; +use super::mapping::IndexMapping; +use super::mapping::MappingKind; +use super::serialization::deserialize_compat_sketch; +use super::serialization::deserialize_sketch; +use super::serialization::serialize_compat_sketch; +use super::serialization::serialize_sketch; +use super::store::Store; +use super::store::StoreKind; +use super::store::validate_store_kind; +use crate::error::Error; + +/// Builder for configuring and constructing a [`DdSketch`]. +/// +/// Defaults to `relative_accuracy = 0.01`, [`MappingKind::Cubic`], and +/// [`StoreKind::CollapsingLowestDense`] with `2048` bins per sign. +/// +/// # Examples +/// +/// ``` +/// use datasketches::ddsketch::DdSketchBuilder; +/// use datasketches::ddsketch::MappingKind; +/// use datasketches::ddsketch::StoreKind; +/// +/// let mut sketch = DdSketchBuilder::new(0.005) +/// .mapping(MappingKind::Logarithmic) +/// .store(StoreKind::Sparse) +/// .build()?; +/// sketch.update(42.0); +/// assert_eq!(sketch.total_weight(), 1); +/// # Ok::<(), datasketches::error::Error>(()) +/// ``` +#[derive(Debug, Clone)] +pub struct DdSketchBuilder { + relative_accuracy: f64, + mapping_kind: MappingKind, + store_kind: StoreKind, +} + +impl Default for DdSketchBuilder { + fn default() -> Self { + Self::new(DEFAULT_RELATIVE_ACCURACY) + } +} + +impl DdSketchBuilder { + /// Creates a builder with the given `relative_accuracy` and default mapping and store settings. + pub fn new(relative_accuracy: f64) -> Self { + Self { + relative_accuracy, + mapping_kind: MappingKind::default(), + store_kind: StoreKind::default(), + } + } + + /// Sets the index mapping curve used to assign positive magnitudes to bucket indices. + #[inline] + pub fn mapping(mut self, mapping_kind: MappingKind) -> Self { + self.mapping_kind = mapping_kind; + self + } + + /// Sets the bin storage strategy used for both positive and negative stores. + #[inline] + pub fn store(mut self, store_kind: StoreKind) -> Self { + self.store_kind = store_kind; + self + } + + /// Validates configuration parameters and creates an empty [`DdSketch`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`, or if `store` specifies a collapsing + /// dense store with `max_num_bins` outside `1..=67_108_864`. + pub fn build(self) -> Result<DdSketch, Error> { + validate_store_kind(self.store_kind)?; + let mapping = IndexMapping::from_accuracy(self.mapping_kind, self.relative_accuracy)?; + let negative = Store::new(self.store_kind)?; + let positive = Store::new(self.store_kind)?; + Ok(DdSketch::from_parts( + mapping, + negative, + positive, + 0, + 0, + SummaryState::Empty, + )) + } +} + +#[derive(Debug, Clone, Copy)] +pub(super) enum SummaryState { + Empty, + Active { min: f64, max: f64, sum: f64 }, + Missing, +} + +impl SummaryState { + #[inline] + fn canonical_f64(value: f64) -> f64 { + if value == 0.0 { 0.0 } else { value } + } + + #[inline] + fn record(&mut self, value: f64, weight: u64) { + let v = Self::canonical_f64(value); + let weighted = Self::canonical_f64(v * (weight as f64)); + match self { + Self::Empty => { + *self = Self::Active { + min: v, + max: v, + sum: weighted, + }; + } + Self::Active { min, max, sum } => { + *min = (*min).min(v); + *max = (*max).max(v); + *sum = Self::canonical_f64(*sum + weighted); + } + Self::Missing => {} + } + } + + #[inline] + fn merge_non_empty(&mut self, other: Self) { + *self = match (*self, other) { + (Self::Empty, rhs) => rhs, + ( + Self::Active { min, max, sum }, + Self::Active { + min: omin, + max: omax, + sum: osum, + }, + ) => Self::Active { + min: min.min(omin), + max: max.max(omax), + sum: Self::canonical_f64(sum + osum), + }, + (Self::Missing, _) | (_, Self::Missing) => Self::Missing, + (lhs, Self::Empty) => lhs, + }; + } +} + +/// Mergeable quantile sketch over finite `f64` values with relative-value error guarantees. +/// +/// See the [module documentation](super) for accuracy, memory, and compatibility details. +#[derive(Clone)] +pub struct DdSketch { + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, +} + +impl fmt::Debug for DdSketch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DdSketch") + .field("relative_accuracy", &self.mapping.relative_accuracy()) + .field("mapping_kind", &self.mapping.kind()) + .field("store_kind", &self.positive.kind()) + .field("is_empty", &self.is_empty()) + .field("total_weight", &self.total_weight) + .field("zero_weight", &self.zero_weight) + .field("num_retained", &self.num_retained()) + .field("min_value", &self.min_value()) + .field("max_value", &self.max_value()) + .field("sum", &self.sum()) + .finish() + } +} + +impl Default for DdSketch { + fn default() -> Self { + DdSketchBuilder::default() + .build() + .expect("default DDSketch configuration is valid") + } +} + +impl DdSketch { + /// Creates an empty sketch with the given `relative_accuracy`, [`MappingKind::Cubic`], and + /// [`StoreKind::default()`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let sketch = DdSketch::new(0.01)?; + /// assert!(sketch.is_empty()); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn new(relative_accuracy: f64) -> Result<Self, Error> { + DdSketchBuilder::new(relative_accuracy).build() + } + + pub(super) fn from_parts( + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, + ) -> Self { + debug_assert_eq!( + negative.total_weight() + zero_weight + positive.total_weight(), + total_weight + ); + debug_assert_eq!(total_weight == 0, matches!(summary, SummaryState::Empty)); + Self { + mapping, + negative, + positive, + zero_weight, + total_weight, + summary, + } + } + + #[inline] + pub(super) const fn summary_state(&self) -> SummaryState { + self.summary + } + + #[inline] + pub(super) const fn mapping(&self) -> IndexMapping { + self.mapping + } + + #[inline] + pub(super) const fn positive_store(&self) -> &Store { + &self.positive + } + + #[inline] + pub(super) const fn negative_store(&self) -> &Store { + &self.negative + } + + /// Returns the relative value accuracy guaranteed for non-collapsed buckets. + #[inline] + pub fn relative_accuracy(&self) -> f64 { + self.mapping.relative_accuracy() + } + + /// Returns the index mapping curve used by this sketch. + #[inline] + pub fn mapping_kind(&self) -> MappingKind { + self.mapping.kind() + } + + /// Returns the bin storage strategy used by this sketch. + #[inline] + pub fn store_kind(&self) -> StoreKind { + self.positive.kind() + } + + /// Returns the magnitude threshold below or at which inputs map to the zero bucket. + /// + /// Any finite input `x` with `|x| <= self.zero_threshold()` increments + /// [`zero_weight`](Self::zero_weight) and is approximated by `0.0`. + #[inline] + pub fn zero_threshold(&self) -> f64 { + self.mapping.min_indexable_value() + } + + /// Returns `true` if this sketch has not recorded any finite observations. + #[inline] + pub fn is_empty(&self) -> bool { + self.total_weight == 0 + } + + /// Returns the accumulated stream weight across all buckets. + #[inline] + pub fn total_weight(&self) -> u64 { + self.total_weight + } + + /// Returns the accumulated stream weight in the zero bucket. + #[inline] + pub fn zero_weight(&self) -> u64 { + self.zero_weight + } + + /// Returns the number of non-empty buckets currently retained by this sketch, including the + /// zero bucket when [`zero_weight`](Self::zero_weight) is non-zero. + #[inline] + pub fn num_retained(&self) -> usize { + self.negative.num_occupied() + + usize::from(self.zero_weight != 0) + + self.positive.num_occupied() + } + + /// Returns an implementation-dependent estimate of heap memory usage in bytes. + pub fn estimated_size(&self) -> usize { + self.negative + .estimated_size() + .saturating_add(self.positive.estimated_size()) + } + + /// Updates this sketch with a single observation. + /// + /// [`f64::NAN`], [`f64::INFINITY`], and [`f64::NEG_INFINITY`] values are ignored. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update(10.0); + /// assert_eq!(sketch.total_weight(), 1); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update(&mut self, value: f64) { + self.update_with_weight(value, 1); + } + + /// Updates this sketch with `value` repeated `weight` times. + /// + /// [`f64::NAN`], [`f64::INFINITY`], [`f64::NEG_INFINITY`], and a `weight` of `0` are no-ops. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Cross-language compatibility + /// + /// In the reference DDSketch implementations, Go rejects `NaN`, infinite, and negative-count + /// inputs with an error, whereas Java rejects out-of-range finite/infinite and negative-count + /// inputs while counting `NaN` into the zero bucket; both accept fractional `f64` counts. This + /// method ignores non-finite values and accumulates exact `u64` integer weights; keep + /// per-bucket weights (and [`total_weight`](Self::total_weight) for Java quantile readers) at + /// or below `2^53 - 1` when serializing with [`serialize_compat`](Self::serialize_compat). + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update_with_weight(25.0, 100); + /// assert_eq!(sketch.total_weight(), 100); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update_with_weight(&mut self, value: f64, weight: u64) { + if !value.is_finite() || weight == 0 { + return; + } + let next_total = self + .total_weight + .checked_add(weight) + .expect("total stream weight overflow"); + + let min_indexable = self.mapping.min_indexable_value(); + if value > min_indexable { + let idx = self.mapping.index(value); + self.positive.ensure_index_capacity(idx); + self.positive.add(idx, weight); + } else if value < -min_indexable { + let idx = self.mapping.index(-value); + self.negative.ensure_index_capacity(idx); + self.negative.add(idx, weight); + } else { + self.zero_weight += weight; + } + self.total_weight = next_total; + self.summary.record(value, weight); + } + + /// Merges `other` into `self`, retaining `self`'s [`StoreKind`]. + /// + /// Sketches that share an equivalent index mapping can be merged even when they use different + /// [`StoreKind`] strategies. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) without modifying + /// `self` if `other` uses an incompatible index mapping (even when either sketch is empty), if + /// the combined [`total_weight`](Self::total_weight) would exceed [`u64::MAX`], or if merging + /// into [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut left = DdSketch::new(0.01)?; + /// let mut right = DdSketch::new(0.01)?; + /// left.update(1.0); + /// right.update(100.0); + /// left.merge(&right)?; + /// assert_eq!(left.total_weight(), 2); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn merge(&mut self, other: &Self) -> Result<(), Error> { + if !self.mapping.is_compatible(&other.mapping) { + return Err(Error::invalid_argument(format!( + "cannot merge DDSketches with incompatible index mappings: expected ({:?}, gamma={}, offset={}), got ({:?}, gamma={}, offset={})", + self.mapping.kind(), + self.mapping.gamma(), + self.mapping.index_offset(), + other.mapping.kind(), + other.mapping.gamma(), + other.mapping.index_offset(), + ))); + } + + if other.is_empty() { + return Ok(()); + } + + let next_total = self + .total_weight + .checked_add(other.total_weight) + .ok_or_else(|| { + Error::invalid_argument(format!( + "combined stream weight exceeds {}: left {}, right {}", + u64::MAX, + self.total_weight, + other.total_weight + )) + })?; + + self.negative.validate_merge_capacity(&other.negative)?; + self.positive.validate_merge_capacity(&other.positive)?; + + self.negative.merge_from(&other.negative); + self.positive.merge_from(&other.positive); + self.zero_weight += other.zero_weight; + self.total_weight = next_total; + self.summary.merge_non_empty(other.summary); + Ok(()) + } + + /// Resets this sketch to its empty state while retaining its configuration and allocated + /// dense-store capacity. + pub fn reset(&mut self) { + self.negative.reset(); + self.positive.reset(); + self.zero_weight = 0; + self.total_weight = 0; + self.summary = SummaryState::Empty; + } + + /// Returns the exact minimum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn min_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { min, .. } => Some(min), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact maximum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn max_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { max, .. } => Some(max), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact sum of recorded values, or `None` if this sketch is empty or was decoded + /// from a compatibility image without exact summary statistics. + /// + /// Accumulated with standard IEEE 754 `f64` addition (`value * weight as f64`). + #[inline] + pub fn sum(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { sum, .. } => Some(sum), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the approximate lower discrete quantile for `rank` in `[0.0, 1.0]`, or `Ok(None)` + /// if this sketch is empty. + /// + /// Selects the bucket containing zero-based sorted item `floor(rank * (total_weight - 1))` + /// using the standard `f64` product when `total_weight <= 2^53` (matching the Java reference + /// implementation) and exact integer significand scaling when `total_weight > 2^53`. When exact + /// summary statistics are present, `rank == 0.0` returns [`min_value`](Self::min_value), + /// `rank == 1.0` returns [`max_value`](Self::max_value), and interior estimates are clamped to + /// `[min_value, max_value]`. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if `rank` is NaN or + /// outside `[0.0, 1.0]`, even if this sketch is empty. + /// + /// # Cross-language compatibility + /// + /// Discrete rank scaling matches `sketches-java` (`floor(rank * (total_weight - 1) as f64)`) + /// when `total_weight <= 2^53`. On negative buckets, `sketches-go` mirrors the floating-point + /// rank (`KeyAtRank(negative_weight - 1 - rank * (total_weight - 1))`), acting as an upper + /// quantile on the negative store and differing on any fractional negative-side rank (such as + /// `quantile(0.5)` on `[-100.0, -1.0]`). Sketches decoded via + /// [`deserialize_compat`](Self::deserialize_compat) from bare `DDSketch` images without exact + /// summary blocks return unclamped bucket estimates. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// for value in 1..=100 { + /// sketch.update(f64::from(value)); + /// } + /// let p95 = sketch.quantile(0.95)?.unwrap(); + /// assert!((p95 - 95.0).abs() <= 0.01 * 95.0); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn quantile(&self, rank: f64) -> Result<Option<f64>, Error> { + check_rank(rank)?; + if self.is_empty() { + return Ok(None); + } + Ok(Some(self.quantile_unchecked(rank))) + } + + /// Returns approximate quantiles for `ranks` in input order, or `Ok(None)` if this sketch is + /// empty. + /// + /// For a non-empty sketch, an empty `ranks` slice returns `Ok(Some(vec![]))`. Multiple ranks + /// are answered in a single pass over the retained buckets regardless of input order. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if any rank is NaN or + /// outside `[0.0, 1.0]`, even if this sketch is empty. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// for value in 1..=1000 { + /// sketch.update(f64::from(value)); + /// } + /// let qs = sketch.quantiles(&[0.5, 0.9, 0.99])?.unwrap(); + /// assert_eq!(qs.len(), 3); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn quantiles(&self, ranks: &[f64]) -> Result<Option<Vec<f64>>, Error> { + for &rank in ranks { + check_rank(rank)?; + } + if self.is_empty() { + return Ok(None); + } + if ranks.is_empty() { + return Ok(Some(Vec::new())); + } + if ranks.len() == 1 { + return Ok(Some(vec![self.quantile_unchecked(ranks[0])])); + } + if let SummaryState::Active { min, max, .. } = self.summary { + if min == max { + return Ok(Some(vec![min; ranks.len()])); + } + } + + let last = self.total_weight - 1; + let is_non_decreasing = ranks.windows(2).all(|pair| pair[0] <= pair[1]); + let mut out = vec![0.0; ranks.len()]; + if is_non_decreasing { + self.resolve_non_decreasing_ranks(ranks, last, &mut out); + } else { + let mut indexed: Vec<(u64, usize)> = ranks + .iter() + .enumerate() + .map(|(pos, &rank)| (quantile_target_index(rank, last), pos)) + .collect(); + indexed.sort_unstable_by_key(|&(target, _)| target); + self.resolve_sorted_targets(&indexed, &mut out); + } + + if let SummaryState::Active { min, max, .. } = self.summary { + for (&rank, estimate) in ranks.iter().zip(&mut out) { + if rank == 0.0 { + *estimate = min; + } else if rank == 1.0 { + *estimate = max; + } else { + *estimate = estimate.clamp(min, max); + } + } + } + Ok(Some(out)) + } + + /// Returns an iterator over non-empty `(value, weight)` buckets in ascending numerical order. + #[inline] + pub fn iter(&self) -> DdSketchIter<'_> { + DdSketchIter::new( + self.mapping, + self.negative.iter_asc(), + self.zero_weight, + self.positive.iter_asc(), + self.num_retained(), + ) + } + + /// Serializes this sketch in this crate's native `DDSKETCH` binary format, preserving + /// [`MappingKind`], [`StoreKind`], collapse state, exact summary statistics, and exact `u64` + /// bucket weights. + /// + /// Note: the native `Family::DDSKETCH` (`id = 25`) wire format is experimental and may change + /// in future releases while cross-language DataSketches standardization is underway. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update(3.5); + /// let bytes = sketch.serialize(); + /// let decoded = DdSketch::deserialize(&bytes)?; + /// assert_eq!(decoded.total_weight(), 1); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn serialize(&self) -> Vec<u8> { + serialize_sketch(self) + } + + /// Deserializes a sketch from this crate's native `DDSKETCH` binary format, restoring its + /// encoded [`StoreKind`] and exact summary statistics (when present). + /// + /// Note: the native `Family::DDSKETCH` (`id = 25`) wire format is experimental and may change + /// in future releases while cross-language DataSketches standardization is underway. Use + /// [`deserialize_compat`](Self::deserialize_compat) to decode compact flag-block images + /// produced by the Java and Go reference DDSketch implementations. + /// + /// # Errors + /// + /// Returns [`InvalidData`](crate::error::ErrorKind::InvalidData) if the image is empty, + /// truncated, has an unsupported family, version, or flag, or violates index, store, or weight + /// bounds. + pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> { + deserialize_sketch(bytes) + } + + /// Serializes this sketch in the compact flag-block binary format of the [Java][java-ref] and + /// [Go][go-ref] reference DDSketch implementations (distinct from the Protobuf format used by + /// Python and JavaScript). + /// + /// [java-ref]: https://github.com/DataDog/sketches-java + /// [go-ref]: https://github.com/DataDog/sketches-go + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// [`zero_weight`](Self::zero_weight), any retained bucket weight, or (when exact summary + /// statistics are present) [`total_weight`](Self::total_weight) exceeds `2^53 - 1` + /// (`9_007_199_254_740_991`), the maximum integer representable without rounding in the + /// compatibility format's `v + 1.0` floating-point counter encoding. + /// + /// # Cross-language compatibility + /// + /// When exact summary statistics are present, emits `count`, `min`, `max`, and non-zero `sum` + /// feature blocks compatible with `DDSketchWithExactSummaryStatistics` in Java and Go (bare + /// `DDSketch` decoders safely skip those blocks). [`StoreKind`] and collapse state are Review Comment: external-java's bare decoder skips these blocks, but external-go's doesn't. In sketches-go, `DecodeDDSketch` skips 8 bytes for every summary flag, but the count is a varfloat, so decoding gets out of sync. It fails even on Go's own exact-summary images, and this is still the case on `main`: ```text DecodeDDSketch(Rust serialize_compat bytes) -> unknown encoding flag DecodeDDSketch(Go exact-summary sketch bytes) -> unknown mapping ``` Every sketch built here now carries a summary, so external-go readers need `DecodeDDSketchWithExactSummaryStatistics`. That reader works well: it decoded all 30 of my exact-summary fixtures after a Rust round trip, with the same count, min, max and sum and bit-identical quantiles. Could the docs say that instead? ########## datasketches/src/ddsketch/sketch.rs: ########## @@ -0,0 +1,977 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt; + +use super::DEFAULT_RELATIVE_ACCURACY; +use super::iter::DdSketchIter; +use super::mapping::IndexMapping; +use super::mapping::MappingKind; +use super::serialization::deserialize_compat_sketch; +use super::serialization::deserialize_sketch; +use super::serialization::serialize_compat_sketch; +use super::serialization::serialize_sketch; +use super::store::Store; +use super::store::StoreKind; +use super::store::validate_store_kind; +use crate::error::Error; + +/// Builder for configuring and constructing a [`DdSketch`]. +/// +/// Defaults to `relative_accuracy = 0.01`, [`MappingKind::Cubic`], and +/// [`StoreKind::CollapsingLowestDense`] with `2048` bins per sign. +/// +/// # Examples +/// +/// ``` +/// use datasketches::ddsketch::DdSketchBuilder; +/// use datasketches::ddsketch::MappingKind; +/// use datasketches::ddsketch::StoreKind; +/// +/// let mut sketch = DdSketchBuilder::new(0.005) +/// .mapping(MappingKind::Logarithmic) +/// .store(StoreKind::Sparse) +/// .build()?; +/// sketch.update(42.0); +/// assert_eq!(sketch.total_weight(), 1); +/// # Ok::<(), datasketches::error::Error>(()) +/// ``` +#[derive(Debug, Clone)] +pub struct DdSketchBuilder { + relative_accuracy: f64, + mapping_kind: MappingKind, + store_kind: StoreKind, +} + +impl Default for DdSketchBuilder { + fn default() -> Self { + Self::new(DEFAULT_RELATIVE_ACCURACY) + } +} + +impl DdSketchBuilder { + /// Creates a builder with the given `relative_accuracy` and default mapping and store settings. + pub fn new(relative_accuracy: f64) -> Self { + Self { + relative_accuracy, + mapping_kind: MappingKind::default(), + store_kind: StoreKind::default(), + } + } + + /// Sets the index mapping curve used to assign positive magnitudes to bucket indices. + #[inline] + pub fn mapping(mut self, mapping_kind: MappingKind) -> Self { + self.mapping_kind = mapping_kind; + self + } + + /// Sets the bin storage strategy used for both positive and negative stores. + #[inline] + pub fn store(mut self, store_kind: StoreKind) -> Self { + self.store_kind = store_kind; + self + } + + /// Validates configuration parameters and creates an empty [`DdSketch`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`, or if `store` specifies a collapsing + /// dense store with `max_num_bins` outside `1..=67_108_864`. + pub fn build(self) -> Result<DdSketch, Error> { + validate_store_kind(self.store_kind)?; + let mapping = IndexMapping::from_accuracy(self.mapping_kind, self.relative_accuracy)?; + let negative = Store::new(self.store_kind)?; + let positive = Store::new(self.store_kind)?; + Ok(DdSketch::from_parts( + mapping, + negative, + positive, + 0, + 0, + SummaryState::Empty, + )) + } +} + +#[derive(Debug, Clone, Copy)] +pub(super) enum SummaryState { + Empty, + Active { min: f64, max: f64, sum: f64 }, + Missing, +} + +impl SummaryState { + #[inline] + fn canonical_f64(value: f64) -> f64 { + if value == 0.0 { 0.0 } else { value } + } + + #[inline] + fn record(&mut self, value: f64, weight: u64) { + let v = Self::canonical_f64(value); + let weighted = Self::canonical_f64(v * (weight as f64)); + match self { + Self::Empty => { + *self = Self::Active { + min: v, + max: v, + sum: weighted, + }; + } + Self::Active { min, max, sum } => { + *min = (*min).min(v); + *max = (*max).max(v); + *sum = Self::canonical_f64(*sum + weighted); + } + Self::Missing => {} + } + } + + #[inline] + fn merge_non_empty(&mut self, other: Self) { + *self = match (*self, other) { + (Self::Empty, rhs) => rhs, + ( + Self::Active { min, max, sum }, + Self::Active { + min: omin, + max: omax, + sum: osum, + }, + ) => Self::Active { + min: min.min(omin), + max: max.max(omax), + sum: Self::canonical_f64(sum + osum), + }, + (Self::Missing, _) | (_, Self::Missing) => Self::Missing, + (lhs, Self::Empty) => lhs, + }; + } +} + +/// Mergeable quantile sketch over finite `f64` values with relative-value error guarantees. +/// +/// See the [module documentation](super) for accuracy, memory, and compatibility details. +#[derive(Clone)] +pub struct DdSketch { + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, +} + +impl fmt::Debug for DdSketch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DdSketch") + .field("relative_accuracy", &self.mapping.relative_accuracy()) + .field("mapping_kind", &self.mapping.kind()) + .field("store_kind", &self.positive.kind()) + .field("is_empty", &self.is_empty()) + .field("total_weight", &self.total_weight) + .field("zero_weight", &self.zero_weight) + .field("num_retained", &self.num_retained()) + .field("min_value", &self.min_value()) + .field("max_value", &self.max_value()) + .field("sum", &self.sum()) + .finish() + } +} + +impl Default for DdSketch { + fn default() -> Self { + DdSketchBuilder::default() + .build() + .expect("default DDSketch configuration is valid") + } +} + +impl DdSketch { + /// Creates an empty sketch with the given `relative_accuracy`, [`MappingKind::Cubic`], and + /// [`StoreKind::default()`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let sketch = DdSketch::new(0.01)?; + /// assert!(sketch.is_empty()); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn new(relative_accuracy: f64) -> Result<Self, Error> { + DdSketchBuilder::new(relative_accuracy).build() + } + + pub(super) fn from_parts( + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, + ) -> Self { + debug_assert_eq!( + negative.total_weight() + zero_weight + positive.total_weight(), + total_weight + ); + debug_assert_eq!(total_weight == 0, matches!(summary, SummaryState::Empty)); + Self { + mapping, + negative, + positive, + zero_weight, + total_weight, + summary, + } + } + + #[inline] + pub(super) const fn summary_state(&self) -> SummaryState { + self.summary + } + + #[inline] + pub(super) const fn mapping(&self) -> IndexMapping { + self.mapping + } + + #[inline] + pub(super) const fn positive_store(&self) -> &Store { + &self.positive + } + + #[inline] + pub(super) const fn negative_store(&self) -> &Store { + &self.negative + } + + /// Returns the relative value accuracy guaranteed for non-collapsed buckets. + #[inline] + pub fn relative_accuracy(&self) -> f64 { + self.mapping.relative_accuracy() + } + + /// Returns the index mapping curve used by this sketch. + #[inline] + pub fn mapping_kind(&self) -> MappingKind { + self.mapping.kind() + } + + /// Returns the bin storage strategy used by this sketch. + #[inline] + pub fn store_kind(&self) -> StoreKind { + self.positive.kind() + } + + /// Returns the magnitude threshold below or at which inputs map to the zero bucket. + /// + /// Any finite input `x` with `|x| <= self.zero_threshold()` increments + /// [`zero_weight`](Self::zero_weight) and is approximated by `0.0`. + #[inline] + pub fn zero_threshold(&self) -> f64 { + self.mapping.min_indexable_value() + } + + /// Returns `true` if this sketch has not recorded any finite observations. + #[inline] + pub fn is_empty(&self) -> bool { + self.total_weight == 0 + } + + /// Returns the accumulated stream weight across all buckets. + #[inline] + pub fn total_weight(&self) -> u64 { + self.total_weight + } + + /// Returns the accumulated stream weight in the zero bucket. + #[inline] + pub fn zero_weight(&self) -> u64 { + self.zero_weight + } + + /// Returns the number of non-empty buckets currently retained by this sketch, including the + /// zero bucket when [`zero_weight`](Self::zero_weight) is non-zero. + #[inline] + pub fn num_retained(&self) -> usize { + self.negative.num_occupied() + + usize::from(self.zero_weight != 0) + + self.positive.num_occupied() + } + + /// Returns an implementation-dependent estimate of heap memory usage in bytes. + pub fn estimated_size(&self) -> usize { + self.negative + .estimated_size() + .saturating_add(self.positive.estimated_size()) + } + + /// Updates this sketch with a single observation. + /// + /// [`f64::NAN`], [`f64::INFINITY`], and [`f64::NEG_INFINITY`] values are ignored. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update(10.0); + /// assert_eq!(sketch.total_weight(), 1); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update(&mut self, value: f64) { + self.update_with_weight(value, 1); + } + + /// Updates this sketch with `value` repeated `weight` times. + /// + /// [`f64::NAN`], [`f64::INFINITY`], [`f64::NEG_INFINITY`], and a `weight` of `0` are no-ops. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Cross-language compatibility + /// + /// In the reference DDSketch implementations, Go rejects `NaN`, infinite, and negative-count + /// inputs with an error, whereas Java rejects out-of-range finite/infinite and negative-count + /// inputs while counting `NaN` into the zero bucket; both accept fractional `f64` counts. This + /// method ignores non-finite values and accumulates exact `u64` integer weights; keep + /// per-bucket weights (and [`total_weight`](Self::total_weight) for Java quantile readers) at + /// or below `2^53 - 1` when serializing with [`serialize_compat`](Self::serialize_compat). + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update_with_weight(25.0, 100); + /// assert_eq!(sketch.total_weight(), 100); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update_with_weight(&mut self, value: f64, weight: u64) { + if !value.is_finite() || weight == 0 { + return; + } + let next_total = self + .total_weight + .checked_add(weight) + .expect("total stream weight overflow"); + + let min_indexable = self.mapping.min_indexable_value(); + if value > min_indexable { + let idx = self.mapping.index(value); + self.positive.ensure_index_capacity(idx); + self.positive.add(idx, weight); + } else if value < -min_indexable { + let idx = self.mapping.index(-value); + self.negative.ensure_index_capacity(idx); + self.negative.add(idx, weight); + } else { + self.zero_weight += weight; + } + self.total_weight = next_total; + self.summary.record(value, weight); + } + + /// Merges `other` into `self`, retaining `self`'s [`StoreKind`]. Review Comment: Could the docs mention what happens to the summary here? Merging a sketch without exact summary statistics (decoded from a bare compat image) makes `self` lose its min, max and sum, and quantiles are no longer clamped. Later updates don't bring them back; only `reset` does: ```text new sketch, update(3.0) min_value() == Some(3.0) merge(bare Go image) min_value() == None serialize + deserialize min_value() == None reset, update(3.0) min_value() == Some(3.0) ``` The behavior itself makes sense to me, since the exact min really is unknown. It would also help to say on `min_value`, `max_value` and `sum` that `None` on a non-empty sketch means the summary is unknown. ########## datasketches/src/ddsketch/sketch.rs: ########## @@ -0,0 +1,977 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt; + +use super::DEFAULT_RELATIVE_ACCURACY; +use super::iter::DdSketchIter; +use super::mapping::IndexMapping; +use super::mapping::MappingKind; +use super::serialization::deserialize_compat_sketch; +use super::serialization::deserialize_sketch; +use super::serialization::serialize_compat_sketch; +use super::serialization::serialize_sketch; +use super::store::Store; +use super::store::StoreKind; +use super::store::validate_store_kind; +use crate::error::Error; + +/// Builder for configuring and constructing a [`DdSketch`]. +/// +/// Defaults to `relative_accuracy = 0.01`, [`MappingKind::Cubic`], and +/// [`StoreKind::CollapsingLowestDense`] with `2048` bins per sign. +/// +/// # Examples +/// +/// ``` +/// use datasketches::ddsketch::DdSketchBuilder; +/// use datasketches::ddsketch::MappingKind; +/// use datasketches::ddsketch::StoreKind; +/// +/// let mut sketch = DdSketchBuilder::new(0.005) +/// .mapping(MappingKind::Logarithmic) +/// .store(StoreKind::Sparse) +/// .build()?; +/// sketch.update(42.0); +/// assert_eq!(sketch.total_weight(), 1); +/// # Ok::<(), datasketches::error::Error>(()) +/// ``` +#[derive(Debug, Clone)] +pub struct DdSketchBuilder { + relative_accuracy: f64, + mapping_kind: MappingKind, + store_kind: StoreKind, +} + +impl Default for DdSketchBuilder { + fn default() -> Self { + Self::new(DEFAULT_RELATIVE_ACCURACY) + } +} + +impl DdSketchBuilder { + /// Creates a builder with the given `relative_accuracy` and default mapping and store settings. + pub fn new(relative_accuracy: f64) -> Self { + Self { + relative_accuracy, + mapping_kind: MappingKind::default(), + store_kind: StoreKind::default(), + } + } + + /// Sets the index mapping curve used to assign positive magnitudes to bucket indices. + #[inline] + pub fn mapping(mut self, mapping_kind: MappingKind) -> Self { + self.mapping_kind = mapping_kind; + self + } + + /// Sets the bin storage strategy used for both positive and negative stores. + #[inline] + pub fn store(mut self, store_kind: StoreKind) -> Self { + self.store_kind = store_kind; + self + } + + /// Validates configuration parameters and creates an empty [`DdSketch`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`, or if `store` specifies a collapsing + /// dense store with `max_num_bins` outside `1..=67_108_864`. + pub fn build(self) -> Result<DdSketch, Error> { + validate_store_kind(self.store_kind)?; + let mapping = IndexMapping::from_accuracy(self.mapping_kind, self.relative_accuracy)?; + let negative = Store::new(self.store_kind)?; + let positive = Store::new(self.store_kind)?; + Ok(DdSketch::from_parts( + mapping, + negative, + positive, + 0, + 0, + SummaryState::Empty, + )) + } +} + +#[derive(Debug, Clone, Copy)] +pub(super) enum SummaryState { + Empty, + Active { min: f64, max: f64, sum: f64 }, + Missing, +} + +impl SummaryState { + #[inline] + fn canonical_f64(value: f64) -> f64 { + if value == 0.0 { 0.0 } else { value } + } + + #[inline] + fn record(&mut self, value: f64, weight: u64) { + let v = Self::canonical_f64(value); + let weighted = Self::canonical_f64(v * (weight as f64)); + match self { + Self::Empty => { + *self = Self::Active { + min: v, + max: v, + sum: weighted, + }; + } + Self::Active { min, max, sum } => { + *min = (*min).min(v); + *max = (*max).max(v); + *sum = Self::canonical_f64(*sum + weighted); + } + Self::Missing => {} + } + } + + #[inline] + fn merge_non_empty(&mut self, other: Self) { + *self = match (*self, other) { + (Self::Empty, rhs) => rhs, + ( + Self::Active { min, max, sum }, + Self::Active { + min: omin, + max: omax, + sum: osum, + }, + ) => Self::Active { + min: min.min(omin), + max: max.max(omax), + sum: Self::canonical_f64(sum + osum), + }, + (Self::Missing, _) | (_, Self::Missing) => Self::Missing, + (lhs, Self::Empty) => lhs, + }; + } +} + +/// Mergeable quantile sketch over finite `f64` values with relative-value error guarantees. +/// +/// See the [module documentation](super) for accuracy, memory, and compatibility details. +#[derive(Clone)] +pub struct DdSketch { + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, +} + +impl fmt::Debug for DdSketch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DdSketch") + .field("relative_accuracy", &self.mapping.relative_accuracy()) + .field("mapping_kind", &self.mapping.kind()) + .field("store_kind", &self.positive.kind()) + .field("is_empty", &self.is_empty()) + .field("total_weight", &self.total_weight) + .field("zero_weight", &self.zero_weight) + .field("num_retained", &self.num_retained()) + .field("min_value", &self.min_value()) + .field("max_value", &self.max_value()) + .field("sum", &self.sum()) + .finish() + } +} + +impl Default for DdSketch { + fn default() -> Self { + DdSketchBuilder::default() + .build() + .expect("default DDSketch configuration is valid") + } +} + +impl DdSketch { + /// Creates an empty sketch with the given `relative_accuracy`, [`MappingKind::Cubic`], and + /// [`StoreKind::default()`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let sketch = DdSketch::new(0.01)?; + /// assert!(sketch.is_empty()); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn new(relative_accuracy: f64) -> Result<Self, Error> { + DdSketchBuilder::new(relative_accuracy).build() + } + + pub(super) fn from_parts( + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, + ) -> Self { + debug_assert_eq!( + negative.total_weight() + zero_weight + positive.total_weight(), + total_weight + ); + debug_assert_eq!(total_weight == 0, matches!(summary, SummaryState::Empty)); + Self { + mapping, + negative, + positive, + zero_weight, + total_weight, + summary, + } + } + + #[inline] + pub(super) const fn summary_state(&self) -> SummaryState { + self.summary + } + + #[inline] + pub(super) const fn mapping(&self) -> IndexMapping { + self.mapping + } + + #[inline] + pub(super) const fn positive_store(&self) -> &Store { + &self.positive + } + + #[inline] + pub(super) const fn negative_store(&self) -> &Store { + &self.negative + } + + /// Returns the relative value accuracy guaranteed for non-collapsed buckets. + #[inline] + pub fn relative_accuracy(&self) -> f64 { + self.mapping.relative_accuracy() + } + + /// Returns the index mapping curve used by this sketch. + #[inline] + pub fn mapping_kind(&self) -> MappingKind { + self.mapping.kind() + } + + /// Returns the bin storage strategy used by this sketch. + #[inline] + pub fn store_kind(&self) -> StoreKind { + self.positive.kind() + } + + /// Returns the magnitude threshold below or at which inputs map to the zero bucket. + /// + /// Any finite input `x` with `|x| <= self.zero_threshold()` increments + /// [`zero_weight`](Self::zero_weight) and is approximated by `0.0`. + #[inline] + pub fn zero_threshold(&self) -> f64 { + self.mapping.min_indexable_value() + } + + /// Returns `true` if this sketch has not recorded any finite observations. + #[inline] + pub fn is_empty(&self) -> bool { + self.total_weight == 0 + } + + /// Returns the accumulated stream weight across all buckets. + #[inline] + pub fn total_weight(&self) -> u64 { + self.total_weight + } + + /// Returns the accumulated stream weight in the zero bucket. + #[inline] + pub fn zero_weight(&self) -> u64 { + self.zero_weight + } + + /// Returns the number of non-empty buckets currently retained by this sketch, including the + /// zero bucket when [`zero_weight`](Self::zero_weight) is non-zero. + #[inline] + pub fn num_retained(&self) -> usize { + self.negative.num_occupied() + + usize::from(self.zero_weight != 0) + + self.positive.num_occupied() + } + + /// Returns an implementation-dependent estimate of heap memory usage in bytes. + pub fn estimated_size(&self) -> usize { + self.negative + .estimated_size() + .saturating_add(self.positive.estimated_size()) + } + + /// Updates this sketch with a single observation. + /// + /// [`f64::NAN`], [`f64::INFINITY`], and [`f64::NEG_INFINITY`] values are ignored. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update(10.0); + /// assert_eq!(sketch.total_weight(), 1); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update(&mut self, value: f64) { + self.update_with_weight(value, 1); + } + + /// Updates this sketch with `value` repeated `weight` times. + /// + /// [`f64::NAN`], [`f64::INFINITY`], [`f64::NEG_INFINITY`], and a `weight` of `0` are no-ops. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Cross-language compatibility + /// + /// In the reference DDSketch implementations, Go rejects `NaN`, infinite, and negative-count + /// inputs with an error, whereas Java rejects out-of-range finite/infinite and negative-count + /// inputs while counting `NaN` into the zero bucket; both accept fractional `f64` counts. This + /// method ignores non-finite values and accumulates exact `u64` integer weights; keep + /// per-bucket weights (and [`total_weight`](Self::total_weight) for Java quantile readers) at + /// or below `2^53 - 1` when serializing with [`serialize_compat`](Self::serialize_compat). + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update_with_weight(25.0, 100); + /// assert_eq!(sketch.total_weight(), 100); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update_with_weight(&mut self, value: f64, weight: u64) { + if !value.is_finite() || weight == 0 { + return; + } + let next_total = self + .total_weight + .checked_add(weight) + .expect("total stream weight overflow"); + + let min_indexable = self.mapping.min_indexable_value(); + if value > min_indexable { + let idx = self.mapping.index(value); + self.positive.ensure_index_capacity(idx); + self.positive.add(idx, weight); + } else if value < -min_indexable { + let idx = self.mapping.index(-value); + self.negative.ensure_index_capacity(idx); + self.negative.add(idx, weight); + } else { + self.zero_weight += weight; + } + self.total_weight = next_total; + self.summary.record(value, weight); + } + + /// Merges `other` into `self`, retaining `self`'s [`StoreKind`]. + /// + /// Sketches that share an equivalent index mapping can be merged even when they use different + /// [`StoreKind`] strategies. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) without modifying + /// `self` if `other` uses an incompatible index mapping (even when either sketch is empty), if + /// the combined [`total_weight`](Self::total_weight) would exceed [`u64::MAX`], or if merging + /// into [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut left = DdSketch::new(0.01)?; + /// let mut right = DdSketch::new(0.01)?; + /// left.update(1.0); + /// right.update(100.0); + /// left.merge(&right)?; + /// assert_eq!(left.total_weight(), 2); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn merge(&mut self, other: &Self) -> Result<(), Error> { + if !self.mapping.is_compatible(&other.mapping) { + return Err(Error::invalid_argument(format!( + "cannot merge DDSketches with incompatible index mappings: expected ({:?}, gamma={}, offset={}), got ({:?}, gamma={}, offset={})", + self.mapping.kind(), + self.mapping.gamma(), + self.mapping.index_offset(), + other.mapping.kind(), + other.mapping.gamma(), + other.mapping.index_offset(), + ))); + } + + if other.is_empty() { + return Ok(()); + } + + let next_total = self + .total_weight + .checked_add(other.total_weight) + .ok_or_else(|| { + Error::invalid_argument(format!( + "combined stream weight exceeds {}: left {}, right {}", + u64::MAX, + self.total_weight, + other.total_weight + )) + })?; + + self.negative.validate_merge_capacity(&other.negative)?; + self.positive.validate_merge_capacity(&other.positive)?; + + self.negative.merge_from(&other.negative); + self.positive.merge_from(&other.positive); + self.zero_weight += other.zero_weight; + self.total_weight = next_total; + self.summary.merge_non_empty(other.summary); + Ok(()) + } + + /// Resets this sketch to its empty state while retaining its configuration and allocated + /// dense-store capacity. + pub fn reset(&mut self) { + self.negative.reset(); + self.positive.reset(); + self.zero_weight = 0; + self.total_weight = 0; + self.summary = SummaryState::Empty; + } + + /// Returns the exact minimum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn min_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { min, .. } => Some(min), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact maximum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn max_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { max, .. } => Some(max), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact sum of recorded values, or `None` if this sketch is empty or was decoded + /// from a compatibility image without exact summary statistics. + /// + /// Accumulated with standard IEEE 754 `f64` addition (`value * weight as f64`). Review Comment: Nit: external-java and external-go keep the exact sum with compensated (Kahan) summation, so `sum` can differ from theirs in the last bits on long streams. Fine to keep plain addition; maybe mention it in the cross-language notes. ########## datasketches/src/ddsketch/sketch.rs: ########## @@ -0,0 +1,977 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt; + +use super::DEFAULT_RELATIVE_ACCURACY; +use super::iter::DdSketchIter; +use super::mapping::IndexMapping; +use super::mapping::MappingKind; +use super::serialization::deserialize_compat_sketch; +use super::serialization::deserialize_sketch; +use super::serialization::serialize_compat_sketch; +use super::serialization::serialize_sketch; +use super::store::Store; +use super::store::StoreKind; +use super::store::validate_store_kind; +use crate::error::Error; + +/// Builder for configuring and constructing a [`DdSketch`]. +/// +/// Defaults to `relative_accuracy = 0.01`, [`MappingKind::Cubic`], and +/// [`StoreKind::CollapsingLowestDense`] with `2048` bins per sign. +/// +/// # Examples +/// +/// ``` +/// use datasketches::ddsketch::DdSketchBuilder; +/// use datasketches::ddsketch::MappingKind; +/// use datasketches::ddsketch::StoreKind; +/// +/// let mut sketch = DdSketchBuilder::new(0.005) +/// .mapping(MappingKind::Logarithmic) +/// .store(StoreKind::Sparse) +/// .build()?; +/// sketch.update(42.0); +/// assert_eq!(sketch.total_weight(), 1); +/// # Ok::<(), datasketches::error::Error>(()) +/// ``` +#[derive(Debug, Clone)] +pub struct DdSketchBuilder { + relative_accuracy: f64, + mapping_kind: MappingKind, + store_kind: StoreKind, +} + +impl Default for DdSketchBuilder { + fn default() -> Self { + Self::new(DEFAULT_RELATIVE_ACCURACY) + } +} + +impl DdSketchBuilder { + /// Creates a builder with the given `relative_accuracy` and default mapping and store settings. + pub fn new(relative_accuracy: f64) -> Self { + Self { + relative_accuracy, + mapping_kind: MappingKind::default(), + store_kind: StoreKind::default(), + } + } + + /// Sets the index mapping curve used to assign positive magnitudes to bucket indices. + #[inline] + pub fn mapping(mut self, mapping_kind: MappingKind) -> Self { + self.mapping_kind = mapping_kind; + self + } + + /// Sets the bin storage strategy used for both positive and negative stores. + #[inline] + pub fn store(mut self, store_kind: StoreKind) -> Self { + self.store_kind = store_kind; + self + } + + /// Validates configuration parameters and creates an empty [`DdSketch`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`, or if `store` specifies a collapsing + /// dense store with `max_num_bins` outside `1..=67_108_864`. + pub fn build(self) -> Result<DdSketch, Error> { + validate_store_kind(self.store_kind)?; + let mapping = IndexMapping::from_accuracy(self.mapping_kind, self.relative_accuracy)?; + let negative = Store::new(self.store_kind)?; + let positive = Store::new(self.store_kind)?; + Ok(DdSketch::from_parts( + mapping, + negative, + positive, + 0, + 0, + SummaryState::Empty, + )) + } +} + +#[derive(Debug, Clone, Copy)] +pub(super) enum SummaryState { + Empty, + Active { min: f64, max: f64, sum: f64 }, + Missing, +} + +impl SummaryState { + #[inline] + fn canonical_f64(value: f64) -> f64 { + if value == 0.0 { 0.0 } else { value } + } + + #[inline] + fn record(&mut self, value: f64, weight: u64) { + let v = Self::canonical_f64(value); + let weighted = Self::canonical_f64(v * (weight as f64)); + match self { + Self::Empty => { + *self = Self::Active { + min: v, + max: v, + sum: weighted, + }; + } + Self::Active { min, max, sum } => { + *min = (*min).min(v); + *max = (*max).max(v); + *sum = Self::canonical_f64(*sum + weighted); + } + Self::Missing => {} + } + } + + #[inline] + fn merge_non_empty(&mut self, other: Self) { + *self = match (*self, other) { + (Self::Empty, rhs) => rhs, + ( + Self::Active { min, max, sum }, + Self::Active { + min: omin, + max: omax, + sum: osum, + }, + ) => Self::Active { + min: min.min(omin), + max: max.max(omax), + sum: Self::canonical_f64(sum + osum), + }, + (Self::Missing, _) | (_, Self::Missing) => Self::Missing, + (lhs, Self::Empty) => lhs, + }; + } +} + +/// Mergeable quantile sketch over finite `f64` values with relative-value error guarantees. +/// +/// See the [module documentation](super) for accuracy, memory, and compatibility details. +#[derive(Clone)] +pub struct DdSketch { + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, +} + +impl fmt::Debug for DdSketch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DdSketch") + .field("relative_accuracy", &self.mapping.relative_accuracy()) + .field("mapping_kind", &self.mapping.kind()) + .field("store_kind", &self.positive.kind()) + .field("is_empty", &self.is_empty()) + .field("total_weight", &self.total_weight) + .field("zero_weight", &self.zero_weight) + .field("num_retained", &self.num_retained()) + .field("min_value", &self.min_value()) + .field("max_value", &self.max_value()) + .field("sum", &self.sum()) + .finish() + } +} + +impl Default for DdSketch { + fn default() -> Self { + DdSketchBuilder::default() + .build() + .expect("default DDSketch configuration is valid") + } +} + +impl DdSketch { + /// Creates an empty sketch with the given `relative_accuracy`, [`MappingKind::Cubic`], and + /// [`StoreKind::default()`]. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) if + /// `relative_accuracy` is NaN or outside `[1e-6, 1.0)`. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let sketch = DdSketch::new(0.01)?; + /// assert!(sketch.is_empty()); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn new(relative_accuracy: f64) -> Result<Self, Error> { + DdSketchBuilder::new(relative_accuracy).build() + } + + pub(super) fn from_parts( + mapping: IndexMapping, + negative: Store, + positive: Store, + zero_weight: u64, + total_weight: u64, + summary: SummaryState, + ) -> Self { + debug_assert_eq!( + negative.total_weight() + zero_weight + positive.total_weight(), + total_weight + ); + debug_assert_eq!(total_weight == 0, matches!(summary, SummaryState::Empty)); + Self { + mapping, + negative, + positive, + zero_weight, + total_weight, + summary, + } + } + + #[inline] + pub(super) const fn summary_state(&self) -> SummaryState { + self.summary + } + + #[inline] + pub(super) const fn mapping(&self) -> IndexMapping { + self.mapping + } + + #[inline] + pub(super) const fn positive_store(&self) -> &Store { + &self.positive + } + + #[inline] + pub(super) const fn negative_store(&self) -> &Store { + &self.negative + } + + /// Returns the relative value accuracy guaranteed for non-collapsed buckets. + #[inline] + pub fn relative_accuracy(&self) -> f64 { + self.mapping.relative_accuracy() + } + + /// Returns the index mapping curve used by this sketch. + #[inline] + pub fn mapping_kind(&self) -> MappingKind { + self.mapping.kind() + } + + /// Returns the bin storage strategy used by this sketch. + #[inline] + pub fn store_kind(&self) -> StoreKind { + self.positive.kind() + } + + /// Returns the magnitude threshold below or at which inputs map to the zero bucket. + /// + /// Any finite input `x` with `|x| <= self.zero_threshold()` increments + /// [`zero_weight`](Self::zero_weight) and is approximated by `0.0`. + #[inline] + pub fn zero_threshold(&self) -> f64 { + self.mapping.min_indexable_value() + } + + /// Returns `true` if this sketch has not recorded any finite observations. + #[inline] + pub fn is_empty(&self) -> bool { + self.total_weight == 0 + } + + /// Returns the accumulated stream weight across all buckets. + #[inline] + pub fn total_weight(&self) -> u64 { + self.total_weight + } + + /// Returns the accumulated stream weight in the zero bucket. + #[inline] + pub fn zero_weight(&self) -> u64 { + self.zero_weight + } + + /// Returns the number of non-empty buckets currently retained by this sketch, including the + /// zero bucket when [`zero_weight`](Self::zero_weight) is non-zero. + #[inline] + pub fn num_retained(&self) -> usize { + self.negative.num_occupied() + + usize::from(self.zero_weight != 0) + + self.positive.num_occupied() + } + + /// Returns an implementation-dependent estimate of heap memory usage in bytes. + pub fn estimated_size(&self) -> usize { + self.negative + .estimated_size() + .saturating_add(self.positive.estimated_size()) + } + + /// Updates this sketch with a single observation. + /// + /// [`f64::NAN`], [`f64::INFINITY`], and [`f64::NEG_INFINITY`] values are ignored. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update(10.0); + /// assert_eq!(sketch.total_weight(), 1); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update(&mut self, value: f64) { + self.update_with_weight(value, 1); + } + + /// Updates this sketch with `value` repeated `weight` times. + /// + /// [`f64::NAN`], [`f64::INFINITY`], [`f64::NEG_INFINITY`], and a `weight` of `0` are no-ops. + /// + /// # Panics + /// + /// Panics without modifying the sketch if [`total_weight`](Self::total_weight) would exceed + /// [`u64::MAX`], or if [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Cross-language compatibility + /// + /// In the reference DDSketch implementations, Go rejects `NaN`, infinite, and negative-count + /// inputs with an error, whereas Java rejects out-of-range finite/infinite and negative-count + /// inputs while counting `NaN` into the zero bucket; both accept fractional `f64` counts. This + /// method ignores non-finite values and accumulates exact `u64` integer weights; keep + /// per-bucket weights (and [`total_weight`](Self::total_weight) for Java quantile readers) at + /// or below `2^53 - 1` when serializing with [`serialize_compat`](Self::serialize_compat). + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut sketch = DdSketch::new(0.01)?; + /// sketch.update_with_weight(25.0, 100); + /// assert_eq!(sketch.total_weight(), 100); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + #[inline] + pub fn update_with_weight(&mut self, value: f64, weight: u64) { + if !value.is_finite() || weight == 0 { + return; + } + let next_total = self + .total_weight + .checked_add(weight) + .expect("total stream weight overflow"); + + let min_indexable = self.mapping.min_indexable_value(); + if value > min_indexable { + let idx = self.mapping.index(value); + self.positive.ensure_index_capacity(idx); + self.positive.add(idx, weight); + } else if value < -min_indexable { + let idx = self.mapping.index(-value); + self.negative.ensure_index_capacity(idx); + self.negative.add(idx, weight); + } else { + self.zero_weight += weight; + } + self.total_weight = next_total; + self.summary.record(value, weight); + } + + /// Merges `other` into `self`, retaining `self`'s [`StoreKind`]. + /// + /// Sketches that share an equivalent index mapping can be merged even when they use different + /// [`StoreKind`] strategies. + /// + /// # Errors + /// + /// Returns [`InvalidArgument`](crate::error::ErrorKind::InvalidArgument) without modifying + /// `self` if `other` uses an incompatible index mapping (even when either sketch is empty), if + /// the combined [`total_weight`](Self::total_weight) would exceed [`u64::MAX`], or if merging + /// into [`StoreKind::UnboundedDense`] would exceed addressable slice capacity. + /// + /// # Examples + /// + /// ``` + /// use datasketches::ddsketch::DdSketch; + /// + /// let mut left = DdSketch::new(0.01)?; + /// let mut right = DdSketch::new(0.01)?; + /// left.update(1.0); + /// right.update(100.0); + /// left.merge(&right)?; + /// assert_eq!(left.total_weight(), 2); + /// # Ok::<(), datasketches::error::Error>(()) + /// ``` + pub fn merge(&mut self, other: &Self) -> Result<(), Error> { + if !self.mapping.is_compatible(&other.mapping) { + return Err(Error::invalid_argument(format!( + "cannot merge DDSketches with incompatible index mappings: expected ({:?}, gamma={}, offset={}), got ({:?}, gamma={}, offset={})", + self.mapping.kind(), + self.mapping.gamma(), + self.mapping.index_offset(), + other.mapping.kind(), + other.mapping.gamma(), + other.mapping.index_offset(), + ))); + } + + if other.is_empty() { + return Ok(()); + } + + let next_total = self + .total_weight + .checked_add(other.total_weight) + .ok_or_else(|| { + Error::invalid_argument(format!( + "combined stream weight exceeds {}: left {}, right {}", + u64::MAX, + self.total_weight, + other.total_weight + )) + })?; + + self.negative.validate_merge_capacity(&other.negative)?; + self.positive.validate_merge_capacity(&other.positive)?; + + self.negative.merge_from(&other.negative); + self.positive.merge_from(&other.positive); + self.zero_weight += other.zero_weight; + self.total_weight = next_total; + self.summary.merge_non_empty(other.summary); + Ok(()) + } + + /// Resets this sketch to its empty state while retaining its configuration and allocated + /// dense-store capacity. + pub fn reset(&mut self) { + self.negative.reset(); + self.positive.reset(); + self.zero_weight = 0; + self.total_weight = 0; + self.summary = SummaryState::Empty; + } + + /// Returns the exact minimum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn min_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { min, .. } => Some(min), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact maximum value recorded by this sketch, or `None` if this sketch is empty + /// or was decoded from a compatibility image without exact summary statistics. + #[inline] + pub fn max_value(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { max, .. } => Some(max), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the exact sum of recorded values, or `None` if this sketch is empty or was decoded + /// from a compatibility image without exact summary statistics. + /// + /// Accumulated with standard IEEE 754 `f64` addition (`value * weight as f64`). + #[inline] + pub fn sum(&self) -> Option<f64> { + match self.summary { + SummaryState::Active { sum, .. } => Some(sum), + SummaryState::Empty | SummaryState::Missing => None, + } + } + + /// Returns the approximate lower discrete quantile for `rank` in `[0.0, 1.0]`, or `Ok(None)` + /// if this sketch is empty. + /// + /// Selects the bucket containing zero-based sorted item `floor(rank * (total_weight - 1))` + /// using the standard `f64` product when `total_weight <= 2^53` (matching the Java reference + /// implementation) and exact integer significand scaling when `total_weight > 2^53`. When exact + /// summary statistics are present, `rank == 0.0` returns [`min_value`](Self::min_value), Review Comment: Nit: external-java and external-go only clamp the bucket estimate to `[min, max]`, with no special case for `0.0` and `1.0`. So `quantile(1.0)` here returns the exact max, while they can return a bucket value slightly below it. internal-1 does the same as this PR, so I'm happy with the choice. Maybe note the difference in the cross-language section. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
