Skip to content

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
wants to merge 10 commits into
base: fd-add-ability-to-delete-full-data-files
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ test-integration:
docker compose -f dev/docker-compose-integration.yml kill
docker compose -f dev/docker-compose-integration.yml rm -f
docker compose -f dev/docker-compose-integration.yml up -d
sleep 10
sleep 5
docker compose -f dev/docker-compose-integration.yml cp ./dev/provision.py spark-iceberg:/opt/spark/provision.py
docker compose -f dev/docker-compose-integration.yml exec -T spark-iceberg ipython ./provision.py
poetry run pytest tests/ -v -m integration ${PYTEST_ARGS}
poetry run pytest tests/ -v -m integration ${PYTEST_ARGS}

test-integration-rebuild:
docker compose -f dev/docker-compose-integration.yml kill
Expand Down
4 changes: 4 additions & 0 deletions pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ def _(self, _: TimeType) -> Literal[int]:
def _(self, _: TimestampType) -> Literal[int]:
return TimestampLiteral(self.value)

@to.register(TimestamptzType)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

def _(self, _: TimestamptzType) -> Literal[int]:
return TimestampLiteral(self.value)

@to.register(DecimalType)
def _(self, type_var: DecimalType) -> Literal[Decimal]:
unscaled = Decimal(self.value)
Expand Down
86 changes: 84 additions & 2 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
And,
BooleanExpression,
EqualTo,
IsNull,
Not,
Or,
Reference,
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
Loading