laskoviymishka commented on code in PR #4320: URL: https://github.com/apache/iceberg-python/pull/4320#discussion_r4518831080
########## pyiceberg/table/labels.py: ########## @@ -0,0 +1,53 @@ +# 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 null value parses the same as a missing key. + if data is None: + return {} + if isinstance(data, dict): + return {key: value for key, value in data.items() if value is not None} + return data Review Comment: Could we read a non-object `labels` as empty, the way Java does? [`LabelsParser.fromJson`](https://github.com/apache/iceberg/blob/16c8a32303542e992160a85645d78835179467e7/core/src/main/java/org/apache/iceberg/LabelsParser.java#L66-L80) only reads keys under `hasNonNull`, which is false for any non-object node, so Java returns empty Labels. Here a string, list, number or bool `labels` raises `ValidationError` and fails the whole `load_table`/`load_view`, where `main` ignores the key. ```suggestion 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 {} ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
