proost opened a new issue, #1192: URL: https://github.com/apache/iceberg-cpp/issues/1192
## Goal Read a compact Theta sketch from a Puffin statistics file written by Iceberg Java and verify its distinct-value estimate in C++. ## Current state - Issue: https://github.com/apache/iceberg-cpp/issues/959#issue-5550843688 - Suggestion: https://github.com/apache/iceberg-cpp/pull/1175#issuecomment-7180418707 ## Workflow I propose splitting the work into the following three independently reviewable PRs. I would appreciate feedback on the proposed scope, API, and dependency management before proceeding. Specification clarifications and future integrations can be discussed separately. ## PR 1: ZSTD decompression for Puffin blobs Implement `Decompress(kZstd)` in core as the spec defines it: a single Zstandard frame with the content size present, rejecting frames without it. - Tests read Iceberg Java's fixtures `empty-puffin-uncompressed.bin`, `sample-metric-data-uncompressed.bin` and `sample-metric-data-compressed-zstd.bin`. These fixtures cover the Puffin container and compression only; their blobs are not theta sketches. - LZ4 stays out of scope; Iceberg Java does not implement it either. ## PR 2: Theta sketch fixtures written by Iceberg Java Add Puffin files with `apache-datasketches-theta-v1` blobs, written by Iceberg Java, as test inputs for PR 3. Iceberg Java's own Puffin fixtures contain no theta blobs, so these have to be generated. ### How Generate the files with Iceberg Java 1.12.0 and datasketches-java 6.2.0 by replaying Spark's `ThetaSketchAgg` ### Cases Empty, single item, exact mode, estimation mode, and estimation mode split across four Spark partitions (where the final union trims the merged sketch), each uncompressed and with ZSTD. ### Tests Read each fixture with the existing `PuffinReader` and check the footer metadata. Reading the ZSTD blobs depends on PR 1. ### Questions 1. **Where to keep the fixtures and the generator** Should they live in iceberg-cpp or iceberg-verification? iceberg-verifcation looks correct to me, Because It can be used for cross-language compatability test case. But I'm okay with that iceberg-cpp first and send another PR for iceberg-verfication also fine. 2. **NDV rounding semantics** The Puffin specification represents the ndv property as a non-negative integer, whereas a Theta sketch returns a floating-point estimate. Iceberg Java currently truncates the estimate to a long. However, the specification does not explicitly define a rounding policy. For interoperability tests, I propose matching Iceberg Java's existing behavior. Should we separately clarify the rounding semantics in the Puffin specification before introducing corresponding conformance assertions in iceberg-verification? ## PR 3: Decode and serialize theta sketches with datasketches-cpp Add datasketches-cpp and a small `ThetaSketch` wrapper (sketch below), with tests that: - check `static_cast<int64_t>(Estimate())` against the blob's `ndv` property for every fixture from PR 2, and - round-trip every fixture through `Serialize()` and `Deserialize()` and check that the decoded state (`Estimate()`, `IsEmpty()`, `IsEstimationMode()`) is unchanged. ### Scope Decoding and serialization. Union and a builder are left out until there is a caller, such as computing statistics or updating them incrementally on write. `ThetaSketch` lives in a public header (`data/theta_sketch.h`), like `RoaringPositionBitmap` for deletion vectors. datasketches-cpp stays out of the header behind a pimpl. ### Serialization format `Serialize()` writes an uncompressed, ordered compact sketch (serial version 3), which is the form Iceberg Java writes. The result is a `std::vector<uint8_t>` that goes straight into `puffin::Blob::data`. <details> <summary>API sketch</summary> ```cpp // iceberg_data library (e.g. data/theta_sketch.h). /// \brief An Apache DataSketches Theta sketch as stored in an /// `apache-datasketches-theta-v1` Puffin blob. class ICEBERG_DATA_EXPORT ThetaSketch { public: ~ThetaSketch(); ThetaSketch(ThetaSketch&& other) noexcept; ThetaSketch& operator=(ThetaSketch&& other) noexcept; /// \brief Deserialize a compact Theta sketch from the contents of an /// `apache-datasketches-theta-v1` blob. /// /// \param bytes The blob contents, already decompressed by the Puffin reader. /// \return The sketch, or an error if the serialized /// data is invalid or has an incompatible seed hash. static Result<ThetaSketch> Deserialize(std::span<const std::byte> bytes); /// \brief Return the estimated number of distinct values. double Estimate() const; /// \brief Return true if the sketch is in the empty state. bool IsEmpty() const; /// \brief Return true if the sketch sampled its input. bool IsEstimationMode() const; /// \brief Serialize into the form stored in an `apache-datasketches-theta-v1` /// blob: an uncompressed, ordered compact sketch (serial version 3), which is /// the form Iceberg Java writes. /// /// The bytes may differ from Iceberg Java's in preamble fields that readers /// ignore, such as the unused `p` field, but each implementation decodes the /// other's output to the same sketch. /// /// \return The bytes to put into `puffin::Blob::data`, before any Puffin /// compression. Result<std::vector<uint8_t>> Serialize() const; private: struct Impl; std::unique_ptr<Impl> impl_; explicit ThetaSketch(std::unique_ptr<Impl> impl); }; ``` </details> **Questions** 3. **Where to put datasketches-cpp** I'd like to add it to `iceberg_data` rather than core. My reasons: - iceberg-java keeps the Puffin container, compression and deletion vectors in core, but its theta sketch code lives only in the Spark module. Deletion vectors are needed for correct reads, while table statistics are informational. - Nothing in core needs to decode sketches. Would you prefer core instead? I'm fine either way. 4. **Dependency management and vendoring** I propose following the existing dependency management pattern used for nlohmann_json. Since datasketches-cpp is largely header-only, hiding it behind a pimpl does not eliminate potential ODR violations when downstream applications use a different version, particularly in static builds. Is the existing vendoring policy acceptable for datasketches-cpp or should we adopt a different dependency strategy? 5. **Alpha and QuickSelect sketch families** The Puffin specification explicitly refers to the Alpha update sketch family, which is currently not implemented in datasketches-cpp This does not prevent the proposed decoding and serialization support, since both update sketch families produce the common compact Theta sketch representation. For future statistics generation, however, we may need to clarify whether QuickSelect is an acceptable alternative under the specification. ## Questions about follow-up work: "Optional integration into scan planning" 6. **Intended consumers and follow-up integration** This proposal focuses on reading and decoding existing Puffin-based column statistics. Integration with table statistics metadata, statistics generation, incremental updates, and scan planning is left out of scope for now. Are there specific downstream engines or use cases that the community has in mind for column statistics? Understanding the intended consumers would help guide the API design and prioritize follow-up work. -- 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]
