Skip to content

Commit 0e6ce1d

Browse files
authored
Fix table_exists behavior in the rest catalog (#1096)
1 parent ce1a122 commit 0e6ce1d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pyiceberg/catalog/rest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,4 +834,15 @@ def table_exists(self, identifier: Union[str, Identifier]) -> bool:
834834
response = self._session.head(
835835
self.url(Endpoints.load_table, prefixed=True, **self._split_identifier_for_path(identifier_tuple))
836836
)
837-
return response.status_code in (200, 204)
837+
838+
if response.status_code == 404:
839+
return False
840+
elif response.status_code == 204:
841+
return True
842+
843+
try:
844+
response.raise_for_status()
845+
except HTTPError as exc:
846+
self._handle_non_200_response(exc, {})
847+
848+
return False

tests/catalog/test_rest.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
NoSuchNamespaceError,
3333
NoSuchTableError,
3434
OAuthError,
35+
ServerError,
3536
TableAlreadyExistsError,
3637
)
3738
from pyiceberg.io import load_file_io
@@ -701,17 +702,17 @@ def test_load_table_404(rest_mock: Mocker) -> None:
701702
assert "Table does not exist" in str(e.value)
702703

703704

704-
def test_table_exist_200(rest_mock: Mocker) -> None:
705+
def test_table_exists_200(rest_mock: Mocker) -> None:
705706
rest_mock.head(
706707
f"{TEST_URI}v1/namespaces/fokko/tables/table",
707708
status_code=200,
708709
request_headers=TEST_HEADERS,
709710
)
710711
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
711-
assert catalog.table_exists(("fokko", "table"))
712+
assert not catalog.table_exists(("fokko", "table"))
712713

713714

714-
def test_table_exist_204(rest_mock: Mocker) -> None:
715+
def test_table_exists_204(rest_mock: Mocker) -> None:
715716
rest_mock.head(
716717
f"{TEST_URI}v1/namespaces/fokko/tables/table",
717718
status_code=204,
@@ -721,16 +722,28 @@ def test_table_exist_204(rest_mock: Mocker) -> None:
721722
assert catalog.table_exists(("fokko", "table"))
722723

723724

724-
def test_table_exist_500(rest_mock: Mocker) -> None:
725+
def test_table_exists_404(rest_mock: Mocker) -> None:
725726
rest_mock.head(
726727
f"{TEST_URI}v1/namespaces/fokko/tables/table",
727-
status_code=500,
728+
status_code=404,
728729
request_headers=TEST_HEADERS,
729730
)
730731
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
731732
assert not catalog.table_exists(("fokko", "table"))
732733

733734

735+
def test_table_exists_500(rest_mock: Mocker) -> None:
736+
rest_mock.head(
737+
f"{TEST_URI}v1/namespaces/fokko/tables/table",
738+
status_code=500,
739+
request_headers=TEST_HEADERS,
740+
)
741+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
742+
743+
with pytest.raises(ServerError):
744+
catalog.table_exists(("fokko", "table"))
745+
746+
734747
def test_drop_table_404(rest_mock: Mocker) -> None:
735748
rest_mock.delete(
736749
f"{TEST_URI}v1/namespaces/fokko/tables/does_not_exists",

0 commit comments

Comments
 (0)