-
Notifications
You must be signed in to change notification settings - Fork 473
Add support for registering views #3288
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,7 @@ class Endpoints: | |
| rename_table: str = "tables/rename" | ||
| list_views: str = "namespaces/{namespace}/views" | ||
| create_view: str = "namespaces/{namespace}/views" | ||
| register_view: str = "namespaces/{namespace}/register-view" | ||
| drop_view: str = "namespaces/{namespace}/views/{view}" | ||
| view_exists: str = "namespaces/{namespace}/views/{view}" | ||
| plan_table_scan: str = "namespaces/{namespace}/tables/{table}/plan" | ||
|
|
@@ -181,6 +182,7 @@ class Capability: | |
|
|
||
| V1_LIST_VIEWS = Endpoint(http_method=HttpMethod.GET, path=f"{API_PREFIX}/{Endpoints.list_views}") | ||
| V1_VIEW_EXISTS = Endpoint(http_method=HttpMethod.HEAD, path=f"{API_PREFIX}/{Endpoints.view_exists}") | ||
| V1_REGISTER_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.register_view}") | ||
| V1_DELETE_VIEW = Endpoint(http_method=HttpMethod.DELETE, path=f"{API_PREFIX}/{Endpoints.drop_view}") | ||
| V1_SUBMIT_TABLE_SCAN_PLAN = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.plan_table_scan}") | ||
| V1_TABLE_SCAN_PLAN_TASKS = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.fetch_scan_tasks}") | ||
|
|
@@ -318,6 +320,11 @@ class RegisterTableRequest(IcebergBaseModel): | |
| metadata_location: str = Field(..., alias="metadata-location") | ||
|
|
||
|
|
||
| class RegisterViewRequest(IcebergBaseModel): | ||
| name: str | ||
| metadata_location: str = Field(..., alias="metadata-location") | ||
|
|
||
|
|
||
| class ConfigResponse(IcebergBaseModel): | ||
| defaults: Properties | None = Field(default_factory=dict) | ||
| overrides: Properties | None = Field(default_factory=dict) | ||
|
|
@@ -1312,6 +1319,27 @@ def view_exists(self, identifier: str | Identifier) -> bool: | |
|
|
||
| return False | ||
|
|
||
| @retry(**_RETRY_ARGS) | ||
| def register_view(self, identifier: str | Identifier, metadata_location: str) -> View: | ||
| self._check_endpoint(Capability.V1_REGISTER_VIEW) | ||
| namespace_and_view = self._split_identifier_for_path(identifier) | ||
| request = RegisterViewRequest( | ||
| name=self._identifier_to_validated_tuple(identifier)[-1], | ||
| metadata_location=metadata_location, | ||
| ) | ||
| serialized_json = request.model_dump_json().encode(UTF8) | ||
| response = self._session.post( | ||
| self.url(Endpoints.register_view, namespace=namespace_and_view["namespace"]), | ||
| data=serialized_json, | ||
| ) | ||
| try: | ||
| response.raise_for_status() | ||
| except HTTPError as exc: | ||
| _handle_non_200_response(exc, {409: ViewAlreadyExistsError}) | ||
|
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. Wonder if here we can catch a TableAlreadyExists if there is a table already registered with the same identifier?
Member
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. I agree with this, this behavior is seen in the java impl: This is also what the rest spec defines Following your implementation you're making a register view requests w/o the existence checks which makes it hard to tell if the 409 came from table or view. |
||
|
|
||
| view_response = ViewResponse.model_validate_json(response.text) | ||
| return self._response_to_view(self.identifier_to_tuple(identifier), view_response) | ||
|
|
||
| @retry(**_RETRY_ARGS) | ||
| def drop_view(self, identifier: str) -> None: | ||
| self._check_endpoint(Capability.V1_DELETE_VIEW) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,7 @@ | |
| Capability.V1_REGISTER_TABLE, | ||
| Capability.V1_LIST_VIEWS, | ||
| Capability.V1_VIEW_EXISTS, | ||
| Capability.V1_REGISTER_VIEW, | ||
| Capability.V1_DELETE_VIEW, | ||
| Capability.V1_SUBMIT_TABLE_SCAN_PLAN, | ||
| Capability.V1_TABLE_SCAN_PLAN_TASKS, | ||
|
|
@@ -2122,6 +2123,47 @@ def test_table_identifier_in_commit_table_request( | |
| ) | ||
|
|
||
|
|
||
| def test_register_view_200(rest_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None: | ||
| rest_mock.post( | ||
| f"{TEST_URI}v1/namespaces/default/register-view", | ||
| json=example_view_metadata_rest_json, | ||
| status_code=200, | ||
| request_headers=TEST_HEADERS, | ||
| ) | ||
| catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) | ||
| actual = catalog.register_view( | ||
| identifier=("default", "registered_view"), metadata_location="s3://warehouse/database/view/metadata.json" | ||
| ) | ||
| expected = View( | ||
| identifier=("default", "registered_view"), | ||
| metadata=ViewMetadata(**example_view_metadata_rest_json["metadata"]), | ||
| ) | ||
| assert actual.metadata.model_dump() == expected.metadata.model_dump() | ||
|
Member
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. You can follow the test from |
||
| assert actual.name() == expected.name() | ||
|
|
||
|
|
||
| def test_register_view_409(rest_mock: Mocker) -> None: | ||
| rest_mock.post( | ||
| f"{TEST_URI}v1/namespaces/default/register-view", | ||
| json={ | ||
| "error": { | ||
| "message": "View already exists: default.view in warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e", | ||
| "type": "AlreadyExistsException", | ||
| "code": 409, | ||
| } | ||
| }, | ||
| status_code=409, | ||
| request_headers=TEST_HEADERS, | ||
| ) | ||
|
|
||
| catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) | ||
| with pytest.raises(ViewAlreadyExistsError) as e: | ||
| catalog.register_view( | ||
| identifier=("default", "registered_view"), metadata_location="s3://warehouse/database/view/metadata.json" | ||
| ) | ||
| assert "View already exists" in str(e.value) | ||
|
|
||
|
|
||
| def test_drop_view_invalid_namespace(rest_mock: Mocker) -> None: | ||
| view = "view" | ||
| with pytest.raises(NoSuchIdentifierError) as e: | ||
|
|
||
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.
self._split_identifier_for_path(identifier, IdentifierKind.VIEW)here?Then you can do: