spark-test commented on code in PR #59722:
URL: https://github.com/apache/spark/pull/59722#discussion_r4576653110
##########
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:
**Nit (P3):** An untyped parameter triples the combos rather than doubling
them: `_param_category_combos` tries it as `integer`, `float`, and `string` and
takes the product. On the base branch, where `dict` was untyped, `def f(a:
dict, b)` yields nine combos, and pinning `a` cuts that to the three asserted
here.
```suggestion
# A pinned map/array param contributes one candidate; an untyped one
contributes three.
```
**Verification:**
- **Inspection:** The combo test's comment states that an untyped parameter
is tried as integer, float, and string, and no comment in the test file says an
untyped parameter doubles the combos.
##########
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:
**Nit (P3):** `array_lt` is the only ordering case, so nothing fails if
`map` is dropped from the ordering refusal in `_lower_value_compare` (`if lc in
("map", "array")`). The map cases here all stop at the separate `==`/`!=`
guard, and neither this file nor the other transpile suites order two `dict`
parameters. Without that member, `def f(a: dict, b: dict, c: bool): return (a <
b) if c else False` would lower: both branches classify as `bool`, the
same-category check passes, and the JVM's `map` arm keeps the option.
CheckAnalysis would then reject the map `LessThan` and fail the query, where
Python returns `False` for every row with `c` false. Adding that UDF here over
two map columns and an all-false boolean column, asserting a fallback reason
that names the map ordering refusal and `False` for every row, would pin the
member.
**Verification:**
- **Regression:** Ordering two dict-annotated parameters produces no
transpiled option, gives the ordering refusal for the map category as its
fallback reason, and returns the interpreted Python result on rows where the
comparison is never evaluated.
--
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]