-
Notifications
You must be signed in to change notification settings - Fork 421
Make REST catalog namespace separator configurable #2826
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
Open
rambleraptor
wants to merge
6
commits into
apache:main
Choose a base branch
from
rambleraptor:namespace_configure_rest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48ee1ce
Make REST catalog namespace separator configurable
rambleraptor d3bfb41
PR comments
rambleraptor 72f0ade
Test fixes from PR
rambleraptor 2c616fc
add test
rambleraptor 1559c26
test fix
rambleraptor 56e93b9
lint fixes
rambleraptor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| Any, | ||
| Union, | ||
| ) | ||
| from urllib.parse import quote, unquote | ||
|
|
||
| from pydantic import ConfigDict, Field, field_validator | ||
| from requests import HTTPError, Session | ||
|
|
@@ -227,7 +228,8 @@ class IdentifierKind(Enum): | |
| VIEW_ENDPOINTS_SUPPORTED = "view-endpoints-supported" | ||
| VIEW_ENDPOINTS_SUPPORTED_DEFAULT = False | ||
|
|
||
| NAMESPACE_SEPARATOR = b"\x1f".decode(UTF8) | ||
| NAMESPACE_SEPARATOR_PROPERTY = "namespace-separator" | ||
| DEFAULT_NAMESPACE_SEPARATOR = b"\x1f".decode(UTF8) | ||
|
|
||
|
|
||
| def _retry_hook(retry_state: RetryCallState) -> None: | ||
|
|
@@ -318,7 +320,7 @@ class ListViewsResponse(IcebergBaseModel): | |
| class RestCatalog(Catalog): | ||
| uri: str | ||
| _session: Session | ||
| _supported_endpoints: set[Endpoint] | ||
| _namespace_separator: str | ||
|
|
||
| def __init__(self, name: str, **properties: str): | ||
| """Rest Catalog. | ||
|
|
@@ -478,6 +480,16 @@ def _extract_optional_oauth_params(self) -> dict[str, str]: | |
|
|
||
| return optional_oauth_param | ||
|
|
||
| def _encode_namespace_path(self, namespace: Identifier) -> str: | ||
| """ | ||
| Encode a namespace for use as a path parameter in a URL. | ||
|
|
||
| Each part of the namespace is URL-encoded using `urllib.parse.quote` | ||
| (ensuring characters like '/' are encoded) and then joined by the | ||
| configured namespace separator. | ||
| """ | ||
| return self._namespace_separator.join(quote(part, safe="") for part in namespace) | ||
|
|
||
| def _fetch_config(self) -> None: | ||
| params = {} | ||
| if warehouse_location := self.properties.get(WAREHOUSE_LOCATION): | ||
|
|
@@ -510,6 +522,11 @@ def _fetch_config(self) -> None: | |
| if property_as_bool(self.properties, VIEW_ENDPOINTS_SUPPORTED, VIEW_ENDPOINTS_SUPPORTED_DEFAULT): | ||
| self._supported_endpoints.update(VIEW_ENDPOINTS) | ||
|
|
||
| separator_from_properties = self.properties.get(NAMESPACE_SEPARATOR_PROPERTY, DEFAULT_NAMESPACE_SEPARATOR) | ||
| if not separator_from_properties: | ||
| raise ValueError("Namespace separator cannot be an empty string") | ||
| self._namespace_separator = unquote(separator_from_properties) | ||
|
|
||
| def _identifier_to_validated_tuple(self, identifier: str | Identifier) -> Identifier: | ||
| identifier_tuple = self.identifier_to_tuple(identifier) | ||
| if len(identifier_tuple) <= 1: | ||
|
|
@@ -520,10 +537,17 @@ def _split_identifier_for_path( | |
| self, identifier: str | Identifier | TableIdentifier, kind: IdentifierKind = IdentifierKind.TABLE | ||
| ) -> Properties: | ||
| if isinstance(identifier, TableIdentifier): | ||
| return {"namespace": NAMESPACE_SEPARATOR.join(identifier.namespace.root), kind.value: identifier.name} | ||
| return { | ||
| "namespace": self._encode_namespace_path(tuple(identifier.namespace.root)), | ||
| kind.value: quote(identifier.name, safe=""), | ||
| } | ||
| identifier_tuple = self._identifier_to_validated_tuple(identifier) | ||
|
|
||
| return {"namespace": NAMESPACE_SEPARATOR.join(identifier_tuple[:-1]), kind.value: identifier_tuple[-1]} | ||
| # Use quote to ensure that '/' aren't treated as path separators. | ||
| return { | ||
| "namespace": self._encode_namespace_path(identifier_tuple[:-1]), | ||
| kind.value: quote(identifier_tuple[-1], safe=""), | ||
| } | ||
|
|
||
| def _split_identifier_for_json(self, identifier: str | Identifier) -> dict[str, Identifier | str]: | ||
| identifier_tuple = self._identifier_to_validated_tuple(identifier) | ||
|
|
@@ -741,7 +765,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str) - | |
| def list_tables(self, namespace: str | Identifier) -> list[Identifier]: | ||
| self._check_endpoint(Capability.V1_LIST_TABLES) | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace_concat = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| namespace_concat = self._encode_namespace_path(namespace_tuple) | ||
| response = self._session.get(self.url(Endpoints.list_tables, namespace=namespace_concat)) | ||
| try: | ||
| response.raise_for_status() | ||
|
|
@@ -827,7 +851,7 @@ def list_views(self, namespace: str | Identifier) -> list[Identifier]: | |
| if Capability.V1_LIST_VIEWS not in self._supported_endpoints: | ||
| return [] | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace_concat = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| namespace_concat = self._encode_namespace_path(namespace_tuple) | ||
| response = self._session.get(self.url(Endpoints.list_views, namespace=namespace_concat)) | ||
| try: | ||
| response.raise_for_status() | ||
|
|
@@ -897,7 +921,7 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties = | |
| def drop_namespace(self, namespace: str | Identifier) -> None: | ||
| self._check_endpoint(Capability.V1_DELETE_NAMESPACE) | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| namespace = self._encode_namespace_path(namespace_tuple) | ||
| response = self._session.delete(self.url(Endpoints.drop_namespace, namespace=namespace)) | ||
| try: | ||
| response.raise_for_status() | ||
|
|
@@ -910,7 +934,7 @@ def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]: | |
| namespace_tuple = self.identifier_to_tuple(namespace) | ||
| response = self._session.get( | ||
| self.url( | ||
| f"{Endpoints.list_namespaces}?parent={NAMESPACE_SEPARATOR.join(namespace_tuple)}" | ||
| f"{Endpoints.list_namespaces}?parent={self._encode_namespace_path(namespace_tuple)}" | ||
| if namespace_tuple | ||
| else Endpoints.list_namespaces | ||
| ), | ||
|
|
@@ -926,7 +950,7 @@ def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]: | |
| def load_namespace_properties(self, namespace: str | Identifier) -> Properties: | ||
| self._check_endpoint(Capability.V1_LOAD_NAMESPACE) | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| namespace = self._encode_namespace_path(namespace_tuple) | ||
| response = self._session.get(self.url(Endpoints.load_namespace_metadata, namespace=namespace)) | ||
| try: | ||
| response.raise_for_status() | ||
|
|
@@ -941,7 +965,7 @@ def update_namespace_properties( | |
| ) -> PropertiesUpdateSummary: | ||
| self._check_endpoint(Capability.V1_UPDATE_NAMESPACE) | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| namespace = self._encode_namespace_path(namespace_tuple) | ||
| payload = {"removals": list(removals or []), "updates": updates} | ||
| response = self._session.post(self.url(Endpoints.update_namespace_properties, namespace=namespace), json=payload) | ||
| try: | ||
|
|
@@ -958,14 +982,7 @@ def update_namespace_properties( | |
| @retry(**_RETRY_ARGS) | ||
| def namespace_exists(self, namespace: str | Identifier) -> bool: | ||
| namespace_tuple = self._check_valid_namespace_identifier(namespace) | ||
| namespace = NAMESPACE_SEPARATOR.join(namespace_tuple) | ||
| # fallback in order to work with older rest catalog implementations | ||
| if Capability.V1_NAMESPACE_EXISTS not in self._supported_endpoints: | ||
| try: | ||
| self.load_namespace_properties(namespace_tuple) | ||
| return True | ||
| except NoSuchNamespaceError: | ||
| return False | ||
|
Comment on lines
-962
to
-968
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this intentional? i think we still need this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| namespace = self._encode_namespace_path(namespace_tuple) | ||
|
|
||
| response = self._session.head(self.url(Endpoints.namespace_exists, namespace=namespace)) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious why
quoteis used hereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the namespace separator has a '/' in it? We need to make sure that it isn't interpreted as part of the path.
Added a comment to make this clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we make this a helper function? i can see a scenario where someone will forget to use
quote