tisonkun commented on code in PR #350:
URL: https://github.com/apache/datasketches-rust/pull/350#discussion_r4546190589
##########
datasketches/src/thetafamily/common/jaccard_similarity.rs:
##########
@@ -185,6 +116,11 @@ where
return Ok(JaccardSimilarity::exact(1.0));
}
+ let union_count = union.retained_entries().len() as u64;
+ if union_count == 0 {
+ return Ok(JaccardSimilarity::new(0.0, 0.5, 1.0));
Review Comment:
I think keeping the early return before constructing `IntersectionState` is
preferable here:
1. Rejecting duplicate hashes in a corrupted deserialized sketch was an
incidental side effect of `IntersectionState`'s first-update hash-table load
rather than a contract of `Jaccard`: even if we move this check below
`intersection.update(...)`, swapping the exact same pair to `compute(sketch_b,
sketch_a)` still returns `Ok(0.5)` because the first update sets `num_retained
= 0` and the second `intersection.update` short-circuits before reading entries
(`intersection.rs:121-130`). The same asymmetry holds for `(valid_disjoint,
corrupt)` (`Ok(0.0)`) vs `(corrupt, valid_disjoint)` (`Err`), `(corrupt,
empty)` (`Ok(0.0)`), and `exactly_equal`.
2. For valid inputs where a low `theta` filters the union down to
`union_count == 0`, `intersection_count` below `union_theta` is guaranteed to
be `0`, so returning early avoids allocating and populating an intersection
hash table for `sketch_a` only to discard it (while keeping `compute(a, b)`
symmetric with `compute(b, a)`).
If we want to reject malformed compact images with duplicate hashes
consistently, validating uniqueness at `deserialize` time would be the right
boundary rather than relying on order-dependent set-operation paths.
--
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]