tisonkun commented on PR #21:
URL: https://github.com/apache/datasketches-tck/pull/21#issuecomment-6826907945

   In Section 4.4 ("Null and empty items"), could we make the query rule 
explicit across all three query methods (`estimate`, `lowerBound`, and 
`upperBound`)?
   
   > **Version 2 rule:** an empty string, an empty byte array (zero length, 
such as Java `new byte[0]`), and a null, in languages that have one, are all 
ignored. An update does nothing, and a query returns 0.
   
   In Count-Min, `estimate(item) == 0` does not automatically imply 
`upperBound(item) == 0` once `totalWeight > 0`, because `upperBound` adds the 
additive error term `relativeError * totalWeight`. Tightening the second 
sentence to:
   
   > An update does nothing, and every query (estimate, lower bound, and upper 
bound) returns 0.
   
   makes it unambiguous that `upperBound` (and `lowerBound`) must also 
short-circuit to `0` for ignored empty or null inputs rather than adding 
`relativeError * totalWeight`.
   
   For context, here is what each current upstream implementation returns for 
`(estimate, lowerBound, upperBound)` on an **empty sketch** (`isEmpty == true`, 
`total == 0`, whether newly constructed or deserialized from a 16-byte 
`IS_EMPTY` image) versus a **non-empty sketch** (`total > 0`, `eps = 
relativeError`):
   
   - **Empty sketch (`total == 0`)**: every implementation allocates a 
zero-initialized counter table of size `numHashes * numBuckets`. Because all 
counters are `0` and `eps * 0 == 0`, querying any normal item returns `0` 
(`0.0` in Python) across `estimate`, `lowerBound`, and `upperBound` (for valid 
`numHashes >= 1`). That zero `total` also masks missing empty-item checks on an 
empty sketch—except Java `(byte[]) null`, which throws `NullPointerException`, 
and Python `None`, which raises `TypeError`.
   - **Non-empty sketch (`total > 0`)**: once `eps * total >= 1` or 0-byte hash 
buckets accumulate weight, missing guards in `upperBound` or raw-byte overloads 
surface across implementations.
   
   | Implementation     | Input                                                 
                | Empty sketch (`est / lower / upper`) | Non-empty sketch (`est 
/ lower / upper`)                                 | Current behavior notes      
                                                                                
                       |
   | ------------------ | 
--------------------------------------------------------------------- | 
------------------------------------ | 
------------------------------------------------------------------------ | 
----------------------------------------------------------------------------------------------------------------------------------
 |
   | Java (`5ff11f6`)   | Normal item (`long`, `String`, `byte[]`)              
                | `0 / 0 / 0`                          | `est / est / est + 
(long)(eps * total)`                                  | All counters are `0` 
when `total == 0`                                                               
                              |
   | Java (`5ff11f6`)   | `""`, `(String) null`, `new byte[0]`                  
                | `0 / 0 / 0`                          | `0 / 0 / 0`            
                                                  | `update` is a no-op; 
`getEstimate`, `getLowerBound`, and `getUpperBound` all short-circuit to `0`    
                              |
   | Java (`5ff11f6`)   | `(byte[]) null`                                       
                | Throws `NullPointerException`        | Throws 
`NullPointerException`                                            | `update`, 
`getEstimate` (and `getLowerBound`), and `getUpperBound` read `item.length` 
without a null check                         |
   | Go (`c9701e7`)     | Normal item (`uint64`, `string`, `[]byte`)            
                | `0 / 0 / 0`                          | `est / est / est + 
int64(eps * total)`                                   | `string` currently 
exposes `GetEstimateString` only (no `*BoundString` helpers)                    
                                |
   | Go (`c9701e7`)     | `""` (`GetEstimateString`)                            
                | `0` (`n/a` for bounds)               | `0` (`n/a` for bounds) 
                                                  | `UpdateString("")` is a 
no-op                                                                           
                           |
   | Go (`c9701e7`)     | `[]byte{}` or `nil` `[]byte`                          
                | `0 / 0 / 0`                          | `0 / 0 / int64(eps * 
total)`                                             | `GetUpperBound` lacks a 
`len(item) == 0` check; masked when `total == 0`, non-zero once `eps * total >= 
1`                         |
   | C++ (`386f335`)    | Normal item (`int64_t`, `uint64_t`, `std::string`, 
`(ptr, size > 0)`) | `0 / 0 / 0`                          | `est / est / W(est 
+ eps * total)`                                       | Assumes `num_hashes >= 
1` (`num_hashes == 0` is unguarded and returns `numeric_limits<W>::max()` for 
`estimate`/`lower_bound`)     |
   | C++ (`386f335`)    | `std::string("")`                                     
                | `0 / 0 / 0`                          | `0 / 0 / 0`            
                                                  | `update` is a no-op; 
`get_estimate`, `get_lower_bound`, and `get_upper_bound` all early-return `0`   
                              |
   | C++ (`386f335`)    | `(ptr, 0)` (including `(nullptr, 0)`)                 
                | `0 / 0 / 0`                          | `c0 / c0 / W(c0 + eps 
* total)`                                          | `(const void*, size_t)` 
has no `size == 0` guard (`c0` is the min counter at the 0-byte hash buckets; 
`update` also mutates state) |
   | Python (`bd0231d`) | Normal item (`int`, `str`)                            
                | `0.0 / 0.0 / 0.0`                    | `est / est / est + eps 
* total`                                          | Wraps C++ 
`count_min_sketch<double>` (`int64_t` and `std::string` overloads only)         
                                         |
   | Python (`bd0231d`) | `""`                                                  
                | `0.0 / 0.0 / 0.0`                    | `0.0 / 0.0 / 0.0`      
                                                  | Delegates to C++ 
`std::string`; passing `None` or `bytes` raises `TypeError`                     
                                  |
   | Rust (`e7d151e`)   | Normal item (`I: Hash`)                               
                | `0 / 0 / 0`                          | `est / est / est + 
floor(eps * total)`                                   | Enforces `num_hashes >= 
1` at construction and deserialization                                          
                           |
   | Rust (`e7d151e`)   | `raw_bytes("")` / `raw_bytes(&[])`                    
                | `0 / 0 / 0`                          | `0 / 0 / 0` (guarded) 
or `c0 / c0 / c0 + floor(eps * total)` (unguarded) | Generic `<I: Hash>` API 
does not inspect emptiness inside the sketch; `countmin` docs direct callers to 
guard `!item.is_empty()`   |
   


-- 
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