Skip to content
Open
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
8 changes: 8 additions & 0 deletions pyiceberg/catalog/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,14 @@ def view_exists(self, identifier: str | Identifier) -> bool:
Returns:
bool: True if the view exists, False otherwise.
"""
# fallback in order to work with older rest catalog implementations
if Capability.V1_VIEW_EXISTS not in self._supported_endpoints:
try:
self.load_view(identifier)
Comment thread
kevinjqliu marked this conversation as resolved.
return True
except NoSuchViewError:
return False

response = self._session.head(
self.url(Endpoints.view_exists, prefixed=True, **self._split_identifier_for_path(identifier, IdentifierKind.VIEW)),
)
Expand Down
47 changes: 47 additions & 0 deletions tests/catalog/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,53 @@ def test_view_exists_multilevel_namespace_404(rest_mock: Mocker) -> None:
assert not catalog.view_exists((multilevel_namespace, view))


def test_view_exists_fallback_200(requests_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None:
requests_mock.get(
f"{TEST_URI}v1/config",
json={"defaults": {}, "overrides": {}, "endpoints": [str(Capability.V1_LOAD_VIEW)]},
status_code=200,
)
requests_mock.get(
f"{TEST_URI}v1/namespaces/fokko/views/view",
json=example_view_metadata_rest_json,
status_code=200,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert catalog.view_exists(("fokko", "view"))


def test_view_exists_fallback_200_legacy_server(requests_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None:
requests_mock.get(
f"{TEST_URI}v1/config",
json={"defaults": {"view-endpoints-supported": "true"}, "overrides": {}},
status_code=200,
)
requests_mock.get(
f"{TEST_URI}v1/namespaces/fokko/views/view",
json=example_view_metadata_rest_json,
status_code=200,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert catalog.view_exists(("fokko", "view"))


def test_view_exists_fallback_404(requests_mock: Mocker) -> None:
requests_mock.get(
f"{TEST_URI}v1/config",
json={"defaults": {}, "overrides": {}, "endpoints": [str(Capability.V1_LOAD_VIEW)]},
status_code=200,
)
requests_mock.get(
f"{TEST_URI}v1/namespaces/fokko/views/view",
status_code=404,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert not catalog.view_exists(("fokko", "view"))


def test_list_namespaces_200(rest_mock: Mocker) -> None:
rest_mock.get(
f"{TEST_URI}v1/namespaces",
Expand Down
Loading