Skip to content

Raise for an explicit snapshot_id=0 in inspect.files() on a table without snapshots - #4620

Open
shashank11yadav wants to merge 2 commits into
apache:mainfrom
shashank11yadav:fix/inspect-files-snapshot-id-zero
Open

shashank11yadav wants to merge 2 commits into
apache:mainfrom
shashank11yadav:fix/inspect-files-snapshot-id-zero

Conversation

@shashank11yadav

Copy link
Copy Markdown

Fixes #4619

Rationale for this change

InspectTable._files checked if not snapshot_id and ..., so an explicit snapshot_id=0 was treated the same as "no snapshot id". On a table with no snapshots, files(), data_files() and delete_files() then returned an empty table for snapshot_id=0, instead of raising ValueError: Cannot find snapshot with ID 0 as every other unknown id does.

The fix checks snapshot_id is None, as _get_snapshot already does. Calls without a snapshot_id still return an empty table on a table without snapshots, as the method docstring describes.

Are these changes tested?

Yes. One new test, test_inspect_files_with_snapshot_id_zero_on_table_without_snapshots, in tests/table/test_inspect.py. For all three methods it checks that the default call returns no rows and that snapshot_id=0 raises ValueError.

The new test fails without the fix.

With the fix applied, the new test passes.

Local results on this commit:

  • The tests/table/test_inspect.py file, with the unit marker: pass.
  • The new test on its own: pass.
  • ruff check: pass.
  • ruff format check on the two changed files: pass.
  • mypy on pyiceberg and tests: pass.

make lint (the full pre-commit hook set) was not run, so the lint result above covers only ruff.

Are there any user-facing changes?

Yes, a behaviour change. An explicit snapshot_id=0 on a table with no snapshots now raises ValueError in inspect.files(), data_files() and delete_files(). Before, it returned an empty table. Calls without a snapshot_id are unchanged, and no other call path changes.

Please add the changelog label, since this changes behaviour.

…hout snapshots

0 was treated as "no snapshot id", so the empty-table shortcut hid an
unknown id. Use `is None`, as `_get_snapshot` does.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @shashank11yadav! Nice catch, and good regression test that fails without the fix. Verified locally that the new test fails on the pre-fix code and passes with it, and the full tests/table/test_inspect.py suite is green. Same truthiness mistake that #3998 fixed for scans, so this closes the loop on #4619.

Two nits, both non-blocking:

  • tests/table/test_inspect.py:313: could you use @pytest.mark.parametrize here instead of a loop?
  • pyiceberg/table/inspect.py:886: the _files docstring does not mention that an explicit unknown snapshot_id raises ValueError, unlike entries at pyiceberg/table/inspect.py:166. Maybe a short Raises: block there?

Comment thread tests/table/test_inspect.py Outdated
Comment on lines +313 to +319
def test_inspect_files_with_snapshot_id_zero_on_table_without_snapshots(catalog: InMemoryCatalog) -> None:
tbl = catalog.create_table("default.no_snapshots", Schema(NestedField(1, "x", LongType(), required=False)))

for inspect_files in [tbl.inspect.files, tbl.inspect.data_files, tbl.inspect.delete_files]:
assert inspect_files().num_rows == 0
with pytest.raises(ValueError, match="Cannot find snapshot with ID 0"):
inspect_files(snapshot_id=0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: could you use @pytest.mark.parametrize here instead of a loop?

Suggested change
def test_inspect_files_with_snapshot_id_zero_on_table_without_snapshots(catalog: InMemoryCatalog) -> None:
tbl = catalog.create_table("default.no_snapshots", Schema(NestedField(1, "x", LongType(), required=False)))
for inspect_files in [tbl.inspect.files, tbl.inspect.data_files, tbl.inspect.delete_files]:
assert inspect_files().num_rows == 0
with pytest.raises(ValueError, match="Cannot find snapshot with ID 0"):
inspect_files(snapshot_id=0)
@pytest.mark.parametrize("inspect_files", [InspectTable.files.fget, InspectTable.data_files.fget, InspectTable.delete_files.fget])
def test_inspect_files_with_snapshot_id_zero_on_table_without_snapshots(
catalog: InMemoryCatalog, inspect_files
) -> None:
tbl = catalog.create_table("default.no_snapshots", Schema(NestedField(1, "x", LongType(), required=False)))
assert inspect_files(tbl).num_rows == 0
with pytest.raises(ValueError, match="Cannot find snapshot with ID 0"):
inspect_files(tbl, snapshot_id=0)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 342f45b. I parametrized over the method names and call getattr(tbl.inspect, method), the same way test_inspect_files_of_snapshot_without_manifests does, instead of using the suggestion as written: files, data_files and delete_files are plain methods rather than properties, so .fget isn't there, and they need tbl.inspect as self, not tbl. It's now three cases, [files], [data_files] and [delete_files], and all three fail without the fix.

…r in _files

Split the test over files, data_files and delete_files with
pytest.mark.parametrize, as test_inspect_files_of_snapshot_without_manifests
does, and add a Raises section to the _files docstring.
@shashank11yadav

Copy link
Copy Markdown
Author

Nice catch, and good regression test that fails without the fix.

Thanks for the review. I added a Raises: section to the _files docstring in 342f45b, worded for an explicit snapshot id that doesn't exist. I left out the "no snapshots and snapshot_id is None" half that entries has, because _files returns an empty table in that case. The parametrize nit is answered in its thread. I can add the same Raises: note to the public files, data_files and delete_files docstrings if you'd like.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inspect.files(snapshot_id=0) returns a table instead of raising on a table with no snapshots

2 participants