spark-test commented on code in PR #59722:
URL: https://github.com/apache/spark/pull/59722#discussion_r4582839780
##########
python/pyspark/sql/transpile.py:
##########
@@ -490,6 +496,16 @@ def _lower_eq(
"Python compares across types as unequal while Spark would
coerce "
"or fail analysis, so the transpiler falls back to
interpreted Python"
)
+ if lc in ("map", "array") or rc in ("map", "array"):
Review Comment:
**Nit (P3):** The docstring above this guard (lines 469-476) was not
updated. It still presents the different-category check as the only refusal and
says "A ``None`` literal operand stays allowed (the four-branch NULL handling
above reproduces Python exactly)." With this guard, `def f(a: dict): return a
== None` is refused, as
`test_udf_transpile_map_and_array_comparison_falls_back` asserts for
`map_eq_none` and `none_eq_map`, so a reader of the docstring would expect a
lowering that never happens. Naming the map/array refusal there, with the
reasons this guard's comment gives (a map `=` fails analysis even against
`None`, and the `array` category records neither the element type nor its
collation), and limiting the `None` sentence to the non-container categories
would make the docstring match the code.
**Verification:**
- **Inspection:** The `_lower_eq` docstring names the map/array refusal,
including the comparison with `None`, and no sentence in it says a `None`
operand lowers for every category.
##########
python/pyspark/sql/transpile.py:
##########
@@ -1126,15 +1150,29 @@ def _annotation_category(annotation: Optional[ast.AST])
-> Optional[str]:
"""Map a parameter's type annotation to a category, or ``None`` when absent
or unrecognised (the caller then tries all numeric and string variants).
- Returns one of ``"integer"``, ``"float"``, ``"string"``, ``"bool"``, or
- ``"binary"`` (matching the constant handling in ``_category``). ``int``
- maps to ``"integer"`` and ``float`` maps to ``"float"`` so the JVM can
- select the variant whose NaN semantics match the bound column type."""
+ Returns one of ``"integer"``, ``"float"``, ``"string"``, ``"bool"``,
+ ``"binary"``, ``"map"``, or ``"array"``. Numeric annotations select the
+ variant whose NaN semantics match the bound column type."""
+ if isinstance(annotation, ast.Subscript):
+ annotation = annotation.value # dict[str, str] -> dict
name: Optional[str] = None
if isinstance(annotation, ast.Name):
name = annotation.id
+ elif (
+ isinstance(annotation, ast.Attribute)
+ and isinstance(annotation.value, ast.Name)
+ and annotation.value.id == "typing"
+ ):
+ name = annotation.attr # typing.Dict -> Dict, but not torch.float
elif isinstance(annotation, ast.Constant) and isinstance(annotation.value,
str):
- name = annotation.value # stringized annotation, e.g. def f(a: "int")
+ # Parse quoted subscripted and typing-qualified forms like their
+ # unquoted spellings. Anything unparseable falls out as untyped.
+ try:
+ with _syntax_warnings_suppressed():
+ parsed = ast.parse(annotation.value, mode="eval").body
+ return _annotation_category(parsed)
+ except (SyntaxError, ValueError):
Review Comment:
**Nit (P3):** No test reaches this handler: the quoted annotations in
`test_annotation_category_map_and_array` and
`test_quoted_container_annotation_suppresses_syntax_warnings` all parse. With
transpilation on, this `except` is what keeps a UDF such as `def f(x: "the
input value", y: int): return y + 1` lowering as it did on the base branch,
with `x` treated as untyped. If the handler were removed, or narrowed so it no
longer catches `SyntaxError`, the parse error would escape
`_param_category_combos`, `_transpile_func`'s outer `except` would return no
options, and the UDF would fall back to interpreted Python with only a warning,
while every test still passes. Adding `('"the input value"', None)` to the
subTest cases in `test_annotation_category_map_and_array` would pin the
fallback.
**Verification:**
- **Regression:** Classifying a quoted annotation whose text does not parse
returns no category, and the test fails if the parse's SyntaxError is allowed
to escape.
--
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]