AlejandroMorgante opened a new pull request, #60306:
URL: https://github.com/apache/spark/pull/60306
### What changes were proposed in this pull request?
`DataFrame.approxQuantile`'s `relativeError` validation (classic and
Connect) checks `if relativeError < 0:`, which is `False` for `float("nan")`
since every comparison with `NaN` is `False`, so `NaN` silently passes.
`relativeError` is also typed as `(float, int)`, and since `bool` is a subclass
of `int`, `True`/`False` are silently accepted as `1.0`/`0.0`. This PR fixes
both and extracts the validation into a shared
`validate_approx_quantile_relative_error` helper in `pyspark/sql/utils.py`,
used by both classic and Connect, mirroring
`validate_approx_quantile_probabilities` added in #60248.
### Why are the changes needed?
This is the same hole already fixed for `probabilities` in #60248, just for
a different argument of the same method -- found and confirmed by
@dongjoon-hyun while reviewing that PR, but intentionally split out since it's
a different argument with its own bug, not the same root cause.
Before this change, `NaN` passes client-side validation and then fails
server-side with a raw, unwrapped `IllegalArgumentException` instead of a
proper PySpark error. `bool` passes silently, which doesn't make semantic sense
for an error-tolerance parameter.
### Does this PR introduce _any_ user-facing change?
Yes. Before:
```
>>> df.stat.approxQuantile("a", [0.5], float("nan"))
IllegalArgumentException # raw, unwrapped, from the server
>>> df.stat.approxQuantile("a", [0.5], True)
# silently treated as relativeError 1.0
```
After:
```
>>> df.stat.approxQuantile("a", [0.5], float("nan"))
PySparkValueError: [NEGATIVE_VALUE] Value for `relativeError` must be
greater than or equal to 0, got 'nan'.
>>> df.stat.approxQuantile("a", [0.5], True)
PySparkTypeError: [NOT_EXPECTED_TYPE] Argument `relativeError` should be
float or int, got bool.
```
### How was this patch tested?
Added `NaN` and `bool` cases to `FunctionsTestsMixin.test_approxQuantile` in
`test_functions.py` (shared by classic and Connect via
`test_parity_functions.py`), which previously had no coverage at all for
`relativeError` (the existing type/negative-value tests live only in the
Connect-only `test_connect_stat.py`). Verified the shared helper directly,
including a real import confirming classic and Connect share the same function
object, and `ruff check`.
### Was this patch authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude Code (Claude Sonnet 5)
---
This contribution is my original work and I license the work to the project
under the project's open source license.
(First-time contributors here, still learning the contribution flow -- happy
to adjust anything that's off.)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]