Skip to content
Merged
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
4 changes: 2 additions & 2 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,9 @@ def _metadata_location_from_version_hint(cls, metadata_location: str, properties
if content.endswith(".metadata.json"):
return os.path.join(metadata_location, "metadata", content)
elif content.isnumeric():
return os.path.join(metadata_location, "metadata", "v%s.metadata.json").format(content)
return os.path.join(metadata_location, "metadata", f"v{content}.metadata.json")
else:
return os.path.join(metadata_location, "metadata", "%s.metadata.json").format(content)
return os.path.join(metadata_location, "metadata", f"{content}.metadata.json")

@classmethod
def from_metadata(cls, metadata_location: str, properties: Properties = EMPTY_DICT) -> StaticTable:
Expand Down
29 changes: 25 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,22 +1135,43 @@ def example_table_metadata_v3() -> Dict[str, Any]:
return EXAMPLE_TABLE_METADATA_V3


@pytest.fixture(scope="session")
def table_location(tmp_path_factory: pytest.TempPathFactory) -> str:
def generate_table_location_with_version_hint(
tmp_path_factory: pytest.TempPathFactory, content_in_version_hint: str, metadata_filename: str
) -> str:
from pyiceberg.io.pyarrow import PyArrowFileIO

metadata_filename = f"{uuid.uuid4()}.metadata.json"
metadata_location = str(tmp_path_factory.getbasetemp() / "metadata" / metadata_filename)
version_hint_location = str(tmp_path_factory.getbasetemp() / "metadata" / "version-hint.text")
metadata = TableMetadataV2(**EXAMPLE_TABLE_METADATA_V2)
ToOutputFile.table_metadata(metadata, PyArrowFileIO().new_output(location=metadata_location), overwrite=True)

with PyArrowFileIO().new_output(location=version_hint_location).create(overwrite=True) as s:
s.write(metadata_filename.encode("utf-8"))
s.write(content_in_version_hint.encode("utf-8"))

return str(tmp_path_factory.getbasetemp())


@pytest.fixture(scope="session")
def table_location_with_version_hint_full(tmp_path_factory: pytest.TempPathFactory) -> str:
content_in_version_hint = str(uuid.uuid4())
metadata_filename = f"{content_in_version_hint}.metadata.json"
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)


@pytest.fixture(scope="session")
def table_location_with_version_hint_numeric(tmp_path_factory: pytest.TempPathFactory) -> str:
content_in_version_hint = "1234567890"
metadata_filename = f"v{content_in_version_hint}.metadata.json"
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)


@pytest.fixture(scope="session")
def table_location_with_version_hint_non_numeric(tmp_path_factory: pytest.TempPathFactory) -> str:
content_in_version_hint = "non_numberic"
metadata_filename = f"{content_in_version_hint}.metadata.json"
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)


@pytest.fixture(scope="session")
def metadata_location(tmp_path_factory: pytest.TempPathFactory) -> str:
from pyiceberg.io.pyarrow import PyArrowFileIO
Expand Down
18 changes: 14 additions & 4 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,20 @@ def test_static_table_gz_same_as_table(table_v2: Table, metadata_location_gz: st
assert static_table.metadata == table_v2.metadata


def test_static_table_version_hint_same_as_table(table_v2: Table, table_location: str) -> None:
static_table = StaticTable.from_metadata(table_location)
assert isinstance(static_table, Table)
assert static_table.metadata == table_v2.metadata
def test_static_table_version_hint_same_as_table(
table_v2: Table,
table_location_with_version_hint_full: str,
table_location_with_version_hint_numeric: str,
table_location_with_version_hint_non_numeric: str,
) -> None:
for table_location in [
table_location_with_version_hint_full,
table_location_with_version_hint_numeric,
table_location_with_version_hint_non_numeric,
]:
static_table = StaticTable.from_metadata(table_location)
assert isinstance(static_table, Table)
assert static_table.metadata == table_v2.metadata


def test_static_table_io_does_not_exist(metadata_location: str) -> None:
Expand Down