HonahX commented on code in PR #139:
URL: https://github.com/apache/iceberg-python/pull/139#discussion_r1401660255
##########
pyiceberg/table/__init__.py:
##########
@@ -350,6 +357,241 @@ class RemovePropertiesUpdate(TableUpdate):
removals: List[str]
+class TableMetadataUpdateContext:
+ updates: List[TableUpdate]
+ last_added_schema_id: Optional[int]
+
+ def __init__(self) -> None:
+ self.updates = []
+ self.last_added_schema_id = None
+
+ def is_added_snapshot(self, snapshot_id: int) -> bool:
+ return any(
+ update.snapshot.snapshot_id == snapshot_id
+ for update in self.updates
+ if update.action == TableUpdateAction.add_snapshot
+ )
+
+ def is_added_schema(self, schema_id: int) -> bool:
+ return any(
+ update.schema_.schema_id == schema_id for update in self.updates
if update.action == TableUpdateAction.add_schema
+ )
+
+
+@singledispatch
+def apply_table_update(update: TableUpdate, base_metadata: TableMetadata,
context: TableMetadataUpdateContext) -> TableMetadata:
+ """Apply a table update to the table metadata.
+
+ Args:
+ update: The update to be applied.
+ base_metadata: The base metadata to be updated.
+ context: Contains previous updates, last_added_snapshot_id and other
change tracking information in the current transaction.
+
+ Returns:
+ The updated metadata.
+
+ """
+ raise NotImplementedError(f"Unsupported table update: {update}")
+
+
+@apply_table_update.register(UpgradeFormatVersionUpdate)
+def _(update: UpgradeFormatVersionUpdate, base_metadata: TableMetadata,
context: TableMetadataUpdateContext) -> TableMetadata:
+ if update.format_version > SUPPORTED_TABLE_FORMAT_VERSION:
+ raise ValueError(f"Unsupported table format version:
{update.format_version}")
+
+ if update.format_version < base_metadata.format_version:
+ raise ValueError(f"Cannot downgrade v{base_metadata.format_version}
table to v{update.format_version}")
+
+ if update.format_version == base_metadata.format_version:
+ return base_metadata
+
+ updated_metadata_data = copy(base_metadata.model_dump())
+ updated_metadata_data["format-version"] = update.format_version
Review Comment:
Thanks for the explanation! I am a little worried about the immutability of
table metadata. I think Pydantic's `frozen` config does not prevent updates to
list, dict, etc. If we make shallow copy of list fields in metadata and later
some code mistakenly alter the list (e.g. append something) in the updated
metadata, the effect will be populated to the base_metadata too and it may be
hard to detect. Do you think this might be a problem?
--
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]