AlejandroMorgante opened a new pull request, #60248:
URL: https://github.com/apache/spark/pull/60248
### What changes were proposed in this pull request?
`DataFrame.approxQuantile` (classic and Connect) validates each value in
`probabilities` with a single check that combines a type test and a range test:
`if not isinstance(p, (float, int)) or p < 0 or p > 1`. Any failure, including
a validly-typed value that's simply out of range, raises `NOT_EXPECTED_TYPE`.
This PR splits it into a type check (still `NOT_EXPECTED_TYPE`) followed by a
separate range check using the existing `VALUE_NOT_BETWEEN` error, mirroring
the pattern already used a few lines below for `relativeError` (type check ->
`NOT_EXPECTED_TYPE`, then value check -> `NEGATIVE_VALUE`) in the same method.
### Why are the changes needed?
Before this change, `df.stat.approxQuantile("a", [0.1, 1.5], 0.1)` raises:
```
PySparkTypeError: [NOT_EXPECTED_TYPE] Argument `probabilities` should be
list[float, int], got float.
```
which is wrong on its face: `1.5` *is* a float, exactly what the message
says it should be. The real problem is the value is out of `[0, 1]`, not its
type. After this change, the same call raises:
```
PySparkValueError: [VALUE_NOT_BETWEEN] Value for `probabilities` must be
between 0 and 1.
```
This is the same type/value conflation bug fixed for streaming's
`awaitTermination` timeout validation in #60215, found while auditing the
codebase for the same anti-pattern elsewhere.
### Does this PR introduce _any_ user-facing change?
Yes. A `probabilities` value outside `[0, 1]` now raises `PySparkValueError`
with `VALUE_NOT_BETWEEN` instead of `PySparkTypeError` with
`NOT_EXPECTED_TYPE`. Values that are actually the wrong type still raise
`NOT_EXPECTED_TYPE` as before.
### How was this patch tested?
Added two tests to `FunctionsTestsMixin.test_approxQuantile` in
`test_functions.py` (shared by classic and Connect via
`test_parity_functions.py`): one asserting a non-numeric value in
`probabilities` still raises `NOT_EXPECTED_TYPE`, one asserting an out-of-range
float now raises `VALUE_NOT_BETWEEN`. Verified the validation logic directly
with a standalone Python script 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]