rok commented on code in PR #50430:
URL: https://github.com/apache/arrow/pull/50430#discussion_r3639948107


##########
python/pyarrow/array.pxi:
##########
@@ -3618,18 +3611,49 @@ cdef class MapArray(ListArray):
     Concrete class for Arrow arrays of a map data type.
     """
 
-    cdef object _getitem_py(self, int64_t i):
+    cdef object _getitem_py(self, int64_t i, object maps_as_pydicts):
         cdef CListArray* arr = <CListArray*> self.ap
+        cdef bint as_dicts = maps_as_pydicts is not None
+        if as_dicts and maps_as_pydicts != "lossy" and maps_as_pydicts != 
"strict":
+            # Matches MapScalar.as_py, which validates before the null check.
+            raise ValueError(
+                "Invalid value for 'maps_as_pydicts': "
+                + "valid values are 'lossy', 'strict' or `None` (default). "
+                + f"Received {maps_as_pydicts!r}."
+            )
         if arr.IsNull(i):
             return None
         if self._children_cache is None:
             self._children_cache = (self.keys, self.items)
         cdef Array keys = <Array> (<tuple> self._children_cache)[0]
         cdef Array items = <Array> (<tuple> self._children_cache)[1]
         cdef int64_t j, start = arr.value_offset(i), end = arr.value_offset(i 
+ 1)
-        # Matches MapScalar.as_py with the default maps_as_pydicts=None:
-        # an association list of (key, value) tuples.
-        return [(keys._getitem_py(j), items._getitem_py(j)) for j in 
range(start, end)]
+        if not as_dicts:

Review Comment:
   Optional simplification: `as_dicts` can be eliminated by validating exactly 
like `MapScalar.as_py` and checking `maps_as_pydicts is None` directly. This is 
only a readability nit and not materially better.
   
   ```suggestion
           if maps_as_pydicts not in (None, "lossy", "strict"):
               # Matches MapScalar.as_py, which validates before the null check.
               raise ValueError(
                   "Invalid value for 'maps_as_pydicts': "
                   + "valid values are 'lossy', 'strict' or `None` (default). "
                   + f"Received {maps_as_pydicts!r}."
               )
           if arr.IsNull(i):
               return None
           if self._children_cache is None:
               self._children_cache = (self.keys, self.items)
           cdef Array keys = <Array> (<tuple> self._children_cache)[0]
           cdef Array items = <Array> (<tuple> self._children_cache)[1]
           cdef int64_t j, start = arr.value_offset(i), end = 
arr.value_offset(i + 1)
           if maps_as_pydicts is 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]

Reply via email to