diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index d9496f5711..449c087724 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -377,14 +377,31 @@ def _user_agent() -> str: } +class ReadRestrictions(IcebergBaseModel): + required_column_projections: list[dict[str, Any]] = Field(alias="required-column-projections", default_factory=list) + required_row_filter: dict[str, Any] | None = Field(alias="required-row-filter", default=None) + + def is_empty(self) -> bool: + return not self.required_column_projections and self.required_row_filter is None + + class TableResponse(IcebergBaseModel): metadata_location: str | None = Field(alias="metadata-location", default=None) metadata: TableMetadata config: NullableProperties = Field(default_factory=dict) storage_credentials: NullableList[StorageCredential] = Field(alias="storage-credentials", default_factory=list) + read_restrictions: ReadRestrictions | None = Field(alias="read-restrictions", default=None) labels: Labels = Field(default_factory=Labels) +def _check_read_restrictions(table_response: TableResponse) -> None: + if table_response.read_restrictions is not None and not table_response.read_restrictions.is_empty(): + raise NotImplementedError( + "The catalog returned read restrictions that PyIceberg cannot apply. " + "Refusing to load the table to avoid returning unrestricted data." + ) + + class LoadCredentialsResponse(IcebergBaseModel): storage_credentials: list[StorageCredential] = Field(alias="storage-credentials") @@ -1185,6 +1202,7 @@ def add_headers(self, request: PreparedRequest, **kwargs: Any) -> None: # pylin session.mount(self.uri, SigV4Adapter(**self.properties)) def _response_to_table(self, identifier_tuple: tuple[str, ...], table_response: TableResponse) -> Table: + _check_read_restrictions(table_response) # Per Iceberg spec: storage-credentials take precedence over config credential_config = self._resolve_storage_credentials( table_response.storage_credentials, table_response.metadata_location @@ -1204,6 +1222,7 @@ def _response_to_table(self, identifier_tuple: tuple[str, ...], table_response: ) def _response_to_staged_table(self, identifier_tuple: tuple[str, ...], table_response: TableResponse) -> StagedTable: + _check_read_restrictions(table_response) # Per Iceberg spec: storage-credentials take precedence over config credential_location = table_response.metadata_location or f"{table_response.metadata.location.rstrip('/')}/" credential_config = self._resolve_storage_credentials(table_response.storage_credentials, credential_location) diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py index e7787b62bc..a7674d0d52 100644 --- a/tests/catalog/test_rest.py +++ b/tests/catalog/test_rest.py @@ -1616,6 +1616,56 @@ def test_load_table_200(rest_mock: Mocker, example_table_metadata_with_snapshot_ assert actual == expected +def test_load_table_with_read_restrictions_fails_closed( + rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any] +) -> None: + rest_mock.get( + f"{TEST_URI}v1/namespaces/fokko/tables/table", + json={ + **example_table_metadata_with_snapshot_v1_rest_json, + "read-restrictions": {"required-column-projections": [{"field-id": 1, "action": "replace-with-null"}]}, + }, + status_code=200, + request_headers=TEST_HEADERS, + ) + + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + with pytest.raises(NotImplementedError, match="read restrictions"): + catalog.load_table(("fokko", "table")) + + +def test_load_table_with_required_row_filter_fails_closed( + rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any] +) -> None: + rest_mock.get( + f"{TEST_URI}v1/namespaces/fokko/tables/table", + json={ + **example_table_metadata_with_snapshot_v1_rest_json, + "read-restrictions": {"required-row-filter": {"type": "eq", "term": {"field-id": 1}, "value": 1}}, + }, + status_code=200, + request_headers=TEST_HEADERS, + ) + + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + with pytest.raises(NotImplementedError, match="read restrictions"): + catalog.load_table(("fokko", "table")) + + +def test_load_table_with_empty_read_restrictions( + rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any] +) -> None: + rest_mock.get( + f"{TEST_URI}v1/namespaces/fokko/tables/table", + json={**example_table_metadata_with_snapshot_v1_rest_json, "read-restrictions": {}}, + status_code=200, + request_headers=TEST_HEADERS, + ) + + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + assert catalog.load_table(("fokko", "table")).name() == ("fokko", "table") + + def test_load_table_200_loading_mode( rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any] ) -> None: