diff --git a/pyiceberg/table/inspect.py b/pyiceberg/table/inspect.py index 4feba3d532..167240dcf8 100644 --- a/pyiceberg/table/inspect.py +++ b/pyiceberg/table/inspect.py @@ -138,7 +138,7 @@ def snapshots(self) -> pa.Table: "committed_at": datetime.fromtimestamp(snapshot.timestamp_ms / 1000.0, tz=UTC), "snapshot_id": snapshot.snapshot_id, "parent_id": snapshot.parent_snapshot_id, - "operation": str(operation), + "operation": operation, "manifest_list": snapshot.manifest_list, "summary": additional_properties, } diff --git a/tests/table/test_inspect.py b/tests/table/test_inspect.py index e507b62f71..789ec70b67 100644 --- a/tests/table/test_inspect.py +++ b/tests/table/test_inspect.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -from pathlib import PosixPath +from pathlib import Path, PosixPath from typing import Any import pyarrow as pa @@ -104,6 +104,32 @@ def test_inspect_entries_and_files_with_data_file_without_metrics(catalog: InMem assert files[0]["column_sizes"] is None +def test_inspect_snapshots_operation_is_null_without_summary(catalog: InMemoryCatalog) -> None: + import json + + # The summary is optional in format v1 and holds the operation, so a snapshot without one has no + # operation: the nullable column must hold null, not the text "None" + schema = Schema(NestedField(1, "x", LongType(), required=False)) + arrow_table = pa.table({"x": [1, 2]}, schema=pa.schema([pa.field("x", pa.int64(), nullable=True)])) + + v2 = catalog.create_table("default.v2_snapshot_operation", schema, properties={"format-version": "2"}) + v2.append(arrow_table) + assert v2.inspect.snapshots().column("operation").to_pylist() == ["append"] + + v1 = catalog.create_table("default.v1_snapshot_without_summary", schema, properties={"format-version": "1"}) + v1.append(arrow_table) + metadata_path = Path(v1.metadata_location.removeprefix("file://")) + metadata = json.loads(metadata_path.read_text()) + for snapshot in metadata["snapshots"]: + del snapshot["summary"] + metadata_path.write_text(json.dumps(metadata)) + + v1 = catalog.load_table("default.v1_snapshot_without_summary") + snapshots = v1.inspect.snapshots().to_pylist() + assert [row["operation"] for row in snapshots] == [None] + assert [row["summary"] for row in snapshots] == [None] + + @pytest.mark.parametrize("newest_first", [False, True]) def test_partitions_last_updated_uses_latest_snapshot_regardless_of_order(newest_first: bool) -> None: # Manifest entries are visited in manifest order, which is not chronological, so the