Skip to content

Commit 3cecbe8

Browse files
committed
fix: build StaticTable version-hint metadata locations with forward slashes
StaticTable._metadata_location_from_version_hint built the version-hint and resolved metadata locations with os.path.join. Iceberg locations are URIs and must always be forward-slash separated, but os.path.join uses the OS separator, so on Windows it produced backslash-separated keys such as s3://.../taxis\metadata\version-hint.text. That is an invalid object-store key, which breaks the documented "pass the table root path" usage of StaticTable.from_metadata on Windows. Join the components with explicit forward slashes, matching how locations are built elsewhere in pyiceberg (for example pyiceberg/table/locations.py). Behavior is unchanged on POSIX. The now-unused os import is removed. Add an OS-independent regression test that swaps os.path.join for the Windows join so the metadata location is asserted to stay a valid forward-slash URI on any platform (the existing test only exercised local temp paths, which use the POSIX separator on Linux CI and never caught this). Related: apache#2477
1 parent 03ad0d6 commit 3cecbe8

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎pyiceberg/table/__init__.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import itertools
2020
import logging
21-
import os
2221
import random
2322
import time
2423
import uuid
@@ -1958,19 +1957,20 @@ def refresh(self) -> Table:
19581957

19591958
@classmethod
19601959
def _metadata_location_from_version_hint(cls, metadata_location: str, properties: Properties = EMPTY_DICT) -> str:
1961-
version_hint_location = os.path.join(metadata_location, "metadata", "version-hint.text")
1960+
metadata_dir = f"{metadata_location.rstrip('/')}/metadata"
1961+
version_hint_location = f"{metadata_dir}/version-hint.text"
19621962
io = load_file_io(properties=properties, location=version_hint_location)
19631963
file = io.new_input(version_hint_location)
19641964

19651965
with file.open() as stream:
19661966
content = stream.read().decode("utf-8")
19671967

19681968
if content.endswith(".metadata.json"):
1969-
return os.path.join(metadata_location, "metadata", content)
1969+
return f"{metadata_dir}/{content}"
19701970
elif content.isnumeric():
1971-
return os.path.join(metadata_location, "metadata", f"v{content}.metadata.json")
1971+
return f"{metadata_dir}/v{content}.metadata.json"
19721972
else:
1973-
return os.path.join(metadata_location, "metadata", f"{content}.metadata.json")
1973+
return f"{metadata_dir}/{content}.metadata.json"
19741974

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

‎tests/table/test_init.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,49 @@ def test_static_table_version_hint_same_as_table(
589589
assert static_table.metadata == table_v2.metadata
590590

591591

592+
@pytest.mark.parametrize(
593+
"version_hint_content, expected_metadata_file",
594+
[
595+
("v3.metadata.json", "v3.metadata.json"),
596+
("3", "v3.metadata.json"),
597+
("some-uuid", "some-uuid.metadata.json"),
598+
],
599+
)
600+
def test_static_table_version_hint_location_uses_forward_slashes(
601+
version_hint_content: str, expected_metadata_file: str, monkeypatch: pytest.MonkeyPatch
602+
) -> None:
603+
# Iceberg locations are URIs and must always be forward-slash separated, regardless of the host OS.
604+
table_root = "s3://warehouse/wh/nyc.db/taxis"
605+
requested_locations: list[str] = []
606+
607+
class _FakeStream:
608+
def __enter__(self) -> "_FakeStream":
609+
return self
610+
611+
def __exit__(self, *args: Any) -> None:
612+
return None
613+
614+
def read(self, size: int = 0) -> bytes:
615+
return version_hint_content.encode("utf-8")
616+
617+
class _FakeInputFile:
618+
def open(self, seekable: bool = True) -> "_FakeStream":
619+
return _FakeStream()
620+
621+
class _FakeFileIO:
622+
def new_input(self, location: str) -> "_FakeInputFile":
623+
requested_locations.append(location)
624+
return _FakeInputFile()
625+
626+
monkeypatch.setattr("pyiceberg.table.load_file_io", lambda *args, **kwargs: _FakeFileIO())
627+
628+
resolved_location = StaticTable._metadata_location_from_version_hint(table_root)
629+
630+
assert requested_locations == [f"{table_root}/metadata/version-hint.text"]
631+
assert resolved_location == f"{table_root}/metadata/{expected_metadata_file}"
632+
assert "\\" not in resolved_location
633+
634+
592635
def test_static_table_io_does_not_exist(metadata_location: str) -> None:
593636
with pytest.raises(ValueError):
594637
StaticTable.from_metadata(metadata_location, {PY_IO_IMPL: "pyiceberg.does.not.exist.FileIO"})

0 commit comments

Comments
 (0)