HonahX commented on code in PR #227: URL: https://github.com/apache/iceberg-python/pull/227#discussion_r1432471641
########## pyiceberg/io/pyarrow.py: ########## @@ -154,10 +154,9 @@ ONE_MEGABYTE = 1024 * 1024 BUFFER_SIZE = "buffer-size" ICEBERG_SCHEMA = b"iceberg.schema" -FIELD_ID = "field_id" -DOC = "doc" -PYARROW_FIELD_ID_KEYS = [b"PARQUET:field_id", b"field_id"] -PYARROW_FIELD_DOC_KEYS = [b"PARQUET:field_doc", b"field_doc", b"doc"] +# The PARQUET: in front means that it is Parquet specific, in this case the field_id +PYARROW_FIELD_ID_KEY = b"PARQUET:field_id" Review Comment: Shall we name it as `PYARROW_PARQUET_FIELD_ID_KEY` since it is parquet specific? If we add ORC Fileformat support later(https://github.com/apache/iceberg-python/issues/20), we may want a `PYARROW_ORC_FIELD_ID_KEY = b"iceberg.id"` as the physical schema looks like ``` user_id: int32 -- field metadata -- iceberg.id: '1' iceberg.required: 'false' user_name: string -- field metadata -- iceberg.id: '2' iceberg.required: 'false' status: string -- field metadata -- iceberg.id: '3' iceberg.required: 'false' cost: double -- field metadata -- iceberg.id: '4' ... ``` ########## tests/io/test_pyarrow_visitor.py: ########## @@ -209,9 +209,9 @@ def test_pyarrow_variable_binary_to_iceberg() -> None: def test_pyarrow_struct_to_iceberg() -> None: pyarrow_struct = pa.struct( [ - pa.field("foo", pa.string(), nullable=True, metadata={"field_id": "1", "doc": "foo doc"}), - pa.field("bar", pa.int32(), nullable=False, metadata={"field_id": "2"}), - pa.field("baz", pa.bool_(), nullable=True, metadata={"field_id": "3"}), + pa.field("foo", pa.string(), nullable=True, metadata={PYARROW_FIELD_ID_KEY: "1", "doc": "foo doc"}), Review Comment: Since it's in the test, shall we explicitly write `PARQUET:field_id` here instead of using the pre-defined string? ########## pyiceberg/io/pyarrow.py: ########## @@ -720,25 +719,17 @@ def primitive(self, primitive: pa.DataType) -> Optional[T]: def _get_field_id(field: pa.Field) -> Optional[int]: - for pyarrow_field_id_key in PYARROW_FIELD_ID_KEYS: - if field_id_str := field.metadata.get(pyarrow_field_id_key): - return int(field_id_str.decode()) - return None + return int(field_id_str.decode()) if (field_id_str := field.metadata.get(PYARROW_FIELD_ID_KEY)) else None -def _get_field_doc(field: pa.Field) -> Optional[str]: - for pyarrow_doc_key in PYARROW_FIELD_DOC_KEYS: - if doc_str := field.metadata.get(pyarrow_doc_key): - return doc_str.decode() - return None class _ConvertToIceberg(PyArrowSchemaVisitor[Union[IcebergType, Schema]]): def _convert_fields(self, arrow_fields: Iterable[pa.Field], field_results: List[Optional[IcebergType]]) -> List[NestedField]: fields = [] for i, field in enumerate(arrow_fields): field_id = _get_field_id(field) - field_doc = _get_field_doc(field) + field_doc = doc_str.decode() if (doc_str := field.metadata.get(PYARROW_FIELD_DOC_KEY)) else None Review Comment: ```suggestion field_doc = doc_str.decode() if (field.metadata and (doc_str := field.metadata.get(PYARROW_FIELD_DOC_KEY))) else None ``` Similar reason as the suggestion for `_get_field_id` ########## pyiceberg/io/pyarrow.py: ########## @@ -720,25 +719,17 @@ def primitive(self, primitive: pa.DataType) -> Optional[T]: def _get_field_id(field: pa.Field) -> Optional[int]: - for pyarrow_field_id_key in PYARROW_FIELD_ID_KEYS: - if field_id_str := field.metadata.get(pyarrow_field_id_key): - return int(field_id_str.decode()) - return None + return int(field_id_str.decode()) if (field_id_str := field.metadata.get(PYARROW_FIELD_ID_KEY)) else None Review Comment: I realized that we did not check if `field.metadata` is `None` here and may encounter: ``` File ".../pyiceberg/io/pyarrow.py", line 724, in _get_field_id if field_id_str := field.metadata.get(pyarrow_field_id_key): AttributeError: 'NoneType' object has no attribute 'get' ``` I think this was intended to be fixed in #183. Since that is closed, shall we fix it here? ```suggestion return int(field_id_str.decode()) if (field.metadata and (field_id_str := field.metadata.get(PYARROW_FIELD_ID_KEY))) else None ``` -- 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]
