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

Add view_exists method to REST Catalog #1242

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions pyiceberg/catalog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,17 @@ def table_exists(self, identifier: Union[str, Identifier]) -> bool:
bool: True if the table exists, False otherwise.
"""

@abstractmethod
def view_exists(self, identifier: Union[str, Identifier]) -> bool:
"""Check if a view exists.

Args:
identifier (str | Identifier): View identifier.

Returns:
bool: True if the view exists, False otherwise.
"""

@abstractmethod
def register_table(self, identifier: Union[str, Identifier], metadata_location: str) -> Table:
"""Register a new table using existing metadata.
Expand Down
3 changes: 3 additions & 0 deletions pyiceberg/catalog/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ def list_views(self, namespace: Union[str, Identifier]) -> List[Identifier]:
def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError

def _get_iceberg_table_item(self, database_name: str, table_name: str) -> Dict[str, Any]:
try:
return self._get_dynamo_item(identifier=f"{database_name}.{table_name}", namespace=database_name)
Expand Down
3 changes: 3 additions & 0 deletions pyiceberg/catalog/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ def list_views(self, namespace: Union[str, Identifier]) -> List[Identifier]:
def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError

@staticmethod
def __is_iceberg_table(table: TableTypeDef) -> bool:
return table["Parameters"] is not None and table["Parameters"][TABLE_TYPE].lower() == ICEBERG
3 changes: 3 additions & 0 deletions pyiceberg/catalog/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ def register_table(self, identifier: Union[str, Identifier], metadata_location:
def list_views(self, namespace: Union[str, Identifier]) -> List[Identifier]:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError

def _create_lock_request(self, database_name: str, table_name: str) -> LockRequest:
lock_component: LockComponent = LockComponent(
level=LockLevel.TABLE, type=LockType.EXCLUSIVE, dbname=database_name, tablename=table_name, isTransactional=True
Expand Down
3 changes: 3 additions & 0 deletions pyiceberg/catalog/noop.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,8 @@ def update_namespace_properties(
def list_views(self, namespace: Union[str, Identifier]) -> List[Identifier]:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError

def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError
29 changes: 29 additions & 0 deletions pyiceberg/catalog/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Endpoints:
rename_table: str = "tables/rename"
list_views: str = "namespaces/{namespace}/views"
drop_view: str = "namespaces/{namespace}/views/{view}"
view_exists: str = "namespaces/{namespace}/views/{view}"


class IdentifierKind(Enum):
Expand Down Expand Up @@ -890,6 +891,34 @@ def table_exists(self, identifier: Union[str, Identifier]) -> bool:

return False

@retry(**_RETRY_ARGS)
def view_exists(self, identifier: Union[str, Identifier]) -> bool:
"""Check if a view exists.

Args:
identifier (str | Identifier): View identifier.

Returns:
bool: True if the view exists, False otherwise.
"""
identifier_tuple = self._identifier_to_tuple_without_catalog(identifier)
response = self._session.head(
self.url(
Endpoints.view_exists, prefixed=True, **self._split_identifier_for_path(identifier_tuple, IdentifierKind.VIEW)
),
)
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

@retry(**_RETRY_ARGS)
def drop_view(self, identifier: Union[str]) -> None:
identifier_tuple = self.identifier_to_tuple_without_catalog(identifier)
Expand Down
3 changes: 3 additions & 0 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,5 +708,8 @@ def update_namespace_properties(
def list_views(self, namespace: Union[str, Identifier]) -> List[Identifier]:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError

def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError
3 changes: 3 additions & 0 deletions tests/catalog/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ def list_views(self, namespace: Optional[Union[str, Identifier]] = None) -> List
def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError

def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError


@pytest.fixture
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
Expand Down
22 changes: 22 additions & 0 deletions tests/catalog/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,28 @@ def test_list_views_404(rest_mock: Mocker) -> None:
assert "Namespace does not exist" in str(e.value)


def test_view_exists_204(rest_mock: Mocker) -> None:
namespace = "examples"
view = "some_view"
rest_mock.head(
f"{TEST_URI}v1/namespaces/{namespace}/views/{view}",
status_code=204,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert catalog.view_exists((namespace, view))

def test_view_exists_404(rest_mock: Mocker) -> None:
namespace = "examples"
view = "some_view"
rest_mock.head(
f"{TEST_URI}v1/namespaces/{namespace}/views/{view}",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add a test case here for a multi-level namespace as well?

Copy link
Author

Choose a reason for hiding this comment

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

@sungwy thanks! Is that as simple as creating a test case with:

multilevel_namespace = "core.examples.some_namespace"
view = "some_view"
rest_mock.head(
    f"{TEST_URI}v1/namespaces/{multilevel_namespace}/views/{view}",
    status_code=404,
    request_headers=TEST_HEADERS,
)

status_code=404,
request_headers=TEST_HEADERS,
)
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
assert not catalog.view_exists((namespace, view))

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