-
Notifications
You must be signed in to change notification settings - Fork 511
Extract DeletionVector logic from PuffinFile #3491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ebyhr
wants to merge
1
commit into
apache:main
Choose a base branch
from
ebyhr:ebi/puffin-refactoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−58
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import math | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyroaring import BitMap, FrozenBitMap | ||
|
|
||
| from pyiceberg.table.puffin import PuffinFile | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pyarrow as pa | ||
|
|
||
| EMPTY_BITMAP = FrozenBitMap() | ||
| MAX_JAVA_SIGNED = int(math.pow(2, 31)) - 1 | ||
| PROPERTY_REFERENCED_DATA_FILE = "referenced-data-file" | ||
|
|
||
|
|
||
| class DeletionVector: | ||
| _deletion_vectors: dict[str, list[BitMap]] | ||
|
|
||
| def __init__(self, puffin: bytes) -> None: | ||
| puffin_file = PuffinFile(puffin) | ||
| self._deletion_vectors = { | ||
| blob.properties[PROPERTY_REFERENCED_DATA_FILE]: self._deserialize_bitmap(puffin_file.get_blob_payload(blob)) | ||
| for blob in puffin_file.footer.blobs | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _deserialize_bitmap(pl: bytes) -> list[BitMap]: | ||
| number_of_bitmaps = int.from_bytes(pl[0:8], byteorder="little") | ||
| pl = pl[8:] | ||
|
|
||
| bitmaps = [] | ||
| last_key = -1 | ||
| for _ in range(number_of_bitmaps): | ||
| key = int.from_bytes(pl[0:4], byteorder="little") | ||
| if key < 0: | ||
| raise ValueError(f"Invalid unsigned key: {key}") | ||
| if key <= last_key: | ||
| raise ValueError("Keys must be sorted in ascending order") | ||
| if key > MAX_JAVA_SIGNED: | ||
| raise ValueError(f"Key {key} is too large, max {MAX_JAVA_SIGNED} to maintain compatibility with Java impl") | ||
| pl = pl[4:] | ||
|
|
||
| while last_key < key - 1: | ||
| bitmaps.append(EMPTY_BITMAP) | ||
| last_key += 1 | ||
|
|
||
| bm = BitMap().deserialize(pl) | ||
| # TODO: Optimize this | ||
| pl = pl[len(bm.serialize()) :] | ||
| bitmaps.append(bm) | ||
|
|
||
| last_key = key | ||
|
|
||
| return bitmaps | ||
|
|
||
| @staticmethod | ||
| def _bitmaps_to_chunked_array(bitmaps: list[BitMap]) -> "pa.ChunkedArray": | ||
| import pyarrow as pa | ||
|
|
||
| return pa.chunked_array([(key_pos << 32) + pos for pos in bitmap] for key_pos, bitmap in enumerate(bitmaps)) | ||
|
|
||
| def to_vector(self) -> dict[str, "pa.ChunkedArray"]: | ||
| return {path: self._bitmaps_to_chunked_array(bitmaps) for path, bitmaps in self._deletion_vectors.items()} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,52 +14,17 @@ | |
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import math | ||
| from typing import TYPE_CHECKING, Literal | ||
|
|
||
| from pydantic import Field | ||
| from pyroaring import BitMap, FrozenBitMap | ||
|
|
||
| from pyiceberg.typedef import IcebergBaseModel | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pyarrow as pa | ||
| pass | ||
|
|
||
| # Short for: Puffin Fratercula arctica, version 1 | ||
| MAGIC_BYTES = b"PFA1" | ||
| EMPTY_BITMAP = FrozenBitMap() | ||
| MAX_JAVA_SIGNED = int(math.pow(2, 31)) - 1 | ||
| PROPERTY_REFERENCED_DATA_FILE = "referenced-data-file" | ||
|
|
||
|
|
||
| def _deserialize_bitmap(pl: bytes) -> list[BitMap]: | ||
| number_of_bitmaps = int.from_bytes(pl[0:8], byteorder="little") | ||
| pl = pl[8:] | ||
|
|
||
| bitmaps = [] | ||
| last_key = -1 | ||
| for _ in range(number_of_bitmaps): | ||
| key = int.from_bytes(pl[0:4], byteorder="little") | ||
| if key < 0: | ||
| raise ValueError(f"Invalid unsigned key: {key}") | ||
| if key <= last_key: | ||
| raise ValueError("Keys must be sorted in ascending order") | ||
| if key > MAX_JAVA_SIGNED: | ||
| raise ValueError(f"Key {key} is too large, max {MAX_JAVA_SIGNED} to maintain compatibility with Java impl") | ||
| pl = pl[4:] | ||
|
|
||
| while last_key < key - 1: | ||
| bitmaps.append(EMPTY_BITMAP) | ||
| last_key += 1 | ||
|
|
||
| bm = BitMap().deserialize(pl) | ||
| # TODO: Optimize this | ||
| pl = pl[len(bm.serialize()) :] | ||
| bitmaps.append(bm) | ||
|
|
||
| last_key = key | ||
|
|
||
| return bitmaps | ||
|
|
||
|
|
||
| class PuffinBlobMetadata(IcebergBaseModel): | ||
|
|
@@ -78,15 +43,9 @@ class Footer(IcebergBaseModel): | |
| properties: dict[str, str] = Field(default_factory=dict) | ||
|
|
||
|
|
||
| def _bitmaps_to_chunked_array(bitmaps: list[BitMap]) -> "pa.ChunkedArray": | ||
| import pyarrow as pa | ||
|
|
||
| return pa.chunked_array([(key_pos << 32) + pos for pos in bitmap] for key_pos, bitmap in enumerate(bitmaps)) | ||
|
|
||
|
|
||
| class PuffinFile: | ||
| footer: Footer | ||
| _deletion_vectors: dict[str, list[BitMap]] | ||
| _payload: bytes | ||
|
|
||
| def __init__(self, puffin: bytes) -> None: | ||
| for magic_bytes in [puffin[:4], puffin[-4:]]: | ||
|
|
@@ -105,12 +64,7 @@ def __init__(self, puffin: bytes) -> None: | |
| footer_payload_size_int = int.from_bytes(puffin[-12:-8], byteorder="little") | ||
|
|
||
| self.footer = Footer.model_validate_json(puffin[-(footer_payload_size_int + 12) : -12]) | ||
| puffin = puffin[8:] | ||
|
|
||
| self._deletion_vectors = { | ||
| blob.properties[PROPERTY_REFERENCED_DATA_FILE]: _deserialize_bitmap(puffin[blob.offset : blob.offset + blob.length]) | ||
| for blob in self.footer.blobs | ||
| } | ||
| self._payload = puffin[8:] | ||
|
|
||
| def to_vector(self) -> dict[str, "pa.ChunkedArray"]: | ||
| return {path: _bitmaps_to_chunked_array(bitmaps) for path, bitmaps in self._deletion_vectors.items()} | ||
|
Comment on lines
-115
to
-116
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a public method, so we'll need to deprecate this. What do you think about having this method call the new |
||
| def get_blob_payload(self, blob: PuffinBlobMetadata) -> bytes: | ||
| return self._payload[blob.offset : blob.offset + blob.length] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we are always expecting a
PuffinFileto be the container format for a DV. Are we keeping the input parameter asbytesinstead of using a puffin file directly to allow it to remain container format (Puffin vs some other future format) agnostic?I'm wondering if splitting out the container specific construction to a separate factory method may make more sense: