Skip to content
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
12 changes: 9 additions & 3 deletions pyiceberg/table/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from abc import ABC, abstractmethod
from datetime import datetime
from functools import singledispatch
from typing import TYPE_CHECKING, Annotated, Any, Generic, Literal, TypeVar, cast
from typing import TYPE_CHECKING, Annotated, Any, Generic, Literal, TypeVar

from pydantic import Field, field_validator, model_serializer, model_validator

Expand Down Expand Up @@ -181,9 +181,15 @@ class SetStatisticsUpdate(IcebergBaseModel):

@model_validator(mode="before")
def validate_snapshot_id(cls, data: dict[str, Any]) -> dict[str, Any]:
stats = cast(StatisticsFile, data["statistics"])
snapshot_id = None

data["snapshot_id"] = stats.snapshot_id
stats = data["statistics"]
if isinstance(stats, StatisticsFile):
snapshot_id = stats.snapshot_id
elif isinstance(stats, dict):
snapshot_id = stats.get("snapshot_id")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
snapshot_id = stats.get("snapshot_id")
snapshot_id = stats.get("snapshot-id")

i think this should be snapshot-id since before validator takes in json as input

class StatisticsCommonFields(IcebergBaseModel):
"""Common fields between table and partition statistics structs found on metadata."""
snapshot_id: int = Field(alias="snapshot-id")
statistics_path: str = Field(alias="statistics-path")
file_size_in_bytes: int = Field(alias="file-size-in-bytes")
class StatisticsFile(StatisticsCommonFields):

Copy link
Contributor

Choose a reason for hiding this comment

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

could you add a test case for this (and possibly one for the else case too)?

The current test only test the StatisticsFile instance branch

def test_set_statistics_update(table_v2_with_statistics: Table) -> None:
snapshot_id = table_v2_with_statistics.metadata.current_snapshot_id
blob_metadata = BlobMetadata(
type="apache-datasketches-theta-v1",
snapshot_id=snapshot_id,
sequence_number=2,
fields=[1],
properties={"prop-key": "prop-value"},
)
statistics_file = StatisticsFile(


Comment on lines +189 to +191
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
elif isinstance(stats, dict):
snapshot_id = stats.get("snapshot_id")
elif isinstance(stats, dict):
snapshot_id = stats.get("snapshot_id")
else:
snapshot_id = None

nit: i think we can inline the else here

data["snapshot_id"] = snapshot_id

return data

Expand Down
15 changes: 14 additions & 1 deletion tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Any

import pytest
from pydantic import ValidationError
from pydantic import BaseModel, ValidationError
from sortedcontainers import SortedList

from pyiceberg.catalog.noop import NoopCatalog
Expand Down Expand Up @@ -1391,6 +1391,8 @@ def test_set_statistics_update(table_v2_with_statistics: Table) -> None:
statistics=statistics_file,
)

assert model_roundtrips(update)

new_metadata = update_table_metadata(
table_v2_with_statistics.metadata,
(update,),
Expand Down Expand Up @@ -1575,3 +1577,14 @@ def test_add_snapshot_update_updates_next_row_id(table_v3: Table) -> None:

new_metadata = update_table_metadata(table_v3.metadata, (AddSnapshotUpdate(snapshot=new_snapshot),))
assert new_metadata.next_row_id == 11


def model_roundtrips(model: BaseModel) -> bool:
"""Helper assertion that tests if a pydantic model roundtrips
successfully.
"""
__tracebackhide__ = True
model_data = model.model_dump()
if model != type(model).model_validate(model_data):
pytest.fail(f"model {type(model)} did not roundtrip successfully")
return True