Skip to content

Commit 3228b17

Browse files
yayahaFokko
andauthored
Expose catalog labels on Table (#4321)
* Parse catalog labels in REST load table and load view responses The REST spec adds an optional `labels` field to LoadTableResult and LoadViewResult for catalog-provided enrichment such as ownership or classification. PyIceberg dropped the field. Add `Labels` and `FieldLabel` models and parse `labels` on `TableResponse` and `ViewResponse`, matching the Java read path (apache/iceberg#18045). * Read non-object labels as empty A string, list, number, or bool `labels` value failed the whole load. Java `LabelsParser` reads keys only under `hasNonNull`, so it returns empty labels for any non-object value. Do the same. * Expose catalog labels on Table Labels parsed from the load response were not reachable from the loaded table. Add `Table.labels`. The REST catalog sets it on load, create, and register. Other catalogs and staged tables (`create_table_transaction`) get empty labels. Labels are not table state: they are not part of equality, and refresh and commits keep them. This matches the Java `SupportsLabels` behavior (apache/iceberg#18046). * Test labels on create, register, and pickle Create and register hand labels to the same `_response_to_table` as load, but only load was tested. Check all three. Also pin that a pickled table keeps its labels, the one intended difference from Java, where `BaseTable.labels` is transient. --------- Co-authored-by: Fokko Driesprong <fokko@apache.org>
1 parent a60c7cc commit 3228b17

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ def _response_to_table(self, identifier_tuple: tuple[str, ...], table_response:
11921192
),
11931193
catalog=self,
11941194
config=table_response.config,
1195+
labels=table_response.labels,
11951196
)
11961197

11971198
def _response_to_staged_table(self, identifier_tuple: tuple[str, ...], table_response: TableResponse) -> StagedTable:

‎pyiceberg/table/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from pyiceberg.schema import Schema
6060
from pyiceberg.table.delete_file_index import DeleteFileIndex
6161
from pyiceberg.table.inspect import InspectTable
62+
from pyiceberg.table.labels import Labels
6263
from pyiceberg.table.locations import LocationProvider, load_location_provider
6364
from pyiceberg.table.maintenance import MaintenanceTable
6465
from pyiceberg.table.metadata import INITIAL_SEQUENCE_NUMBER, TableMetadata
@@ -1349,6 +1350,8 @@ class Table:
13491350
io: FileIO
13501351
catalog: Catalog
13511352
config: dict[str, str]
1353+
# Catalog-provided labels, set when the catalog loads the table. They are not table state, so refresh() and commits keep them.
1354+
labels: Labels
13521355

13531356
def __init__(
13541357
self,
@@ -1359,6 +1362,7 @@ def __init__(
13591362
catalog: Catalog,
13601363
config: dict[str, str] = EMPTY_DICT,
13611364
kms_client: KeyManagementClient | None = None,
1365+
labels: Labels | None = None,
13621366
) -> None:
13631367
self._identifier = identifier
13641368
self.metadata = metadata
@@ -1367,6 +1371,7 @@ def __init__(
13671371
self.catalog = catalog
13681372
self.config = config
13691373
self._kms_client = kms_client
1374+
self.labels = labels if labels is not None else Labels()
13701375
self._encryption_manager: _CachedEncryptionManager | None = None
13711376

13721377
@property

‎tests/catalog/test_rest.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import base64
2121
import os
22+
import pickle
2223
import uuid
2324
from collections.abc import Callable, Iterator
2425
from contextlib import contextmanager
@@ -3835,3 +3836,43 @@ def test_load_responses_parse_labels(
38353836
assert response_type.model_validate(payload).labels == Labels()
38363837
for invalid in invalid_values:
38373838
assert response_type.model_validate({**payload, "labels": invalid}).labels == Labels()
3839+
3840+
3841+
def test_table_labels_from_rest_responses(
3842+
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any], table_schema_simple: Schema
3843+
) -> None:
3844+
response = {
3845+
**example_table_metadata_with_snapshot_v1_rest_json,
3846+
"labels": {"object-labels": {"owner": "team-a"}, "fields": [{"field-id": 1, "labels": {"classification": "pii"}}]},
3847+
}
3848+
rest_mock.get(f"{TEST_URI}v1/namespaces/fokko/tables/table", json=response, status_code=200, request_headers=TEST_HEADERS)
3849+
rest_mock.post(f"{TEST_URI}v1/namespaces/fokko/tables", json=response, status_code=200, request_headers=TEST_HEADERS)
3850+
rest_mock.post(f"{TEST_URI}v1/namespaces/fokko/register", json=response, status_code=200, request_headers=TEST_HEADERS)
3851+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
3852+
expected = Labels(object_labels={"owner": "team-a"}, fields=[FieldLabel(field_id=1, labels={"classification": "pii"})])
3853+
3854+
table = catalog.load_table(("fokko", "table"))
3855+
created = catalog.create_table(("fokko", "table"), schema=table_schema_simple)
3856+
registered = catalog.register_table(("fokko", "table"), metadata_location="s3://warehouse/database/table/metadata.json")
3857+
for actual in (table, created, registered):
3858+
assert actual.labels == expected
3859+
3860+
# A pickled table keeps its labels. Java does not serialize them, because BaseTable.labels is transient.
3861+
assert pickle.loads(pickle.dumps(table)).labels == expected
3862+
3863+
# Labels are not table state, so equality ignores them and refresh() keeps the labels from the first load.
3864+
assert table == Table(
3865+
identifier=("fokko", "table"),
3866+
metadata_location=example_table_metadata_with_snapshot_v1_rest_json["metadata-location"],
3867+
metadata=TableMetadataV1(**example_table_metadata_with_snapshot_v1_rest_json["metadata"]),
3868+
io=load_file_io(),
3869+
catalog=catalog,
3870+
)
3871+
rest_mock.get(
3872+
f"{TEST_URI}v1/namespaces/fokko/tables/table",
3873+
json={**example_table_metadata_with_snapshot_v1_rest_json, "labels": {"object-labels": {"owner": "team-b"}}},
3874+
status_code=200,
3875+
request_headers=TEST_HEADERS,
3876+
)
3877+
table.refresh()
3878+
assert table.labels == expected

‎tests/table/test_init.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Table,
4747
TableIdentifier,
4848
)
49+
from pyiceberg.table.labels import Labels
4950
from pyiceberg.table.metadata import EncryptedKey, TableMetadataUtil, TableMetadataV1, TableMetadataV2, _generate_snapshot_id
5051
from pyiceberg.table.refs import MAIN_BRANCH, SnapshotRef, SnapshotRefType
5152
from pyiceberg.table.snapshots import (
@@ -194,6 +195,10 @@ def test_location(table_v2: Table) -> None:
194195
assert table_v2.location() == "s3://bucket/test/location"
195196

196197

198+
def test_labels_default_to_empty(table_v2: Table) -> None:
199+
assert table_v2.labels == Labels()
200+
201+
197202
def test_current_snapshot(table_v2: Table) -> None:
198203
assert table_v2.current_snapshot() == Snapshot(
199204
snapshot_id=3055729675574597004,

0 commit comments

Comments
 (0)