gaogaotiantian commented on code in PR #57105:
URL: https://github.com/apache/spark/pull/57105#discussion_r3562684695


##########
python/pyspark/sql/conversion.py:
##########
@@ -1043,9 +1046,44 @@ def _to_pylist(column: Union["pa.Array", 
"pa.ChunkedArray"]) -> List[Any]:
                 result.extend(ArrowTableToRowsConversion._to_pylist(chunk))
             return result
 
-        if (pa.types.is_list(column.type) or 
pa.types.is_large_list(column.type)) and len(
-            column
-        ) > 0:
+        if len(column) == 0:
+            return []
+
+        if pa.types.is_map(column.type):
+            # Maps have the same offsets layout as lists; each row becomes a
+            # list of (key, value) tuples, matching MapScalar.as_py.
+            n = len(column)
+            offsets = column.offsets.to_numpy(zero_copy_only=True).tolist()
+            start = offsets[0]
+            length = offsets[-1] - start
+            keys = 
ArrowTableToRowsConversion._to_pylist(column.keys.slice(start, length))
+            items = 
ArrowTableToRowsConversion._to_pylist(column.items.slice(start, length))
+            if column.null_count == 0:
+                return [
+                    list(
+                        zip(
+                            keys[offsets[i] - start : offsets[i + 1] - start],
+                            items[offsets[i] - start : offsets[i + 1] - start],
+                        )
+                    )
+                    for i in range(n)
+                ]
+            valid = column.is_valid().to_numpy(zero_copy_only=False).tolist()
+            return [
+                (
+                    list(
+                        zip(
+                            keys[offsets[i] - start : offsets[i + 1] - start],
+                            items[offsets[i] - start : offsets[i + 1] - start],
+                        )
+                    )
+                    if valid[i]
+                    else None
+                )
+                for i in range(n)
+            ]
+
+        if pa.types.is_list(column.type) or 
pa.types.is_large_list(column.type):

Review Comment:
   nit: could you change this `if` to `elif`? otherwise looks good to me.



##########
python/pyspark/sql/conversion.py:
##########
@@ -1043,9 +1046,44 @@ def _to_pylist(column: Union["pa.Array", 
"pa.ChunkedArray"]) -> List[Any]:
                 result.extend(ArrowTableToRowsConversion._to_pylist(chunk))
             return result
 
-        if (pa.types.is_list(column.type) or 
pa.types.is_large_list(column.type)) and len(
-            column
-        ) > 0:
+        if len(column) == 0:
+            return []
+
+        if pa.types.is_map(column.type):
+            # Maps have the same offsets layout as lists; each row becomes a
+            # list of (key, value) tuples, matching MapScalar.as_py.
+            n = len(column)
+            offsets = column.offsets.to_numpy(zero_copy_only=True).tolist()
+            start = offsets[0]
+            length = offsets[-1] - start
+            keys = 
ArrowTableToRowsConversion._to_pylist(column.keys.slice(start, length))
+            items = 
ArrowTableToRowsConversion._to_pylist(column.items.slice(start, length))
+            if column.null_count == 0:
+                return [
+                    list(
+                        zip(
+                            keys[offsets[i] - start : offsets[i + 1] - start],
+                            items[offsets[i] - start : offsets[i + 1] - start],
+                        )
+                    )
+                    for i in range(n)
+                ]
+            valid = column.is_valid().to_numpy(zero_copy_only=False).tolist()
+            return [
+                (
+                    list(
+                        zip(
+                            keys[offsets[i] - start : offsets[i + 1] - start],
+                            items[offsets[i] - start : offsets[i + 1] - start],
+                        )
+                    )
+                    if valid[i]
+                    else None
+                )
+                for i in range(n)
+            ]
+
+        if pa.types.is_list(column.type) or 
pa.types.is_large_list(column.type):

Review Comment:
   nit: could you change this `if` and the one below to `elif`? otherwise looks 
good to me.



-- 
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]

Reply via email to