|
91 | 91 | from pyiceberg.table.metadata import TableMetadata |
92 | 92 | from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder, assign_fresh_sort_order_ids |
93 | 93 | from pyiceberg.table.update import ( |
| 94 | + AddSchemaUpdate, |
| 95 | + AddViewVersionUpdate, |
| 96 | + AssertViewUUID, |
| 97 | + SetCurrentViewVersionUpdate, |
| 98 | + SetLocationUpdate, |
| 99 | + SetPropertiesUpdate, |
94 | 100 | TableRequirement, |
95 | 101 | TableUpdate, |
| 102 | + ViewRequirement, |
| 103 | + ViewUpdate, |
96 | 104 | ) |
97 | 105 | from pyiceberg.typedef import EMPTY_DICT, UTF8, IcebergBaseModel, Identifier, Properties |
98 | 106 | from pyiceberg.types import transform_dict_value_to_str |
@@ -173,6 +181,7 @@ class Endpoints: |
173 | 181 | list_views: str = "namespaces/{namespace}/views" |
174 | 182 | load_view: str = "namespaces/{namespace}/views/{view}" |
175 | 183 | create_view: str = "namespaces/{namespace}/views" |
| 184 | + update_view: str = "namespaces/{namespace}/views/{view}" |
176 | 185 | register_view: str = "namespaces/{namespace}/register-view" |
177 | 186 | drop_view: str = "namespaces/{namespace}/views/{view}" |
178 | 187 | view_exists: str = "namespaces/{namespace}/views/{view}" |
@@ -209,6 +218,7 @@ class Capability: |
209 | 218 | V1_LOAD_VIEW = Endpoint(http_method=HttpMethod.GET, path=f"{API_PREFIX}/{Endpoints.load_view}") |
210 | 219 | V1_VIEW_EXISTS = Endpoint(http_method=HttpMethod.HEAD, path=f"{API_PREFIX}/{Endpoints.view_exists}") |
211 | 220 | V1_CREATE_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.create_view}") |
| 221 | + V1_UPDATE_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.update_view}") |
212 | 222 | V1_REGISTER_VIEW = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.register_view}") |
213 | 223 | V1_DELETE_VIEW = Endpoint(http_method=HttpMethod.DELETE, path=f"{API_PREFIX}/{Endpoints.drop_view}") |
214 | 224 | V1_SUBMIT_TABLE_SCAN_PLAN = Endpoint(http_method=HttpMethod.POST, path=f"{API_PREFIX}/{Endpoints.plan_table_scan}") |
@@ -247,6 +257,7 @@ class Capability: |
247 | 257 | Capability.V1_LIST_VIEWS, |
248 | 258 | Capability.V1_LOAD_VIEW, |
249 | 259 | Capability.V1_CREATE_VIEW, |
| 260 | + Capability.V1_UPDATE_VIEW, |
250 | 261 | Capability.V1_DELETE_VIEW, |
251 | 262 | ) |
252 | 263 | ) |
@@ -414,6 +425,12 @@ class RegisterViewRequest(IcebergBaseModel): |
414 | 425 | metadata_location: str = Field(..., alias="metadata-location") |
415 | 426 |
|
416 | 427 |
|
| 428 | +class CommitViewRequest(IcebergBaseModel): |
| 429 | + identifier: TableIdentifier = Field() |
| 430 | + requirements: tuple[ViewRequirement, ...] = Field(default_factory=tuple) |
| 431 | + updates: tuple[ViewUpdate, ...] = Field(default_factory=tuple) |
| 432 | + |
| 433 | + |
417 | 434 | class ConfigResponse(IcebergBaseModel): |
418 | 435 | defaults: Properties | None = Field(default_factory=dict) |
419 | 436 | overrides: Properties | None = Field(default_factory=dict) |
@@ -1332,6 +1349,75 @@ def create_view( |
1332 | 1349 | view_response = ViewResponse.model_validate_json(response.text) |
1333 | 1350 | return self._response_to_view(self.identifier_to_tuple(identifier), view_response) |
1334 | 1351 |
|
| 1352 | + @override |
| 1353 | + @retry(**_RETRY_ARGS) |
| 1354 | + def replace_view( |
| 1355 | + self, |
| 1356 | + identifier: str | Identifier, |
| 1357 | + schema: Schema | pa.Schema, |
| 1358 | + view_version: ViewVersion, |
| 1359 | + location: str | None = None, |
| 1360 | + properties: Properties = EMPTY_DICT, |
| 1361 | + ) -> View: |
| 1362 | + self._check_endpoint(Capability.V1_UPDATE_VIEW) |
| 1363 | + iceberg_schema = self._convert_schema_if_needed(schema) |
| 1364 | + |
| 1365 | + namespace_and_view = self._split_identifier_for_path(identifier, IdentifierKind.VIEW) |
| 1366 | + if self.table_exists(identifier): |
| 1367 | + raise TableAlreadyExistsError(f"Table with same name already exists: {identifier}") |
| 1368 | + if not self.view_exists(identifier): |
| 1369 | + raise NoSuchViewError(f"View does not exist: {identifier}") |
| 1370 | + |
| 1371 | + current_view = self.load_view(identifier) |
| 1372 | + |
| 1373 | + if location: |
| 1374 | + location = location.rstrip("/") |
| 1375 | + |
| 1376 | + # Check if schema already exists in view metadata by comparing structure |
| 1377 | + schema_id = None |
| 1378 | + for existing_schema in current_view.metadata.schemas: |
| 1379 | + if existing_schema.as_struct() == iceberg_schema.as_struct(): |
| 1380 | + schema_id = existing_schema.schema_id |
| 1381 | + break |
| 1382 | + |
| 1383 | + updates: list[ViewUpdate] = [] |
| 1384 | + if schema_id is None: |
| 1385 | + # Schema not found, add new schema with next schema_id |
| 1386 | + next_schema_id = max((s.schema_id for s in current_view.metadata.schemas), default=0) + 1 |
| 1387 | + schema_to_add = iceberg_schema.model_copy(update={"schema_id": next_schema_id}) |
| 1388 | + updates.append(AddSchemaUpdate(schema_=schema_to_add)) |
| 1389 | + schema_id = next_schema_id |
| 1390 | + |
| 1391 | + fresh_view_version = view_version.model_copy(update={"schema_id": schema_id}) |
| 1392 | + updates.append(AddViewVersionUpdate(view_version=fresh_view_version)) |
| 1393 | + updates.append(SetCurrentViewVersionUpdate(view_version_id=fresh_view_version.version_id)) |
| 1394 | + |
| 1395 | + updates_tuple: tuple[ViewUpdate, ...] = tuple(updates) |
| 1396 | + if location: |
| 1397 | + updates_tuple = updates_tuple + (SetLocationUpdate(location=location),) |
| 1398 | + if properties: |
| 1399 | + updates_tuple = updates_tuple + (SetPropertiesUpdate(updates=properties),) |
| 1400 | + |
| 1401 | + requirements: tuple[ViewRequirement, ...] = (AssertViewUUID(uuid=current_view.metadata.view_uuid),) |
| 1402 | + |
| 1403 | + identifier = current_view.name() |
| 1404 | + view_identifier = TableIdentifier(namespace=identifier[:-1], name=identifier[-1]) |
| 1405 | + request = CommitViewRequest(identifier=view_identifier, requirements=requirements, updates=updates_tuple) |
| 1406 | + |
| 1407 | + serialized_json = request.model_dump_json().encode(UTF8) |
| 1408 | + response = self._session.post( |
| 1409 | + self.url(Endpoints.update_view, **namespace_and_view), |
| 1410 | + data=serialized_json, |
| 1411 | + ) |
| 1412 | + |
| 1413 | + try: |
| 1414 | + response.raise_for_status() |
| 1415 | + except HTTPError as exc: |
| 1416 | + _handle_non_200_response(exc, {409: CommitFailedException, 404: NoSuchViewError}) |
| 1417 | + |
| 1418 | + view_response = ViewResponse.model_validate_json(response.text) |
| 1419 | + return self._response_to_view(self.identifier_to_tuple(identifier), view_response) |
| 1420 | + |
1335 | 1421 | @retry(**_RETRY_ARGS) |
1336 | 1422 | @override |
1337 | 1423 | def register_table(self, identifier: str | Identifier, metadata_location: str, overwrite: bool = False) -> Table: |
|
0 commit comments