WenyXu commented on code in PR #293:
URL: https://github.com/apache/datasketches-rust/pull/293#discussion_r4582709809
##########
datasketches/src/kll/sketch.rs:
##########
@@ -143,6 +143,72 @@ impl<T: Clone + Ord> KllSketch<T> {
self.internal_update(item);
}
+ /// Updates the sketch with an item repeated `weight` times.
+ ///
+ /// The result is equivalent to calling [`update`](Self::update) `weight`
times, at a cost
+ /// that grows with the logarithm of `weight` rather than with `weight`
itself. A weight of
+ /// zero is a no-op.
+ ///
+ /// # Panics
+ ///
+ /// Panics without modifying the sketch if the stream weight would exceed
[`u64::MAX`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use datasketches::common::SearchCriteria;
+ /// # use datasketches::kll::KllSketch;
+ /// let mut sketch = KllSketch::<i64>::new(200).unwrap();
+ /// sketch.update_with_weight(1, 3);
+ /// sketch.update_with_weight(2, 1_000_000);
+ /// assert_eq!(sketch.n(), 1_000_003);
+ /// assert_eq!(
+ /// sketch.rank(&1, SearchCriteria::Inclusive).unwrap(),
+ /// 3.0 / 1_000_003.0
+ /// );
+ /// ```
+ pub fn update_with_weight(&mut self, item: T, weight: u64) {
+ if weight == 0 {
+ return;
+ }
+ let final_n = self
+ .n
+ .checked_add(weight)
+ .expect("total stream weight overflow");
+ if weight < (self.capacity - self.num_retained) as u64 {
Review Comment:
`weight == self.capacity - self.num_retained` cannot compact either, since
`internal_update` checks `num_retained >= capacity` before pushing. With `<`,
that boundary takes the merge path and drops out of exact mode for no reason:
at k=200, 150 `update` calls plus `update_with_weight(x, 50)` keep 153 retained
items in estimation mode, while 50 `update` calls keep all 200. `<=` costs
nothing; if you prefer parity with Java and cpp, a comment saying so would help.
##########
datasketches/src/kll/sketch.rs:
##########
@@ -143,6 +143,72 @@ impl<T: Clone + Ord> KllSketch<T> {
self.internal_update(item);
}
+ /// Updates the sketch with an item repeated `weight` times.
+ ///
+ /// The result is equivalent to calling [`update`](Self::update) `weight`
times, at a cost
+ /// that grows with the logarithm of `weight` rather than with `weight`
itself. A weight of
+ /// zero is a no-op.
+ ///
+ /// # Panics
+ ///
+ /// Panics without modifying the sketch if the stream weight would exceed
[`u64::MAX`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use datasketches::common::SearchCriteria;
+ /// # use datasketches::kll::KllSketch;
+ /// let mut sketch = KllSketch::<i64>::new(200).unwrap();
+ /// sketch.update_with_weight(1, 3);
+ /// sketch.update_with_weight(2, 1_000_000);
+ /// assert_eq!(sketch.n(), 1_000_003);
+ /// assert_eq!(
+ /// sketch.rank(&1, SearchCriteria::Inclusive).unwrap(),
+ /// 3.0 / 1_000_003.0
+ /// );
+ /// ```
+ pub fn update_with_weight(&mut self, item: T, weight: u64) {
+ if weight == 0 {
Review Comment:
This silently accepts a zero weight, while both references reject it:
datasketches-java `KllItemsSketch.update(item, weight)` throws `Weight is less
than one.`, and the companion cpp PR #542 throws `weight must be positive`.
Following `CountMinSketch::update_with_weight` is defensible, but please record
the divergence here so callers porting from Java or C++ are not surprised by
the silent success. I verified the Rust branches and the level folding against
both references; only this case differs.
##########
tests-integration/tests/kll_test/update.rs:
##########
@@ -0,0 +1,148 @@
+// 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::panic::AssertUnwindSafe;
+use std::panic::catch_unwind;
+
+use datasketches::common::SearchCriteria;
+use datasketches::kll::KllSketch;
+
+const DEFAULT_K: u16 = 200;
+const RANK_EPS_FOR_K_200: f64 = 0.0133;
+
+#[test]
+fn zero_weight_is_a_no_op() {
+ let mut sketch = KllSketch::<i64>::new(DEFAULT_K).unwrap();
+ sketch.update_with_weight(1, 0);
+ assert!(sketch.is_empty());
+ assert_eq!(sketch.min_item(), None);
+}
+
+#[test]
+fn weighted_update_matches_repeated_updates_in_exact_mode() {
Review Comment:
This test never reaches the branch it looks like it covers: weights `1..=15`
total 120 against a capacity of 200, so every call takes the `O(weight)` fast
path and the merge path is untested here. Adding the boundary case (150
updates, then `update_with_weight(x, 50)`) would cover the guard in `sketch.rs`
and pin the one weight where the choice changes the sketch's mode.
--
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]