forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Dynamic Overwrite #24
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
jqin61
wants to merge
10
commits into
Fokko:fd-add-ability-to-delete-full-data-files
Choose a base branch
from
jqin61:dynamic_overwrite_reapply
base: fd-add-ability-to-delete-full-data-files
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.
+296
−43
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f76e50
dyanmic_overwrite
jqin61 bd25aef
fix unsupported types
jqin61 8f8d41c
fix double type
jqin61 fa5303b
remove printing
jqin61 590ecd1
clean up
jqin61 654cb4e
dynamic overwrite only deletes latest spec
jqin61 356f885
Merge branch 'fd-add-ability-to-delete-full-data-files' into dynamic_…
jqin61 b54e088
merge
jqin61 2e03525
filter applies to all data files
jqin61 a3c9ab8
fix makefile
jqin61 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
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 |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
And, | ||
BooleanExpression, | ||
EqualTo, | ||
IsNull, | ||
Not, | ||
Or, | ||
Reference, | ||
|
@@ -439,6 +440,72 @@ def append(self, df: pa.Table, snapshot_properties: Dict[str, str] = EMPTY_DICT) | |
for data_file in data_files: | ||
update_snapshot.append_data_file(data_file) | ||
|
||
def _build_partition_predicate(self, spec_id: int, delete_partitions: List[Record]) -> BooleanExpression: | ||
partition_spec = self.table_metadata.spec() | ||
schema = self.table_metadata.schema() | ||
partition_fields = [schema.find_field(field.source_id).name for field in partition_spec.fields] | ||
|
||
expr: BooleanExpression = AlwaysFalse() | ||
for partition_record in delete_partitions: | ||
match_partition_expression: BooleanExpression = AlwaysTrue() | ||
|
||
for pos in range(len(partition_fields)): | ||
predicate = ( | ||
EqualTo(Reference(partition_fields[pos]), partition_record[pos]) | ||
if partition_record[pos] is not None | ||
else IsNull(Reference(partition_fields[pos])) | ||
) | ||
match_partition_expression = And(match_partition_expression, predicate) | ||
expr = Or(expr, match_partition_expression) | ||
return expr | ||
|
||
def dynamic_overwrite(self, df: pa.Table, snapshot_properties: Dict[str, str] = EMPTY_DICT) -> None: | ||
""" | ||
Shorthand for adding a table dynamic overwrite with a PyArrow table to the transaction. | ||
|
||
Args: | ||
df: The Arrow dataframe that will be used to overwrite the table | ||
snapshot_properties: Custom properties to be added to the snapshot summary | ||
""" | ||
|
||
try: | ||
import pyarrow as pa | ||
except ModuleNotFoundError as e: | ||
raise ModuleNotFoundError("For writes PyArrow needs to be installed") from e | ||
|
||
if not isinstance(df, pa.Table): | ||
raise ValueError(f"Expected PyArrow table, got: {df}") | ||
|
||
_check_schema_compatible(self._table.schema(), other_schema=df.schema) | ||
|
||
# cast if the two schemas are compatible but not equal | ||
table_arrow_schema = self._table.schema().as_arrow() | ||
if table_arrow_schema != df.schema: | ||
df = df.cast(table_arrow_schema) | ||
|
||
# If dataframe does not have data, there is no need to overwrite | ||
if df.shape[0] == 0: | ||
return | ||
Comment on lines
+479
to
+488
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. At some point we might want to consolidate these checks |
||
|
||
append_snapshot_commit_uuid = uuid.uuid4() | ||
data_files: List[DataFile] = list( | ||
_dataframe_to_data_files( | ||
table_metadata=self._table.metadata, write_uuid=append_snapshot_commit_uuid, df=df, io=self._table.io | ||
) | ||
) | ||
with self.update_snapshot(snapshot_properties=snapshot_properties).delete() as delete_snapshot: | ||
delete_partitions = [data_file.partition for data_file in data_files] | ||
delete_filter = self._build_partition_predicate( | ||
spec_id=self.table_metadata.spec().spec_id, delete_partitions=delete_partitions | ||
) | ||
delete_snapshot.delete_by_predicate(delete_filter) | ||
|
||
with self.update_snapshot(snapshot_properties=snapshot_properties).fast_append( | ||
append_snapshot_commit_uuid | ||
) as append_snapshot: | ||
for data_file in data_files: | ||
append_snapshot.append_data_file(data_file) | ||
|
||
def overwrite( | ||
self, | ||
df: pa.Table, | ||
|
@@ -1436,6 +1503,17 @@ def append(self, df: pa.Table, snapshot_properties: Dict[str, str] = EMPTY_DICT) | |
with self.transaction() as tx: | ||
tx.append(df=df, snapshot_properties=snapshot_properties) | ||
|
||
def dynamic_overwrite(self, df: pa.Table, snapshot_properties: Dict[str, str] = EMPTY_DICT) -> None: | ||
"""Shorthand for dynamic overwriting the table with a PyArrow table. | ||
|
||
Old partitions are auto detected and replaced with data files created for input arrow table. | ||
Args: | ||
df: The Arrow dataframe that will be used to overwrite the table | ||
snapshot_properties: Custom properties to be added to the snapshot summary | ||
""" | ||
with self.transaction() as tx: | ||
tx.dynamic_overwrite(df=df, snapshot_properties=snapshot_properties) | ||
|
||
def overwrite( | ||
self, | ||
df: pa.Table, | ||
|
@@ -3265,9 +3343,13 @@ def __init__(self, transaction: Transaction, io: FileIO, snapshot_properties: Di | |
self._io = io | ||
self._snapshot_properties = snapshot_properties | ||
|
||
def fast_append(self) -> FastAppendFiles: | ||
def fast_append(self, commit_uuid: Optional[uuid.UUID] = None) -> FastAppendFiles: | ||
return FastAppendFiles( | ||
operation=Operation.APPEND, transaction=self._transaction, io=self._io, snapshot_properties=self._snapshot_properties | ||
operation=Operation.APPEND, | ||
transaction=self._transaction, | ||
io=self._io, | ||
snapshot_properties=self._snapshot_properties, | ||
commit_uuid=commit_uuid, | ||
) | ||
|
||
def overwrite(self, commit_uuid: Optional[uuid.UUID] = None) -> OverwriteFiles: | ||
|
Oops, something went wrong.
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.
Nice!