holdenk commented on code in PR #59722:
URL: https://github.com/apache/spark/pull/59722#discussion_r4580876653


##########
python/pyspark/sql/tests/test_udf_transpile_unit.py:
##########
@@ -2670,6 +2670,244 @@ def 
test_param_category_combos_caps_preserve_typed_pins(self):
         for combo in combos:
             self.assertEqual(combo[0], "string")
 
+    def test_annotation_category_map_and_array(self):
+        # dict/Dict/Mapping -> "map", list/List/Sequence -> "array": bare, 
subscripted,
+        # typing.-qualified or stringized (SPARK-55219).
+        import ast as _ast
+
+        from pyspark.sql.transpile import _annotation_category
+
+        for ann, expected in [
+            ("dict", "map"),
+            ("Dict", "map"),
+            ("Mapping", "map"),
+            ("dict[str, str]", "map"),
+            ("Mapping[str, str]", "map"),
+            ("typing.Dict[str, str]", "map"),
+            ('"dict"', "map"),
+            ('"dict[str, str]"', "map"),
+            ('"typing.Dict[str, str]"', "map"),
+            ("list", "array"),
+            ("List", "array"),
+            ("Sequence", "array"),
+            ("list[int]", "array"),
+            ("Sequence[int]", "array"),
+            ("typing.List[int]", "array"),
+            ('"list"', "array"),
+            ('"list[int]"', "array"),
+            ("set", None),
+            ("torch.float", None),
+        ]:
+            with self.subTest(ann=ann):
+                fn = _ast.parse(f"def f(a: {ann}): ...").body[0]
+                
self.assertEqual(_annotation_category(fn.args.args[0].annotation), expected)
+
+    def test_param_category_combos_pins_map_and_array(self):
+        # A pinned map/array param does not double the combos like an untyped 
one.

Review Comment:
   Fixed in 6b4ec891420ce53810feb35523d447e2bd5cfa5d: the comment now reads "A 
pinned map/array param contributes one candidate; an untyped one contributes 
three."
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



##########
python/pyspark/sql/tests/test_udf_transpile_unit.py:
##########
@@ -2670,6 +2670,244 @@ def 
test_param_category_combos_caps_preserve_typed_pins(self):
         for combo in combos:
             self.assertEqual(combo[0], "string")
 
+    def test_annotation_category_map_and_array(self):
+        # dict/Dict/Mapping -> "map", list/List/Sequence -> "array": bare, 
subscripted,
+        # typing.-qualified or stringized (SPARK-55219).
+        import ast as _ast
+
+        from pyspark.sql.transpile import _annotation_category
+
+        for ann, expected in [
+            ("dict", "map"),
+            ("Dict", "map"),
+            ("Mapping", "map"),
+            ("dict[str, str]", "map"),
+            ("Mapping[str, str]", "map"),
+            ("typing.Dict[str, str]", "map"),
+            ('"dict"', "map"),
+            ('"dict[str, str]"', "map"),
+            ('"typing.Dict[str, str]"', "map"),
+            ("list", "array"),
+            ("List", "array"),
+            ("Sequence", "array"),
+            ("list[int]", "array"),
+            ("Sequence[int]", "array"),
+            ("typing.List[int]", "array"),
+            ('"list"', "array"),
+            ('"list[int]"', "array"),
+            ("set", None),
+            ("torch.float", None),
+        ]:
+            with self.subTest(ann=ann):
+                fn = _ast.parse(f"def f(a: {ann}): ...").body[0]
+                
self.assertEqual(_annotation_category(fn.args.args[0].annotation), expected)
+
+    def test_param_category_combos_pins_map_and_array(self):
+        # A pinned map/array param does not double the combos like an untyped 
one.
+        import ast as _ast
+
+        from pyspark.sql.transpile import _param_category_combos
+
+        fn = _ast.parse("def f(a: dict, b): return b").body[0]
+        self.assertEqual(
+            _param_category_combos(fn, ["a", "b"]),
+            [
+                {0: "map", 1: "integer"},
+                {0: "map", 1: "float"},
+                {0: "map", 1: "string"},
+            ],
+        )
+        fn = _ast.parse("def f(a: list, b: dict): return b").body[0]
+        self.assertEqual(_param_category_combos(fn, ["a", "b"]), [{0: "array", 
1: "map"}])
+
+    def test_udf_transpile_map_and_array_input_categories(self):
+        # The option lowers against a matching MapType/ArrayType column and is 
pruned
+        # to interpreted Python against anything else.
+        def map_plus(x: dict, y: int):
+            return y + 1
+
+        def array_plus(x: list, y: int):
+            return y + 1
+
+        for func, schema, rows, other_schema, other_rows in (
+            (
+                map_plus,
+                "a map<string,string>, b long",
+                [({"k": "v"}, 1), ({}, 2), (None, 3)],
+                "a string, b long",
+                [("z", 1)],
+            ),
+            (
+                array_plus,
+                "a array<string>, b long",
+                [(["x"], 1), ([], 2), (None, 3)],
+                "a long, b long",
+                [(1, 1)],
+            ),
+        ):
+            self.assertEqual(self._vals(func, LongType(), schema, rows), [2, 
3, 4])
+            with self.sql_conf(_TRANSPILE_ON):
+                u = self._transpiled_udf(func, LongType())
+                projected = self.spark.createDataFrame(other_rows, 
other_schema).select(u("a", "b"))
+                self.assertGreater(self._eval_python_count(projected), 0, 
str(func))
+                self.assertEqual([r[0] for r in projected.collect()], [2], 
str(func))
+
+    def test_udf_transpile_map_and_array_comparison_falls_back(self):
+        # A lowered `=`/`<` fails analysis on a map (even vs None), while the 
array
+        # category permits unorderable map elements, so refuse and let Python 
answer
+        # (SPARK-55219).
+        def map_eq(a: dict, b: dict):
+            return a == b
+
+        def array_eq(a: list, b: list):
+            return a == b
+
+        def array_lt(a: list, b: list):

Review Comment:
   Added in 6b4ec891420ce53810feb35523d447e2bd5cfa5d: `map_lt_unreached(a: 
dict, b: dict, c: bool): return (a < b) if c else False` now runs in the 
comparison fallback test over two map columns and an all-false `c`. It asserts 
the fallback reason "operands are both `map`" and `False` for every row. With 
`map` dropped from the ordering refusal the UDF lowers and the test fails, so 
the member is pinned.
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



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