Skip to content

Commit da70e10

Browse files
authored
Fix StaticTable._metadata_location_from_version_hint metadata location format ERROR (#2609)
<!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> # Rationale for this change To fix StaticTable._metadata_location_from_version_hint metadata location format ERROR: ```python 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) else: return os.path.join(metadata_location, "metadata", "%s.metadata.json").format(content) ``` Closes #2608 ## Are these changes tested? Yes. Added 2 more cases with numeric and non numeric metadata version hints. ## Are there any user-facing changes? No <!-- In the case of user-facing changes, please add the changelog label. -->
1 parent cc14158 commit da70e10

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

pyiceberg/table/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,9 @@ def _metadata_location_from_version_hint(cls, metadata_location: str, properties
16241624
if content.endswith(".metadata.json"):
16251625
return os.path.join(metadata_location, "metadata", content)
16261626
elif content.isnumeric():
1627-
return os.path.join(metadata_location, "metadata", "v%s.metadata.json").format(content)
1627+
return os.path.join(metadata_location, "metadata", f"v{content}.metadata.json")
16281628
else:
1629-
return os.path.join(metadata_location, "metadata", "%s.metadata.json").format(content)
1629+
return os.path.join(metadata_location, "metadata", f"{content}.metadata.json")
16301630

16311631
@classmethod
16321632
def from_metadata(cls, metadata_location: str, properties: Properties = EMPTY_DICT) -> StaticTable:

tests/conftest.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,22 +1135,43 @@ def example_table_metadata_v3() -> Dict[str, Any]:
11351135
return EXAMPLE_TABLE_METADATA_V3
11361136

11371137

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

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

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

11511151
return str(tmp_path_factory.getbasetemp())
11521152

11531153

1154+
@pytest.fixture(scope="session")
1155+
def table_location_with_version_hint_full(tmp_path_factory: pytest.TempPathFactory) -> str:
1156+
content_in_version_hint = str(uuid.uuid4())
1157+
metadata_filename = f"{content_in_version_hint}.metadata.json"
1158+
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)
1159+
1160+
1161+
@pytest.fixture(scope="session")
1162+
def table_location_with_version_hint_numeric(tmp_path_factory: pytest.TempPathFactory) -> str:
1163+
content_in_version_hint = "1234567890"
1164+
metadata_filename = f"v{content_in_version_hint}.metadata.json"
1165+
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)
1166+
1167+
1168+
@pytest.fixture(scope="session")
1169+
def table_location_with_version_hint_non_numeric(tmp_path_factory: pytest.TempPathFactory) -> str:
1170+
content_in_version_hint = "non_numberic"
1171+
metadata_filename = f"{content_in_version_hint}.metadata.json"
1172+
return generate_table_location_with_version_hint(tmp_path_factory, content_in_version_hint, metadata_filename)
1173+
1174+
11541175
@pytest.fixture(scope="session")
11551176
def metadata_location(tmp_path_factory: pytest.TempPathFactory) -> str:
11561177
from pyiceberg.io.pyarrow import PyArrowFileIO

tests/table/test_init.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,20 @@ def test_static_table_gz_same_as_table(table_v2: Table, metadata_location_gz: st
356356
assert static_table.metadata == table_v2.metadata
357357

358358

359-
def test_static_table_version_hint_same_as_table(table_v2: Table, table_location: str) -> None:
360-
static_table = StaticTable.from_metadata(table_location)
361-
assert isinstance(static_table, Table)
362-
assert static_table.metadata == table_v2.metadata
359+
def test_static_table_version_hint_same_as_table(
360+
table_v2: Table,
361+
table_location_with_version_hint_full: str,
362+
table_location_with_version_hint_numeric: str,
363+
table_location_with_version_hint_non_numeric: str,
364+
) -> None:
365+
for table_location in [
366+
table_location_with_version_hint_full,
367+
table_location_with_version_hint_numeric,
368+
table_location_with_version_hint_non_numeric,
369+
]:
370+
static_table = StaticTable.from_metadata(table_location)
371+
assert isinstance(static_table, Table)
372+
assert static_table.metadata == table_v2.metadata
363373

364374

365375
def test_static_table_io_does_not_exist(metadata_location: str) -> None:

0 commit comments

Comments
 (0)