ebyhr commented on code in PR #2822:
URL: https://github.com/apache/iceberg-python/pull/2822#discussion_r2604965779
##########
pyiceberg/table/puffin.py:
##########
@@ -114,3 +147,97 @@ def __init__(self, puffin: bytes) -> None:
def to_vector(self) -> dict[str, "pa.ChunkedArray"]:
return {path: _bitmaps_to_chunked_array(bitmaps) for path, bitmaps in
self._deletion_vectors.items()}
+
+
+class PuffinWriter:
+ _blobs: list[PuffinBlobMetadata]
+ _blob_payloads: list[bytes]
+
+ def __init__(self) -> None:
+ self._blobs = []
+ self._blob_payloads = []
+
+ def add(
+ self,
+ positions: Iterable[int],
+ referenced_data_file: str,
+ ) -> None:
+ # 1. Create bitmaps from positions
+ bitmaps: dict[int, BitMap] = {}
+ cardinality = 0
+ for pos in positions:
+ cardinality += 1
+ key = pos >> 32
+ low_bits = pos & 0xFFFFFFFF
+ if key not in bitmaps:
+ bitmaps[key] = BitMap()
+ bitmaps[key].add(low_bits)
+
+ # 2. Serialize bitmaps for the vector payload
+ vector_payload = _serialize_bitmaps(bitmaps)
+
+ # 3. Construct the full blob payload for deletion-vector-v1
+ with io.BytesIO() as blob_payload_buffer:
+ # Magic bytes for DV
+ blob_payload_buffer.write(DELETION_VECTOR_MAGIC)
+ # The vector itself
+ blob_payload_buffer.write(vector_payload)
+
+ # The content for CRC calculation
+ crc_content = blob_payload_buffer.getvalue()
+ crc32 = zlib.crc32(crc_content)
+
+ # The full blob to be stored in the Puffin file
+ with io.BytesIO() as full_blob_buffer:
+ # Combined length of the vector and magic bytes stored as 4
bytes, big-endian
+ full_blob_buffer.write(len(crc_content).to_bytes(4, "big"))
+ # The content (magic + vector)
+ full_blob_buffer.write(crc_content)
+ # A CRC-32 checksum of the magic bytes and serialized vector
as 4 bytes, big-endian
+ full_blob_buffer.write(crc32.to_bytes(4, "big"))
+
+ self._blob_payloads.append(full_blob_buffer.getvalue())
+
+ # 4. Create blob metadata
+ properties = {PROPERTY_REFERENCED_DATA_FILE: referenced_data_file,
"cardinality": str(cardinality)}
+
+ self._blobs.append(
+ PuffinBlobMetadata(
+ type="deletion-vector-v1",
+ fields=[],
Review Comment:
Does this `fields` correspond to `BlobMetadata.inputFields` in Iceberg Java?
Java implementation writes 2147483645 (Integer.MAX_VALUE - 2):
https://github.com/apache/iceberg/blob/c6ba7a4c48cf1280eeff35829492fd7e6a5f6787/core/src/main/java/org/apache/iceberg/deletes/BaseDVFileWriter.java#L158
##########
pyiceberg/table/puffin.py:
##########
@@ -114,3 +147,97 @@ def __init__(self, puffin: bytes) -> None:
def to_vector(self) -> dict[str, "pa.ChunkedArray"]:
return {path: _bitmaps_to_chunked_array(bitmaps) for path, bitmaps in
self._deletion_vectors.items()}
+
+
+class PuffinWriter:
+ _blobs: list[PuffinBlobMetadata]
+ _blob_payloads: list[bytes]
+
+ def __init__(self) -> None:
+ self._blobs = []
+ self._blob_payloads = []
+
+ def add(
+ self,
+ positions: Iterable[int],
+ referenced_data_file: str,
+ ) -> None:
+ # 1. Create bitmaps from positions
+ bitmaps: dict[int, BitMap] = {}
+ cardinality = 0
+ for pos in positions:
+ cardinality += 1
+ key = pos >> 32
+ low_bits = pos & 0xFFFFFFFF
+ if key not in bitmaps:
+ bitmaps[key] = BitMap()
+ bitmaps[key].add(low_bits)
+
+ # 2. Serialize bitmaps for the vector payload
+ vector_payload = _serialize_bitmaps(bitmaps)
+
+ # 3. Construct the full blob payload for deletion-vector-v1
+ with io.BytesIO() as blob_payload_buffer:
+ # Magic bytes for DV
+ blob_payload_buffer.write(DELETION_VECTOR_MAGIC)
+ # The vector itself
+ blob_payload_buffer.write(vector_payload)
+
+ # The content for CRC calculation
+ crc_content = blob_payload_buffer.getvalue()
+ crc32 = zlib.crc32(crc_content)
+
+ # The full blob to be stored in the Puffin file
+ with io.BytesIO() as full_blob_buffer:
+ # Combined length of the vector and magic bytes stored as 4
bytes, big-endian
+ full_blob_buffer.write(len(crc_content).to_bytes(4, "big"))
+ # The content (magic + vector)
+ full_blob_buffer.write(crc_content)
+ # A CRC-32 checksum of the magic bytes and serialized vector
as 4 bytes, big-endian
+ full_blob_buffer.write(crc32.to_bytes(4, "big"))
+
+ self._blob_payloads.append(full_blob_buffer.getvalue())
+
+ # 4. Create blob metadata
+ properties = {PROPERTY_REFERENCED_DATA_FILE: referenced_data_file,
"cardinality": str(cardinality)}
+
+ self._blobs.append(
+ PuffinBlobMetadata(
+ type="deletion-vector-v1",
+ fields=[],
Review Comment:
Does this `fields` correspond to `BlobMetadata.inputFields` in Iceberg Java?
Java implementation writes 2147483645 (Integer.MAX_VALUE - 2) for DV:
https://github.com/apache/iceberg/blob/c6ba7a4c48cf1280eeff35829492fd7e6a5f6787/core/src/main/java/org/apache/iceberg/deletes/BaseDVFileWriter.java#L158
--
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]