Skip to content

Commit

Permalink
Allow for missing operation (apache#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko authored Oct 31, 2024
1 parent 8f3e6e5 commit 45d611f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pyiceberg/table/snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import time
import warnings
from collections import defaultdict
from enum import Enum
from typing import TYPE_CHECKING, Any, DefaultDict, Dict, Iterable, List, Mapping, Optional
Expand Down Expand Up @@ -182,7 +183,10 @@ class Summary(IcebergBaseModel, Mapping[str, str]):
operation: Operation = Field()
_additional_properties: Dict[str, str] = PrivateAttr()

def __init__(self, operation: Operation, **data: Any) -> None:
def __init__(self, operation: Optional[Operation] = None, **data: Any) -> None:
if operation is None:
warnings.warn("Encountered invalid snapshot summary: operation is missing, defaulting to overwrite")
operation = Operation.OVERWRITE
super().__init__(operation=operation, **data)
self._additional_properties = data

Expand Down
7 changes: 7 additions & 0 deletions tests/table/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def test_deserialize_snapshot(snapshot: Snapshot) -> None:
assert actual == snapshot


def test_deserialize_snapshot_without_operation(snapshot: Snapshot) -> None:
payload = """{"snapshot-id": 25, "parent-snapshot-id": 19, "sequence-number": 200, "timestamp-ms": 1602638573590, "manifest-list": "s3:/a/b/c.avro", "summary": {}, "schema-id": 3}"""
with pytest.warns(UserWarning, match="Encountered invalid snapshot summary: operation is missing, defaulting to overwrite"):
actual = Snapshot.model_validate_json(payload)
assert actual.summary.operation == Operation.OVERWRITE


def test_deserialize_snapshot_with_properties(snapshot_with_properties: Snapshot) -> None:
payload = """{"snapshot-id":25,"parent-snapshot-id":19,"sequence-number":200,"timestamp-ms":1602638573590,"manifest-list":"s3:/a/b/c.avro","summary":{"operation":"append","foo":"bar"},"schema-id":3}"""
snapshot = Snapshot.model_validate_json(payload)
Expand Down

0 comments on commit 45d611f

Please sign in to comment.