Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table_exists behavior in REST catalog #1096

Merged
merged 1 commit into from
Aug 24, 2024
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
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting, the integration tests aren't working?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t have integration tests for the catalog interactions, so I created an issue to implement a test suite: #1010

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, thanks!

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