tisonkun opened a new issue, #367:
URL: https://github.com/apache/datasketches-rust/issues/367
## Summary
Weight-overflow and parameter-validation contracts currently differ across
sketches for parallel operations (`merge`, `decay`, and `update_with_weight`).
Unifying these rules clarifies which errors callers can recover from with
`Result<(), Error>` and guarantees sketches remain unmodified on error.
## Current State
| Operation | Sketch / Type
| Current Signature / Behavior
| Module |
| -------------------- |
-------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------------------------------------------------------------------------------------
| ---------------------------------------- |
| `merge` | `KllSketch<T>`, `ReqSketch<T>`
| Returns `Result<(), Error>` (`InvalidArgument`)
on `u64::MAX` overflow or incompatibility
| `datasketches/src/{kll,req}/sketch.rs` |
| `merge` | `CountMinSketch<T>`
| Returns `Result<(), Error>` (`InvalidArgument`)
on `T::MAX` `total_weight` overflow or config mismatch
| `datasketches/src/countmin/sketch.rs` |
| `merge` | `FrequentItemsSketch<T>`
| Returns `()`, panics via `.expect("total stream
weight overflow")`; unchecked `self.offset += other.offset`; mutates map in
loop before updating `stream_weight` | `datasketches/src/frequencies/sketch.rs`
|
| `merge` | `TDigestMut`
| Returns `()`, panics via `.expect("total weight
overflow")` (and `FromIterator<TDigestMut>` panics on overflow)
| `datasketches/src/tdigest/sketch.rs` |
| `decay` | `CountMinSketch<T: UnsignedCountMinValue>`
| Returns `()`, panics via `assert!(decay > 0.0 &&
decay <= 1.0, ...)` on `NaN`, `<= 0.0`, or `> 1.0`
| `datasketches/src/countmin/sketch.rs` |
| `update_with_weight` | `FrequentItemsSketch<T>`
| Accepts user-supplied `u64`; panics via
`checked_add(weight).expect(...)`
|
`datasketches/src/frequencies/sketch.rs` |
| `update_with_weight` | `CountMinSketch<T>`
| Accepts user-supplied `T` (`i8..i64`, `u8..u64`);
panics on `checked_abs()` (`T::MIN`) or `total_weight` overflow
| `datasketches/src/countmin/sketch.rs` |
| `update` | `KllSketch`, `ReqSketch`, `TDigestMut`,
`FrequentItemsSketch`, `CountMinSketch` | Increments by `1`; panics on
`u64::MAX` / `T::MAX` total weight overflow
| Various
|
## Proposed Scope
1. **Unify `merge` to `Result<(), Error>`**:
- Change `FrequentItemsSketch::merge(&mut self, other: &Self) ->
Result<(), Error>` and `TDigestMut::merge(&mut self, other: &TDigestMut) ->
Result<(), Error>` to return `Err(Error::invalid_argument(...))` on
total-weight overflow instead of panicking.
- In `FrequentItemsSketch::merge`, validate both
`self.stream_weight.checked_add(other.stream_weight)` and
`self.offset.checked_add(other.offset)` **before** mutating `self` (and avoid
relying on `update_with_weight_ref` repeatedly mutating `self.stream_weight`
mid-loop before resetting `self.stream_weight = merged_total`), preserving the
no-mutation guarantee on error.
- Check `FromIterator<TDigestMut>`
(`datasketches/src/tdigest/sketch.rs`): either clearly document its
panic-on-overflow contract (matching `FromIterator` signature constraints) or
add a fallible helper such as `TDigestMut::try_from_iter` / `try_merge_all`.
2. **Make `CountMinSketch::decay` fallible**:
- Change `CountMinSketch::<T: UnsignedCountMinValue>::decay(&mut self,
decay: f64) -> Result<(), Error>` so invalid factors (`NaN`, `<= 0.0`, `> 1.0`)
return `Err(Error::invalid_argument(...))` without modifying the sketch,
matching `CountMinSketch::new` and `merge` while keeping `halve(&mut self)`
infallible.
3. **Evaluate weighted updates (`update_with_weight` /
`try_update_with_weight`) vs unit `update`**:
- Weighted updates take caller-supplied weights that can easily overflow
narrow `CountMinValue` types (`u8`, `i8`, `u16`, `i16`, `u32`, `i32`) or `u64`,
and `CountMinSketch::update_with_weight` additionally panics on an empty sketch
if passed `T::MIN` because `T::MIN.checked_abs()` is `None`.
- Decide whether to make `FrequentItemsSketch::{update_with_weight,
update_with_weight_ref}` and `CountMinSketch::update_with_weight` return
`Result<(), Error>` directly in the next breaking release (or provide
`try_update_with_weight* -> Result<(), Error>`), while keeping single-item
`update(&mut self, item)` returning `()` with documented counter/stream-weight
overflow preconditions.
4. **Document the crate-wide contract in `CONTRIBUTING.md`**:
- Add an error-vs-panic contract section (alongside "Query semantics")
specifying when mutations return `Result<(), Error>` (runtime
inputs/compatibility/merge overflow/user weights, guaranteeing `self` is
unchanged on `Err`) vs when unit-weight streaming `update` documents a
`u64::MAX`/`T::MAX` overflow `# Panics` contract.
## Acceptance Criteria
- `FrequentItemsSketch::merge`, `TDigestMut::merge`, and
`CountMinSketch::decay` return `Result<(), Error>` with
`ErrorKind::InvalidArgument` and leave `self` unchanged on error.
- Weighted update overflow behavior (`update_with_weight` /
`try_update_with_weight`) is decided, consistently implemented, and covered by
regression tests (including `T::MIN` and narrow counter overflow on
`CountMinSketch`, plus `u64::MAX` merge/offset overflow on
`FrequentItemsSketch` and `TDigestMut`).
- `CONTRIBUTING.md` and `CHANGELOG.md` are updated, and `cargo x check`,
`cargo x test`, and `cargo x lint` pass.
--
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]