pmcgleenon commented on PR #299:
URL:
https://github.com/apache/datasketches-rust/pull/299#issuecomment-6705240877
**1. Empty strings and byte arrays are counted.**
Section 4 says an empty string or byte array is ignored: the update does
nothing and a query returns 0. Here
`update_with_weight(raw_bytes::from_str(""), 5)` adds 5 to the total weight and
increments one counter per row, so the image differs from what Java and Go
write for the same update stream, and `estimate` returns 5 where every other
implementation returns 0.
The doc comment asks callers to skip these, but the spec indicates it should
be handled by the implementation.
This fails today on `assert!(sketch.is_empty())`:
```rust
#[test]
fn v2_ignores_empty_strings_and_byte_arrays() {
let mut sketch = CountMinSketch::<i64>::new(3, 1000).unwrap();
sketch.update_with_weight(raw_bytes::from_str(""), 5);
sketch.update_with_weight(raw_bytes::from_slice(&[]), 7);
assert!(sketch.is_empty());
sketch.update_with_weight(raw_bytes::from_str("apple"), 5);
assert_eq!(sketch.estimate(raw_bytes::from_str("")), 0);
assert_eq!(sketch.estimate(raw_bytes::from_slice(&[])), 0);
}
```
**2. Bare narrow integers are not widened.**
Section 4 says a narrower integer is widened to 64 bits first, sign-extended
if signed. `V2Hasher` overrides only `write_i64` and `write_u64`, so
`update(9i8)` hashes a single byte and lands in buckets 406, 263, 103 instead
of 490, 217, 440 (the 7.3 vector for integer 9).
```rust
#[test]
fn v2_widens_narrow_integers_to_64_bits() {
let mut sketch = CountMinSketch::<i64>::new(3, 1000).unwrap();
sketch.update_with_weight(9i64, 10);
sketch.update_with_weight(-2i64, 20);
assert_eq!(sketch.estimate(9i8), 10);
assert_eq!(sketch.estimate(9u16), 10);
assert_eq!(sketch.estimate(9usize), 10);
assert_eq!(sketch.estimate(-2i32), 20);
assert_eq!(sketch.estimate(-2isize), 20);
}
```
We could add something like this?
```rust
fn write_u8(&mut self, v: u8) { self.write_u64(u64::from(v)) }
fn write_u16(&mut self, v: u16) { self.write_u64(u64::from(v)) }
fn write_u32(&mut self, v: u32) { self.write_u64(u64::from(v)) }
fn write_usize(&mut self, v: usize) { self.write_u64(v as u64) }
fn write_i8(&mut self, v: i8) { self.write_i64(i64::from(v)) }
fn write_i16(&mut self, v: i16) { self.write_i64(i64::from(v)) }
fn write_i32(&mut self, v: i32) { self.write_i64(i64::from(v)) }
fn write_isize(&mut self, v: isize) { self.write_i64(v as i64) }
```
--
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]