holdenk opened a new pull request, #60720:
URL: https://github.com/apache/spark/pull/60720
### What changes were proposed in this pull request?
1. **Bounded early-return normalization.** `if <test>: return y` followed by
`return body` is
rewritten into a single if/else only when `<test>` proves a parameter
non-null (for example
`if x is None: return None`). Other two-statement bodies, such as `if x >
0: return 1` /
`return 0`, keep running as interpreted Python, as they did before
SPARK-58628.
2. **One NULL-guard advisory per UDF, outside the fallback handler.**
`_transpile_func` now
reports whether any returned option kept a raising NULL guard, and
`UserDefinedFunction.__init__` emits a single `UserWarning` naming the
UDF after its
`try` / `except`. Only options that lowered successfully count, so a UDF
that runs
entirely as interpreted Python gets no advisory. Previously the warning
was emitted inside
`_transpile_from_ast`, once per option and within the handler that turns
any exception into
a fallback, so escalating it with `warnings.filterwarnings("error", ...)`
silently discarded
options that had lowered successfully.
The guard sites keep setting `_null_guard_emitted` unchanged, so the
str-method guards added in
SPARK-59816 are covered too.
This is written to avoid conflicting with SPARK-61913, which covers the
other planned follow-ups:
NaN ordering, `and` / `or` short-circuiting, null proofs through `or` and
arithmetic, and bool
repeat counts.
### Why are the changes needed?
- The unconditional early-return rewrite also lowered bodies that have
nothing to do with the
null-guard idiom it was added for, extending native lowering (and its
documented NaN and
NULL-arithmetic differences from Python) to UDFs that do not benefit from
null narrowing.
- A UDF with several lowered options emitted the same warning once per
option, and promoting
the warning to an error turned a working transpilation into a silent
fallback.
### Does this PR introduce _any_ user-facing change?
Only with `spark.sql.experimental.optimizer.transpilePyUDFs` enabled, and
only relative to
unreleased master:
- Early-return bodies whose test does not prove a parameter non-null now run
as interpreted
Python instead of being transpiled.
- The NULL-guard `UserWarning` is emitted once per UDF and includes the UDF,
instead of once per
transpiled option with generic text. Escalating it to an error raises from
the `udf(...)`
call instead of silently dropping the transpiled options.
### How was this patch tested?
Extended two existing tests in `test_udf_transpile_unit.py`; both fail
without this change:
- `test_udf_transpile_early_return_null_guard`: an early return whose test
proves nothing
non-null (`if x > 0: return 1` / `return 0`) now falls back to Python.
- `test_udf_transpile_null_guard_warning`: exactly one advisory, naming the
UDF; with
warnings escalated to errors, `_transpile_func` returns every option and
reports the guard,
and the `UserDefinedFunction` constructor raises the advisory.
Built with `build/mvn -Phive` on master and ran, all passing:
- `pyspark.sql.tests.test_udf_transpile_unit`
- `pyspark.sql.tests.test_udf`
- `pyspark.sql.udf`
- `pyspark.sql.tests.test_udf_transpile_parity`
- `pyspark.sql.tests.test_udf_transpile_hypothesis` (`RUN_HYPOTHESIS=1`,
`RUN_HYPOTHESIS_MAX_EXAMPLES=200`)
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 5.5)
--
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]