tisonkun opened a new issue, #363:
URL: https://github.com/apache/datasketches-rust/issues/363

   ## Summary
   
   `KllSketch::merge` in `datasketches/src/kll/sketch.rs` currently 
short-circuits when `other.is_empty()` before validating that `self.m == 
other.m`. As a result, merging an empty `KllSketch` with an incompatible `m` 
parameter silently returns `Ok(())`, whereas merging a non-empty sketch with a 
different `m` returns `Err(Error)` with `ErrorKind::InvalidArgument`.
   
   ## Current Behavior vs Sibling Sketches
   
   In `datasketches/src/kll/sketch.rs`, `KllSketch::merge` checks emptiness 
first:
   
   ```rust
   if other.is_empty() {
       return Ok(());
   }
   
   if self.m != other.m {
       return Err(Error::invalid_argument(format!(
           "cannot merge sketches with different m values: {} and {}",
           self.m, other.m
       )));
   }
   ```
   
   By contrast, `ReqSketch::merge` in `datasketches/src/req/sketch.rs` checks 
configuration compatibility (`self.rank_accuracy != other.rank_accuracy`) 
before checking `other.is_empty()`. In addition, `ReqSketch::merge` explicitly 
documents the no-mutation guarantee and configuration mismatch error under `# 
Errors`, whereas `KllSketch::merge` currently documents only:
   
   > Returns an error if the combined stream weight exceeds [`u64::MAX`].
   
   ## Scope
   
   - Move the `self.m != other.m` check before `if other.is_empty() { return 
Ok(()); }` in `KllSketch::merge` (`datasketches/src/kll/sketch.rs`).
   - Update the `# Errors` doc comment on `KllSketch::merge` to match 
`ReqSketch::merge`, documenting both the `m` mismatch condition and the 
`u64::MAX` overflow condition without modifying `self` on error.
   - Add a unit test (`#[cfg(test)] mod tests`) inside 
`datasketches/src/kll/sketch.rs` verifying that merging an empty `KllSketch` 
with a different `m` returns `ErrorKind::InvalidArgument` and leaves `self` 
unchanged (because `KllSketch::new` currently always initializes `DEFAULT_M = 
8` from `datasketches/src/kll/mod.rs` and `deserialize` rejects non-default 
`m`, constructing a mismatched-`m` instance requires crate-private access as 
described in `CONTRIBUTING.md`).
   
   ## Acceptance Criteria
   
   - Merging an empty `KllSketch` whose `m` differs from `self.m` returns `Err` 
with `ErrorKind::InvalidArgument` without modifying `self`.
   - `KllSketch::merge` documentation accurately specifies all error conditions 
and the no-mutation invariant.
   - `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]

Reply via email to