[SPARK-52382][PYTHON] Fix TypeError when collecting MapType with ArrayType keys#55413
Open
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-52382][PYTHON] Fix TypeError when collecting MapType with ArrayType keys#55413yadavay-amzn wants to merge 1 commit intoapache:masterfrom
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Conversation
…yType keys When a DataFrame has a MapType column with ArrayType keys (e.g., MapType(ArrayType(StringType()), StringType())), calling collect() raises TypeError: unhashable type: 'list'. Root cause: The JVM-side EvaluatePython.toJava converts ArrayType data to java.util.ArrayList, which Pyrolite pickles as Python lists. Since Python lists are unhashable, they cannot be used as dict keys when the map is deserialized on the Python side. Fix: - JVM side (EvaluatePython.scala): Add makeHashable() that recursively converts ArrayList to Array for map keys, so Pyrolite pickles them as Python tuples (which are hashable). - Python side (types.py, conversion.py): Add _make_hashable() in the MapType converters to convert any list keys to tuples, handling both the classic pickle path and the Arrow/Spark Connect path. Closes #XXXXX
b8681aa to
67778a1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Fix
TypeError: unhashable type: 'list'when callingcollect()on a DataFrame withMapType(ArrayType(...), ...)columns.JVM side (
EvaluatePython.scala): AddedmakeHashable()that recursively convertsjava.util.ArrayListtoArrayfor map keys before pickling. Pyrolite pickles Java arrays as Python tuples (hashable) instead of lists (unhashable).Python side (
types.py,conversion.py): Added_make_hashable()in MapType converters to convert list keys to tuples, covering both the classic pickle path and the Arrow/Spark Connect path.Why are the changes needed?
MapTypewithArrayTypekeys is valid per the PySpark documentation, butcollect()fails because:java.util.ArrayListDoes this PR introduce any user-facing change?
Yes.
DataFrame.collect()now works correctly forMapTypecolumns withArrayTypekeys. Array keys are returned as tuples instead of raisingTypeError.How was this patch tested?
Added
test_collect_map_with_array_keyintest_collection.pythat creates a DataFrame withMapType(ArrayType(StringType()), StringType()), callscollect(), and verifies the result.Was this patch authored or co-authored using generative AI tooling?
Yes.