Repository navigation
Parse catalog labels in REST load table and load view responses #4320
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
Merged
+145
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from typing import Any | ||
|
|
||
| from pydantic import Field, model_validator | ||
|
|
||
| from pyiceberg.typedef import IcebergBaseModel | ||
|
|
||
|
|
||
| class FieldLabel(IcebergBaseModel): | ||
| """Labels attached to a single field, identified by its field ID.""" | ||
|
|
||
| field_id: int = Field(alias="field-id") | ||
| labels: dict[str, str] | ||
|
|
||
|
|
||
| class Labels(IcebergBaseModel): | ||
| """Optional labels that a catalog returns with a table or view. | ||
|
|
||
| Labels are enrichment supplied by the catalog, such as ownership or classification. | ||
| They are not table state and are not written to the table metadata. | ||
| """ | ||
|
|
||
| object_labels: dict[str, str] = Field(alias="object-labels", default_factory=dict) | ||
| fields: list[FieldLabel] = Field(default_factory=list) | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def treat_null_as_absent(cls, data: Any) -> Any: | ||
| # A key with a null value parses the same as a missing key. | ||
| if isinstance(data, dict): | ||
| return {key: value for key, value in data.items() if value is not None} | ||
| if isinstance(data, cls): | ||
| return data | ||
| # Like Java, a null or non-object value reads as empty labels. | ||
| return {} | ||
|
|
||
| def is_empty(self) -> bool: | ||
| """Return True when there are neither object-level nor field-level labels.""" | ||
| return not self.object_labels and not self.fields |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from pyiceberg.table.labels import FieldLabel, Labels | ||
|
|
||
|
|
||
| def test_labels_from_json() -> None: | ||
| cases = [ | ||
| ('{"object-labels": null, "fields": null}', Labels()), | ||
| ( | ||
| '{"object-labels": {"owner": "team-a", "sensitivity": "high"}}', | ||
| Labels(object_labels={"owner": "team-a", "sensitivity": "high"}), | ||
| ), | ||
| ( | ||
| '{"fields": [{"field-id": 3, "labels": {"pii": "true"}}]}', | ||
| Labels(fields=[FieldLabel(field_id=3, labels={"pii": "true"})]), | ||
| ), | ||
| ( | ||
| """ | ||
| { | ||
| "object-labels": {"owner": "team-a"}, | ||
| "fields": [ | ||
| {"field-id": 1, "labels": {"classification": "pii"}}, | ||
| {"field-id": 2, "labels": {}} | ||
| ] | ||
| } | ||
| """, | ||
| Labels( | ||
| object_labels={"owner": "team-a"}, | ||
| fields=[FieldLabel(field_id=1, labels={"classification": "pii"}), FieldLabel(field_id=2, labels={})], | ||
| ), | ||
| ), | ||
| ] | ||
| for json, expected in cases: | ||
| assert Labels.model_validate_json(json) == expected | ||
|
|
||
|
|
||
| def test_labels_is_empty() -> None: | ||
| assert Labels().is_empty() | ||
| assert not Labels(object_labels={"owner": "team-a"}).is_empty() | ||
| assert not Labels(fields=[FieldLabel(field_id=1, labels={})]).is_empty() | ||
|
|
||
|
|
||
| def test_field_label_requires_field_id_and_labels() -> None: | ||
| cases = [ | ||
| ('{"labels": {}}', "1 validation error for FieldLabel\nfield-id\n Field required"), | ||
| ('{"field-id": 1}', "1 validation error for FieldLabel\nlabels\n Field required"), | ||
| ] | ||
| for json, message in cases: | ||
| with pytest.raises(ValidationError, match=message): | ||
| FieldLabel.model_validate_json(json) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it would be nice to test this against the test-fixtures, but that's waiting on apache/iceberg#18047
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.
Following up here: #18047 is merged now, so the fixtures are ready. I'll add a fixture-based test as a follow-up.
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.
Thanks. One note on the fixture: no published
apache/iceberg-rest-fixtureimage has #18047 yet. The newest image,latest, is from 2026-04-29, because the publish workflow is disabled. I opened apache/iceberg#18877 to publish the image again. After a new image ships, the test needsCATALOG_INCLUDE__LABELS=trueon therestservice indev/docker-compose-integration.yml. I can add the test then, if you want.