Skip to content

Fix TableScan.update to exclude cached properties #2178

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

Merged
merged 6 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,13 @@ def _parse_row_filter(expr: Union[str, BooleanExpression]) -> BooleanExpression:
return parser.parse(expr) if isinstance(expr, str) else expr


def _get_cached_property_names(cls: Any) -> Set[str]:
"""Return a set of all cached property names defined on the class."""
from inspect import getmembers

return {name for name, attr in getmembers(cls) if isinstance(attr, cached_property)}


S = TypeVar("S", bound="TableScan", covariant=True)


Expand Down Expand Up @@ -1691,7 +1698,13 @@ def to_polars(self) -> pl.DataFrame: ...

def update(self: S, **overrides: Any) -> S:
"""Create a copy of this table scan with updated fields."""
return type(self)(**{**self.__dict__, **overrides})
data = {**self.__dict__, **overrides}

# Cached properties are also stored in the __dict__, so must be removed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that normal methods annotated with property (not cached_property) aren't stored in dict.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is the nicest solution, though it feels like a minimally viable one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #2178 (comment), I'm no longer convinced

for cached_prop in _get_cached_property_names(self.__class__):
data.pop(cached_prop, None)

return type(self)(**data)

def use_ref(self: S, name: str) -> S:
if self.snapshot_id:
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/test_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,16 @@ def test_scan_with_datetime(catalog: Catalog) -> None:

df = table.scan(row_filter=LessThan("datetime", yesterday)).to_pandas()
assert len(df) == 0


@pytest.mark.integration
@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")])
def test_filter_after_arrow_scan(catalog: Catalog) -> None:
identifier = "test_partitioned_by_hours"
table = catalog.load_table(f"default.{identifier}")

scan = table.scan()
assert len(scan.to_arrow()) > 0

scan = scan.filter("ts >= '2023-03-05T00:00:00+00:00'")
assert len(scan.to_arrow()) > 0