GitHub user tisonkun created a discussion: Cross-language hashing: inconsistent empty-byte and signed-zero handling
While documenting cross-language input handling in [apache/datasketches-rust#340](https://github.com/apache/datasketches-rust/pull/340), I found two inconsistencies in the Java and C++ implementations: empty byte inputs and signed floating-point zero. A Bloom filter written in Java can be read successfully in C++, but querying an item that was inserted in Java can return `false`. As in the [Count-Min discussion](https://github.com/apache/datasketches-tck/discussions/18), it would help to agree on the input contract and compatibility implications before changing implementations. ## 1. What happens The reproduction below writes two separate Bloom filters in Java, each containing one item, then reads them in C++. Both use 1,024 bits, three hashes, and seed 9001. | Inserted item in Java | Java query for that item | C++ query after reading the same image | | ---------------------- | -------------------------- | ---------------------------------------- | | Empty `byte[]` | `true` | `false` | | `-0.0` | `true` | `false` | These results were reproduced against [Java `ef440a8`](https://github.com/apache/datasketches-java/commit/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7) and [C++ `c22f2b1`](https://github.com/apache/datasketches-cpp/commit/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3), using JDK 25 and a C++17 compiler on little-endian hardware. The scope here is these source revisions; this is not a release-by-release or all-language audit. ## 2. Empty byte inputs Here, an empty byte input means Java `new byte[0]` or a **non-null C++ pointer with length zero**. It does not mean a null pointer or an empty sketch. | Family | Java empty-byte update | C++ zero-length byte update | Source | | -------------- | ------------------------ | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | HLL | Ignores it | Hashes and updates | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/hll/BaseHllSketch.java#L368), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/hll/include/HllSketch-internal.hpp#L205) | | CPC | Ignores it | Hashes and updates | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/cpc/CpcSketch.java#L360), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/cpc/include/cpc_sketch_impl.hpp#L189) | | Theta | Ignores it | Hashes and updates | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/theta/UpdatableThetaSketch.java#L405), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/theta/include/theta_sketch_impl.hpp#L203) | | Tuple keys | Ignores them | Hashes and updates | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/tuple/UpdatableTupleSketch.java#L184), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/tuple/include/tuple_sketch_impl.hpp#L213) | | Bloom | Hashes and inserts | Ignores it | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/filters/bloomfilter/BloomFilter.java#L308), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/filters/include/bloom_filter_impl.hpp#L637) | | XOR | Hashes and inserts | No implementation at this revision | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/filters/xorfilter/XorFilterBuilder.java#L131) | | Count-Min V1 | Ignores it | Hashes and updates | [Java](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/count/CountMinSketch.java#L238), [C++](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/count/include/count_min_impl.hpp#L184) | The corresponding string update APIs skip empty strings in both languages. Thus an empty string and its zero-length byte encoding can also behave differently within one implementation. For Bloom, C++ returns `false` for a zero-length byte query before hashing or inspecting the filter. Java performs the query normally. This also applies to queries combined with insertion. The serialized bits cannot resolve a query that returns before looking at them. The [Count-Min V2 proposal](https://github.com/apache/datasketches-tck/discussions/18#discussioncomment-19361102) already specifies ignoring empty strings and byte arrays and returning zero for queries. That is a useful explicit contract for V2; the legacy V1 row above describes the checked implementation, not the V2 proposal. ## 3. Signed zero Java's `Double.doubleToLongBits` canonicalizes NaNs but **preserves the sign of zero**: ```text +0.0 -> 0x0000000000000000 -0.0 -> 0x8000000000000000 ``` The Java Bloom and XOR floating-point APIs pass these bits directly to xxHash64. Java HLL, CPC, Theta, and Tuple first normalize zero, effectively doing `value == 0.0 ? 0.0 : value`, then call `Double.doubleToLongBits`. C++ HLL, CPC, Theta, Tuple, and Bloom also normalize signed zero. | Family | Java | C++ | | ----------------------- | ------------------------------- | ---------------------------------- | | HLL, CPC, Theta, Tuple | Maps both zeros to `+0.0` | Maps both zeros to `+0.0` | | Bloom | Preserves the two bit patterns | Maps both zeros to `+0.0` | | XOR | Preserves the two bit patterns | No implementation at this revision | Sources: [Java Bloom](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/filters/bloomfilter/BloomFilter.java#L278), [Java XOR](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/filters/xorfilter/XorFilterBuilder.java#L103), [Java HLL](https://github.com/apache/datasketches-java/blob/ef440a814319f57d165dfbe2c39fc07bf4a0a1e7/src/main/java/org/apache/datasketches/hll/BaseHllSketch.java#L319), and [C++ Bloom](https://github.com/apache/datasketches-cpp/blob/c22f2b1d41497ff2d331e74d1a65be3cd17b1dc3/filters/include/bloom_filter_impl.hpp#L615). In the Bloom reproduction, Java sets the bits for `-0.0`. C++ reads the image, converts a `-0.0` query to `+0.0`, and probes different positions. This is a mismatch in the bytes passed to the hash, not in xxHash64 itself. XOR demonstrates the same Java signed-zero convention, but is not a Java/C++ comparison because there is no C++ XOR implementation at the checked revision. Count-Min V2 does not define floating-point inputs. ## 4. Minimal reproduction Save this as `Repro.java`. It writes the two Bloom images and also shows Java XOR's signed-zero behavior. ```java import java.nio.file.Files; import java.nio.file.Path; import org.apache.datasketches.filters.bloomfilter.BloomFilterBuilder; import org.apache.datasketches.filters.xorfilter.XorFilterBuilder; public class Repro { public static void main(String[] args) throws Exception { Path output = Path.of(args[0]); Files.createDirectories(output); var bytes = BloomFilterBuilder.createBySize(1024, 3, 9001); bytes.update(new byte[0]); System.out.println("Java Bloom empty bytes: " + bytes.query(new byte[0])); Files.write(output.resolve("empty-bytes.sk"), bytes.toByteArray()); var zero = BloomFilterBuilder.createBySize(1024, 3, 9001); zero.update(-0.0); System.out.println("Java Bloom -0.0: " + zero.query(-0.0)); System.out.println("Java Bloom +0.0: " + zero.query(+0.0)); Files.write(output.resolve("negative-zero.sk"), zero.toByteArray()); var xor = new XorFilterBuilder(16, 9001).update(-0.0).build(); System.out.println("Java XOR -0.0: " + xor.query(-0.0)); System.out.println("Java XOR +0.0: " + xor.query(+0.0)); } } ``` Save this as `repro.cpp`. It reads the exact images written by Java. ```cpp #include <fstream> #include <iostream> #include <string> #include "bloom_filter.hpp" int main(int argc, char** argv) { if (argc != 2) return 1; const std::string directory = argv[1]; std::ifstream bytes_input(directory + "/empty-bytes.sk", std::ios::binary); std::ifstream zero_input(directory + "/negative-zero.sk", std::ios::binary); if (!bytes_input || !zero_input) return 1; auto bytes = datasketches::bloom_filter::deserialize(bytes_input); auto zero = datasketches::bloom_filter::deserialize(zero_input); const char backing = 0; // Non-null pointer; the byte input length is zero. std::cout << std::boolalpha << "C++ Bloom empty bytes: " << bytes.query(&backing, 0) << '\n' << "C++ Bloom -0.0: " << zero.query(-0.0) << '\n' << "C++ Bloom +0.0: " << zero.query(+0.0) << '\n'; } ``` Set `JAVA_REPO` and `CPP_REPO` to checkouts of the revisions above. From a separate directory containing the two source files, run: ```sh mkdir -p classes find "$JAVA_REPO/src/main/java" -name '*.java' > sources.txt javac -d classes @sources.txt java --class-path classes Repro.java images c++ -std=c++17 \ -I"$CPP_REPO/common/include" \ -I"$CPP_REPO/filters/include" \ repro.cpp -o repro ./repro images ``` Observed output: ```text Java Bloom empty bytes: true Java Bloom -0.0: true Java Bloom +0.0: false Java XOR -0.0: true Java XOR +0.0: false C++ Bloom empty bytes: false C++ Bloom -0.0: false C++ Bloom +0.0: false ``` The `false` results for the empty-byte and `-0.0` queries in C++ are for items that were actually inserted in Java. Deserialization succeeds in both cases. ## 5. Compatibility and possible next steps I suggest agreeing on the input contract before adapting one implementation to another's current behavior: 1. Specify empty-string, empty-byte, and signed-zero handling for each interoperable family, including both updates and queries. We should say explicitly whether empty byte sequences are valid items, and whether floating-point `-0.0` and `+0.0` represent one item or two. 2. Add TCK cases that query images produced by another language for these boundary inputs. Byte round trips alone would not catch these failures. Include empty strings, empty byte sequences, both signs of zero, and multiple NaN payloads as controls for floating-point normalization. 3. Choose a compatibility plan before changing an existing input encoding. For example, if Java Bloom starts normalizing zero, old images containing only `-0.0` still contain the old hash positions. Simply changing the update/query code would not migrate those images. Conversely, changing a reader's empty-input policy changes what it can query in existing images. We should decide whether explicit legacy handling, a format/version distinction, or a documented limitation is needed. The Rust PR linked above documents input strategies and empty-input handling; it does not change runtime hashing behavior. Rust's existing `canonical_float` continues to normalize both signed zero and NaNs. ## 6. Questions for the community - Are these input distinctions intentional, or should the cross-language contract align them? - Should empty byte sequences follow the existing empty-string convention for each family, or be treated as real items? Should Bloom and XOR normalize signed zero like HLL, CPC, Theta, and Tuple? - Which compatibility policy should apply to existing serialized images if we change these rules? - Can we capture the agreed behavior in the TCK before making corresponding implementation changes? GitHub link: https://github.com/apache/datasketches-tck/discussions/20 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
