Repository navigation
Description
Describe the bug
On a table with no snapshots, table.inspect.files(snapshot_id=0) returns a table instead of raising. Any other unknown id, such as 12345, raises ValueError: Cannot find snapshot with ID 12345. The check at pyiceberg/table/inspect.py:896 is if not snapshot_id and ..., so an explicit 0 is treated as "no snapshot id given". The check is in _files, which files() (inspect.py:921), data_files() (inspect.py:933) and delete_files() (inspect.py:947) all return through. The script below only exercises files().
To reproduce
import sys
from pyiceberg.catalog.noop import NoopCatalog
from pyiceberg.io.pyarrow import PyArrowFileIO
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC
from pyiceberg.schema import Schema
from pyiceberg.table import Table
from pyiceberg.table.metadata import new_table_metadata
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER
from pyiceberg.types import LongType, NestedField
schema = Schema(NestedField(1, "a", LongType(), required=False))
meta = new_table_metadata(schema, UNPARTITIONED_PARTITION_SPEC, UNSORTED_SORT_ORDER, "file:///tmp/t")
tbl = Table(("ns", "t"), meta, "file:///tmp/t/metadata/v1.json", PyArrowFileIO(), NoopCatalog("c"))
assert tbl.metadata.current_snapshot() is None
def probe(sid):
try:
tbl.inspect.files(snapshot_id=sid)
return "returned table"
except ValueError as e:
return f"ValueError: {e}"
r12345 = probe(12345)
r0 = probe(0)
print("EXPECTED: snapshot_id=0 behaves like unknown id 12345 ->", r12345)
print("ACTUAL: snapshot_id=0 ->", r0)
sys.exit(1 if r0 == "returned table" else 0)Output (same on 0.12.0 and on main @ c0448bc):
EXPECTED: snapshot_id=0 behaves like unknown id 12345 -> ValueError: Cannot find snapshot with ID 12345
ACTUAL: snapshot_id=0 -> returned table
Expected behavior
An explicit snapshot_id that isn't in the table raises ValueError, as _get_snapshot does for every other id (inspect.py:91, which checks is not None). A table should only come back for the default call, when no snapshot id is given and the table has no current snapshot.
Versions
- pyiceberg 0.12.0 and
main@ c0448bc - Python 3.12, Linux
Additional context
This is the same truthiness mistake that #3998 fixed for scans. A possible fix is if snapshot_id is None and not self.tbl.metadata.current_snapshot():, with a test that files, data_files and delete_files raise for snapshot_id=0 on an empty table while the default call still returns a table.
I have a fix with a regression test ready and will open a pull request for it right after filing this issue; it will link back here.
Activity