This is an automated email from the ASF dual-hosted git repository.

tisonkun pushed a commit to branch codex/cross-language-p1-regressions
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git

commit 6cc0a33b1976baf205e2446fcc55b25ca7b27288
Author: tison <[email protected]>
AuthorDate: Tue Jul 28 23:25:40 2026 +0800

    test(hll): cover merge order regression
---
 datasketches/tests/hll_test/union.rs | 80 ++++++++++++++++++++++--------------
 1 file changed, 50 insertions(+), 30 deletions(-)

diff --git a/datasketches/tests/hll_test/union.rs 
b/datasketches/tests/hll_test/union.rs
index 91080bf..b9d2c7e 100644
--- a/datasketches/tests/hll_test/union.rs
+++ b/datasketches/tests/hll_test/union.rs
@@ -402,40 +402,60 @@ fn test_union_reset() {
     }
 }
 
-#[test]
-fn test_union_commutativity() {
-    // Verify A∪B = B∪A
-    let mut sketch_a = HllSketch::new(12, HllType::Hll8);
-    let mut sketch_b = HllSketch::new(12, HllType::Hll8);
-
-    for i in 0..1000 {
-        sketch_a.update(i);
-    }
-    for i in 500..1500 {
-        sketch_b.update(i);
+fn next_power_series_point(points_per_octave: i32, current: i64) -> i64 {
+    let current = current.max(1);
+    let mut generating_index =
+        ((current as f64).log2() * f64::from(points_per_octave)).round() as 
i32;
+    loop {
+        generating_index += 1;
+        let next = 2_f64
+            .powf(f64::from(generating_index) / f64::from(points_per_octave))
+            .round() as i64;
+        if next > current {
+            return next;
+        }
     }
+}
 
-    // Union in order A, B
-    let mut union1 = HllUnion::new(12);
-    union1.update(&sketch_a);
-    union1.update(&sketch_b);
-
-    // Union in order B, A
-    let mut union2 = HllUnion::new(12);
-    union2.update(&sketch_b);
-    union2.update(&sketch_a);
+fn power_series_sketch(start: i64, points_per_octave: i32, limit: i64) -> 
HllSketch {
+    let mut sketch = HllSketch::new(11, HllType::Hll8);
+    let mut value = start;
+    while value < limit {
+        sketch.update(value);
+        value = next_power_series_point(points_per_octave, value);
+    }
+    sketch
+}
 
-    let est1 = union1.estimate();
-    let est2 = union2.estimate();
+fn merge_estimate(sketches: &[&HllSketch; 3], order: [usize; 3]) -> f64 {
+    let mut union = HllUnion::new(11);
+    for index in order {
+        union.update(sketches[index]);
+    }
+    union.estimate()
+}
 
-    let relative_diff = (est1 - est2).abs() / est1.max(est2);
-    assert!(
-        relative_diff < 0.001,
-        "Union not commutative: {} vs {} (diff: {:.4}%)",
-        est1,
-        est2,
-        relative_diff * 100.0
-    );
+#[test]
+fn test_union_merge_order_regression() {
+    // Large fractional powers of two reproduce the reference implementation's 
merge-order case.
+    let points_per_octave = 1 << 17;
+    let a = power_series_sketch(1_i64 << 59, points_per_octave, 1_i64 << 60);
+    let b = power_series_sketch(1_i64 << 60, points_per_octave, 1_i64 << 61);
+    let c = power_series_sketch(1_i64 << 61, points_per_octave, 1_i64 << 62);
+    let sketches = [&a, &b, &c];
+
+    let estimates = [
+        merge_estimate(&sketches, [0, 1, 2]),
+        merge_estimate(&sketches, [0, 2, 1]),
+        merge_estimate(&sketches, [1, 0, 2]),
+        merge_estimate(&sketches, [1, 2, 0]),
+        merge_estimate(&sketches, [2, 0, 1]),
+        merge_estimate(&sketches, [2, 1, 0]),
+    ];
+
+    for estimate in estimates.iter().skip(1) {
+        assert_eq!(*estimate, estimates[0]);
+    }
 }
 
 #[test]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to