rahulsmahadev commented on code in PR #4419:
URL: https://github.com/apache/iceberg-python/pull/4419#discussion_r4733983158
##########
pyiceberg/io/pyarrow.py:
##########
@@ -1282,6 +1301,68 @@ def pyarrow_to_schema(
)
+def _has_any_field_ids(schema: pa.Schema) -> bool:
+ """Return whether any field in the schema, including nested ones, has a
field ID."""
+ return any(_field_has_any_ids(field) for field in schema)
+
+
+def _field_has_any_ids(field: pa.Field) -> bool:
+ if _get_field_id(field) is not None:
+ return True
+ arrow_type = field.type
+ if pa.types.is_struct(arrow_type):
+ return any(_field_has_any_ids(arrow_type.field(i)) for i in
range(arrow_type.num_fields))
+ elif isinstance(arrow_type, (pa.ListType, pa.LargeListType,
pa.FixedSizeListType)):
+ return _field_has_any_ids(arrow_type.value_field)
+ elif isinstance(arrow_type, pa.MapType):
+ return _field_has_any_ids(arrow_type.key_field) or
_field_has_any_ids(arrow_type.item_field)
+ return False
+
+
+def _prune_fields_without_ids(schema: pa.Schema) -> pa.Schema:
+ """Drop fields without a field ID, so only the columns with IDs are read.
+
+ A list is dropped when its element has no ID, and a map when its key or
value has none,
+ since neither can be represented in Iceberg without them. A struct whose
children all
+ get dropped is dropped as well.
+ """
+ fields = [pruned for field in schema if (pruned :=
_prune_field_without_id(field)) is not None]
+ return pa.schema(fields, metadata=schema.metadata)
+
+
+def _prune_field_without_id(field: pa.Field) -> pa.Field | None:
+ if _get_field_id(field) is None:
+ return None
+ pruned_type = _prune_type_without_ids(field.type)
+ if pruned_type is None:
+ return None
+ return field.with_type(pruned_type)
+
+
+def _prune_type_without_ids(arrow_type: pa.DataType) -> pa.DataType | None:
+ if pa.types.is_struct(arrow_type):
+ children = [arrow_type.field(i) for i in range(arrow_type.num_fields)]
+ pruned_children = [pruned for child in children if (pruned :=
_prune_field_without_id(child)) is not None]
+ return pa.struct(pruned_children) if pruned_children else None
+ elif isinstance(arrow_type, (pa.ListType, pa.LargeListType,
pa.FixedSizeListType)):
+ element_field = _prune_field_without_id(arrow_type.value_field)
+ if element_field is None:
+ return None
+ if isinstance(arrow_type, pa.LargeListType):
+ return pa.large_list(element_field)
+ if isinstance(arrow_type, pa.FixedSizeListType):
+ return pa.list_(element_field, arrow_type.list_size)
+ return pa.list_(element_field)
+ elif isinstance(arrow_type, pa.MapType):
+ key_field = _prune_field_without_id(arrow_type.key_field)
+ value_field = _prune_field_without_id(arrow_type.item_field)
+ if key_field is None or value_field is None:
Review Comment:
A map whose key or value has an ID, but not both, is dropped here, so its
entries read as null. Java keeps it: `PruneColumns.map` returns the whole map
when either child is selected, and `TypeWithSchemaVisitor` matches key and
value by position.
```python
# foo: map<string, int> with IDs on foo and its key only (same with the
value only), file already in the table
tbl.scan().to_arrow()["foo"].to_pylist()
# main with a name mapping: [[('k', 1)], [], None]; Java's reader: [{k=1},
{}, null]
# here, with or without a mapping: [None, None, None]
```
One option would be to keep the map and match key and value by position as
Java does; if that's out of scope here, a follow-up issue and a line in
Limitations that Java reads these maps would help.
--
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]