Skip to content

Commit ea139cc

Browse files
rambleraptorclaudekevinjqliuCopilot
authored
Add rename_view to REST Catalog (#4081)
* just need to add abstract methods * add abstract methods to additional classes * Add namespace check on rename_view * Added test for namespace exists * rebase * PR comments * Address review comments on rename_view - Add @OverRide to RestCatalog/SqlCatalog rename_view and restore it on BigQueryMetastoreCatalog.load_namespace_properties - Split identifiers once in RestCatalog.rename_view - Fix "fully classified" typo in the Catalog.rename_view docstring - Add a cross-namespace rename integration test - Drop a stale skip on test_rest_custom_namespace_separator reintroduced by rebase Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Align rename_view ordering with sibling methods Put @Retry before @OverRide as in rename_table and drop_view, group SqlCatalog.rename_view with the other view stubs, and move the rename_view unit tests next to the other view tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Document rename_view and extend its test coverage Add a "Rename a view" docs section, list the errors rename_view raises in its docstring, assert view-endpoints-supported enables V1_RENAME_VIEW, and cover rename_view in the special-character identifiers integration test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Kevin Liu <kevin.jq.liu@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e2c1e78 commit ea139cc

11 files changed

Lines changed: 241 additions & 1 deletion

File tree

‎mkdocs/docs/api.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,17 @@ To list views in the `default` namespace:
17461746
catalog.list_views("default")
17471747
```
17481748

1749+
## Rename a view
1750+
1751+
To rename a view:
1752+
1753+
```python
1754+
catalog.rename_view(
1755+
from_identifier="default.some_view",
1756+
to_identifier="default.some_view_renamed"
1757+
)
1758+
```
1759+
17491760
## Drop a view
17501761

17511762
To drop a view:

‎pyiceberg/catalog/__init__.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,20 @@ def create_view(
744744
ViewAlreadyExistsError: If a view with the name already exists.
745745
"""
746746

747+
@abstractmethod
748+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
749+
"""Rename a fully qualified view name.
750+
751+
Args:
752+
from_identifier (str | Identifier): Existing view identifier.
753+
to_identifier (str | Identifier): New view identifier.
754+
755+
Raises:
756+
NoSuchViewError: If a view with the name does not exist.
757+
ViewAlreadyExistsError: If a table or view with the target name already exists.
758+
NoSuchNamespaceError: If the source or destination namespace does not exist.
759+
"""
760+
747761
@staticmethod
748762
def identifier_to_tuple(identifier: str | Identifier) -> Identifier:
749763
"""Parse an identifier to a tuple.

‎pyiceberg/catalog/bigquery_metastore.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ def view_exists(self, identifier: str | Identifier) -> bool:
333333
def load_view(self, identifier: str | Identifier) -> View:
334334
raise NotImplementedError
335335

336+
@override
337+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
338+
raise NotImplementedError
339+
336340
@override
337341
def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
338342
dataset_name = self.identifier_to_database(namespace, NoSuchNamespaceError)

‎pyiceberg/catalog/dynamodb.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ def view_exists(self, identifier: str | Identifier) -> bool:
584584
def load_view(self, identifier: str | Identifier) -> View:
585585
raise NotImplementedError
586586

587+
@override
588+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
589+
raise NotImplementedError
590+
587591
def _get_iceberg_table_item(self, database_name: str, table_name: str) -> dict[str, Any]:
588592
try:
589593
return self._get_dynamo_item(identifier=f"{database_name}.{table_name}", namespace=database_name)

‎pyiceberg/catalog/glue.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,10 @@ def view_exists(self, identifier: str | Identifier) -> bool:
10161016
def load_view(self, identifier: str | Identifier) -> View:
10171017
raise NotImplementedError
10181018

1019+
@override
1020+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
1021+
raise NotImplementedError
1022+
10191023
@staticmethod
10201024
def __is_iceberg_table(table: "TableTypeDef") -> bool:
10211025
return table.get("Parameters", {}).get(TABLE_TYPE, "").lower() == ICEBERG

‎pyiceberg/catalog/hive.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ def view_exists(self, identifier: str | Identifier) -> bool:
506506
def load_view(self, identifier: str | Identifier) -> View:
507507
raise NotImplementedError
508508

509+
@override
510+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
511+
raise NotImplementedError
512+
509513
def _create_lock_request(self, database_name: str, table_name: str) -> LockRequest:
510514
# Iceberg commits are not executed within a Hive transaction, so the lock component uses operationType=NO_TXN.
511515
# Setting it explicitly also matters for Hive 2.1.0, which rejects a lock component left at the default UNSET

‎pyiceberg/catalog/noop.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,7 @@ def create_view(
175175
@override
176176
def load_view(self, identifier: str | Identifier) -> View:
177177
raise NotImplementedError
178+
179+
@override
180+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
181+
raise NotImplementedError

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Endpoints:
176176
register_view: str = "namespaces/{namespace}/register-view"
177177
drop_view: str = "namespaces/{namespace}/views/{view}"
178178
view_exists: str = "namespaces/{namespace}/views/{view}"
179+
rename_view: str = "views/rename"
179180
plan_table_scan: str = "namespaces/{namespace}/tables/{table}/plan"
180181
# Use plan_id (underscore) for str.format; Capability paths use {plan-id} to match the REST spec.
181182
fetch_planning_result: str = "namespaces/{namespace}/tables/{table}/plan/{plan_id}"
@@ -211,6 +212,7 @@ class Capability:
211212
V1_CREATE_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.create_view}")
212213
V1_REGISTER_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.register_view}")
213214
V1_DELETE_VIEW = Endpoint(http_method=HttpMethod.DELETE, path=f"{API_PREFIX}/{Endpoints.drop_view}")
215+
V1_RENAME_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.rename_view}")
214216
V1_SUBMIT_TABLE_SCAN_PLAN = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.plan_table_scan}")
215217
# Spec advertises {plan-id}; must match ConfigResponse endpoint strings from servers.
216218
V1_FETCH_TABLE_SCAN_PLAN = Endpoint(
@@ -248,6 +250,7 @@ class Capability:
248250
Capability.V1_LOAD_VIEW,
249251
Capability.V1_CREATE_VIEW,
250252
Capability.V1_DELETE_VIEW,
253+
Capability.V1_RENAME_VIEW,
251254
)
252255
)
253256

@@ -1856,6 +1859,29 @@ def drop_view(self, identifier: str | Identifier) -> None:
18561859
except HTTPError as exc:
18571860
_handle_non_200_response(exc, {404: NoSuchViewError})
18581861

1862+
@retry(**_RETRY_ARGS)
1863+
@override
1864+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
1865+
self._check_endpoint(Capability.V1_RENAME_VIEW)
1866+
source = self._split_identifier_for_json(from_identifier)
1867+
destination = self._split_identifier_for_json(to_identifier)
1868+
1869+
# Ensure that namespaces exist on source and destination.
1870+
source_namespace = source["namespace"]
1871+
if not self.namespace_exists(source_namespace):
1872+
raise NoSuchNamespaceError(f"Source namespace does not exist: {source_namespace}")
1873+
1874+
destination_namespace = destination["namespace"]
1875+
if not self.namespace_exists(destination_namespace):
1876+
raise NoSuchNamespaceError(f"Destination namespace does not exist: {destination_namespace}")
1877+
1878+
payload = {"source": source, "destination": destination}
1879+
response = self._session.post(self.url(Endpoints.rename_view), json=payload)
1880+
try:
1881+
response.raise_for_status()
1882+
except HTTPError as exc:
1883+
_handle_non_200_response(exc, {404: NoSuchViewError, 409: ViewAlreadyExistsError})
1884+
18591885
def close(self) -> None:
18601886
"""Close the catalog and release Session connection adapters.
18611887

‎pyiceberg/catalog/sql.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,10 @@ def drop_view(self, identifier: str | Identifier) -> None:
837837
def load_view(self, identifier: str | Identifier) -> View:
838838
raise NotImplementedError
839839

840+
@override
841+
def rename_view(self, from_identifier: str | Identifier, to_identifier: str | Identifier) -> None:
842+
raise NotImplementedError
843+
840844
def close(self) -> None:
841845
"""Close the catalog and release database connections.
842846

‎tests/catalog/test_rest.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
Capability.V1_CREATE_VIEW,
122122
Capability.V1_REGISTER_VIEW,
123123
Capability.V1_DELETE_VIEW,
124+
Capability.V1_RENAME_VIEW,
124125
Capability.V1_SUBMIT_TABLE_SCAN_PLAN,
125126
Capability.V1_TABLE_SCAN_PLAN_TASKS,
126127
]
@@ -2950,6 +2951,125 @@ def test_drop_view_204(rest_mock: Mocker) -> None:
29502951
RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).drop_view(("some_namespace", "some_view"))
29512952

29522953

2954+
def test_rename_view_204(rest_mock: Mocker) -> None:
2955+
from_identifier = ("some_namespace", "old_view")
2956+
to_identifier = ("some_namespace", "new_view")
2957+
rest_mock.head(
2958+
f"{TEST_URI}v1/namespaces/some_namespace",
2959+
status_code=200,
2960+
request_headers=TEST_HEADERS,
2961+
)
2962+
rest_mock.post(
2963+
f"{TEST_URI}v1/views/rename",
2964+
json={
2965+
"source": {"namespace": ["some_namespace"], "name": "old_view"},
2966+
"destination": {"namespace": ["some_namespace"], "name": "new_view"},
2967+
},
2968+
status_code=204,
2969+
request_headers=TEST_HEADERS,
2970+
)
2971+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
2972+
catalog.rename_view(from_identifier, to_identifier)
2973+
assert (
2974+
rest_mock.last_request.text == """{"source": {"namespace": ["some_namespace"], "name": "old_view"}, """
2975+
""""destination": {"namespace": ["some_namespace"], "name": "new_view"}}"""
2976+
)
2977+
2978+
2979+
def test_rename_view_404(rest_mock: Mocker) -> None:
2980+
from_identifier = ("some_namespace", "non_existent_view")
2981+
to_identifier = ("some_namespace", "new_view")
2982+
rest_mock.head(
2983+
f"{TEST_URI}v1/namespaces/some_namespace",
2984+
status_code=200,
2985+
request_headers=TEST_HEADERS,
2986+
)
2987+
rest_mock.post(
2988+
f"{TEST_URI}v1/views/rename",
2989+
json={
2990+
"error": {
2991+
"message": "View does not exist: some_namespace.non_existent_view",
2992+
"type": "NoSuchViewException",
2993+
"code": 404,
2994+
}
2995+
},
2996+
status_code=404,
2997+
request_headers=TEST_HEADERS,
2998+
)
2999+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
3000+
with pytest.raises(NoSuchViewError) as exc_info:
3001+
catalog.rename_view(from_identifier, to_identifier)
3002+
assert "View does not exist: some_namespace.non_existent_view" in str(exc_info.value)
3003+
3004+
3005+
def test_rename_view_409(rest_mock: Mocker) -> None:
3006+
from_identifier = ("some_namespace", "old_view")
3007+
to_identifier = ("some_namespace", "existing_view")
3008+
rest_mock.head(
3009+
f"{TEST_URI}v1/namespaces/some_namespace",
3010+
status_code=200,
3011+
request_headers=TEST_HEADERS,
3012+
)
3013+
rest_mock.post(
3014+
f"{TEST_URI}v1/views/rename",
3015+
json={
3016+
"error": {
3017+
"message": "View already exists: some_namespace.existing_view",
3018+
"type": "ViewAlreadyExistsException",
3019+
"code": 409,
3020+
}
3021+
},
3022+
status_code=409,
3023+
request_headers=TEST_HEADERS,
3024+
)
3025+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
3026+
with pytest.raises(ViewAlreadyExistsError) as exc_info:
3027+
catalog.rename_view(from_identifier, to_identifier)
3028+
assert "View already exists: some_namespace.existing_view" in str(exc_info.value)
3029+
3030+
3031+
def test_rename_view_source_namespace_does_not_exist(rest_mock: Mocker) -> None:
3032+
from_identifier = ("non_existent_namespace", "old_view")
3033+
to_identifier = ("some_namespace", "new_view")
3034+
3035+
rest_mock.head(
3036+
f"{TEST_URI}v1/namespaces/non_existent_namespace",
3037+
status_code=404,
3038+
request_headers=TEST_HEADERS,
3039+
)
3040+
rest_mock.head(
3041+
f"{TEST_URI}v1/namespaces/some_namespace",
3042+
status_code=200,
3043+
request_headers=TEST_HEADERS,
3044+
)
3045+
3046+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
3047+
with pytest.raises(NoSuchNamespaceError) as exc_info:
3048+
catalog.rename_view(from_identifier, to_identifier)
3049+
assert "Source namespace does not exist: ('non_existent_namespace',)" in str(exc_info.value)
3050+
3051+
3052+
def test_rename_view_destination_namespace_does_not_exist(rest_mock: Mocker) -> None:
3053+
from_identifier = ("some_namespace", "old_view")
3054+
to_identifier = ("non_existent_namespace", "new_view")
3055+
3056+
rest_mock.head(
3057+
f"{TEST_URI}v1/namespaces/some_namespace",
3058+
status_code=200,
3059+
request_headers=TEST_HEADERS,
3060+
)
3061+
rest_mock.head(
3062+
f"{TEST_URI}v1/namespaces/non_existent_namespace",
3063+
status_code=404,
3064+
request_headers=TEST_HEADERS,
3065+
)
3066+
3067+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
3068+
with pytest.raises(NoSuchNamespaceError) as exc_info:
3069+
catalog.rename_view(from_identifier, to_identifier)
3070+
assert "Destination namespace does not exist: ('non_existent_namespace',)" in str(exc_info.value)
3071+
3072+
29533073
@mock.patch("google.auth.transport.requests.Request")
29543074
@mock.patch("google.auth.load_credentials_from_file")
29553075
def test_rest_catalog_with_google_credentials_path(
@@ -3311,6 +3431,7 @@ def test_view_endpoints_enabled_with_config(self, requests_mock: Mocker) -> None
33113431
# View endpoints should be supported when enabled
33123432
catalog._check_endpoint(Capability.V1_LIST_VIEWS)
33133433
catalog._check_endpoint(Capability.V1_DELETE_VIEW)
3434+
catalog._check_endpoint(Capability.V1_RENAME_VIEW)
33143435

33153436

33163437
def test_table_uuid_check_on_commit(rest_mock: Mocker, example_table_metadata_v2: dict[str, Any]) -> None:

0 commit comments

Comments
 (0)