github-actions[bot] commented on code in PR #41737:
URL: https://github.com/apache/beam/pull/41737#discussion_r4726809524
##########
sdks/python/apache_beam/typehints/arrow_type_compatibility.py:
##########
@@ -295,13 +298,75 @@ def _arrow_type_from_beam_fieldtype(
return output_arrow_type
+def _to_arrow_value_fn(
+ arrow_type: pa.DataType) -> Optional[Callable[[Any], Any]]:
+ """Build a converter; pa.array rejects row objects for struct fields."""
+ if isinstance(arrow_type, pa.StructType):
+ fields = [(arrow_type[i].name, _to_arrow_value_fn(arrow_type[i].type))
+ for i in range(len(arrow_type))]
+ return _row_to_dict_fn(fields)
+
+ if isinstance(arrow_type, (pa.ListType, pa.LargeListType)):
+ convert_element = _to_arrow_value_fn(arrow_type.value_type)
+ if convert_element is None:
+ return None
+ return lambda value: None if value is None else [
+ convert_element(element) for element in value
+ ]
+
+ if isinstance(arrow_type, pa.MapType):
+ convert_item = _to_arrow_value_fn(arrow_type.item_type)
+ if convert_item is None:
+ return None
+
+ def convert_map(value):
+ if value is None:
+ return None
+ return {key: convert_item(item) for key, item in value.items()}
Review Comment:
**🟢 Low**: PyArrow's `pa.array` accepts both `dict` and lists of `(key,
item)` tuples for `MapType` fields (and `pa.MapScalar.as_py()` returns a list
of tuples). Supporting both `dict` (via `.items()`) and sequence of pairs here
would prevent an `AttributeError` if map values are passed as tuple lists.
```suggestion
return {key: convert_item(item) for key, item in (value.items() if
hasattr(value, 'items') else value)}
```
--
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]