Skip to content

Commit

Permalink
Fix table_exists behavior in the rest catalog (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrluis authored Aug 24, 2024
1 parent ce1a122 commit 0e6ce1d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 12 additions & 1 deletion pyiceberg/catalog/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,15 @@ def table_exists(self, identifier: Union[str, Identifier]) -> bool:
response = self._session.head(
self.url(Endpoints.load_table, prefixed=True, **self._split_identifier_for_path(identifier_tuple))
)
return response.status_code in (200, 204)

if response.status_code == 404:
return False
elif response.status_code == 204:
return True

try:
response.raise_for_status()
except HTTPError as exc:
self._handle_non_200_response(exc, {})

return False
23 changes: 18 additions & 5 deletions tests/catalog/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
NoSuchNamespaceError,
NoSuchTableError,
OAuthError,
ServerError,
TableAlreadyExistsError,
)
from pyiceberg.io import load_file_io
Expand Down Expand Up @@ -701,17 +702,17 @@ def test_load_table_404(rest_mock: Mocker) -> None:
assert "Table does not exist" in str(e.value)


def test_table_exist_200(rest_mock: Mocker) -> None:
def test_table_exists_200(rest_mock: Mocker) -> None:
rest_mock.head(
f"{TEST_URI}v1/namespaces/fokko/tables/table",
status_code=200,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert catalog.table_exists(("fokko", "table"))
assert not catalog.table_exists(("fokko", "table"))


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


def test_table_exist_500(rest_mock: Mocker) -> None:
def test_table_exists_404(rest_mock: Mocker) -> None:
rest_mock.head(
f"{TEST_URI}v1/namespaces/fokko/tables/table",
status_code=500,
status_code=404,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert not catalog.table_exists(("fokko", "table"))


def test_table_exists_500(rest_mock: Mocker) -> None:
rest_mock.head(
f"{TEST_URI}v1/namespaces/fokko/tables/table",
status_code=500,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)

with pytest.raises(ServerError):
catalog.table_exists(("fokko", "table"))


def test_drop_table_404(rest_mock: Mocker) -> None:
rest_mock.delete(
f"{TEST_URI}v1/namespaces/fokko/tables/does_not_exists",
Expand Down

0 comments on commit 0e6ce1d

Please sign in to comment.