dongjoon-hyun commented on code in PR #60248:
URL: https://github.com/apache/spark/pull/60248#discussion_r4521497431
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1741,6 +1741,15 @@ def approxQuantile(
"arg_type": type(p).__name__,
},
)
+ if p < 0 or p > 1:
Review Comment:
`p < 0 or p > 1` evaluates to `False` for `float("nan")`, so NaN still slips
through this new client-side check. `df.stat.approxQuantile("a",
[float("nan")], 0.1)` then fails on the server side at
`require(probabilities.forall(p => p >= 0 && p <= 1.0), ...)` in
`StatFunctions` with `IllegalArgumentException`, which is not a `ValueError`
subclass.
How about the following, which rejects NaN as well? The same applies to the
Connect side.
```python
if not (0 <= p <= 1):
```
##########
python/pyspark/sql/tests/connect/test_connect_stat.py:
##########
@@ -263,19 +263,15 @@ def test_stat_approx_quantile(self):
"arg_type": "float",
},
)
- with self.assertRaises(PySparkTypeError) as pe:
+ with self.assertRaises(PySparkValueError) as pe:
Review Comment:
This test change shows that this PR changes the exception class users see:
an out-of-range `probabilities` value used to raise `PySparkTypeError` (a
`TypeError`) and now raises `PySparkValueError` (a `ValueError`). Existing user
code like `except TypeError:` will stop catching it after upgrading.
Could you add an entry to the `Upgrading from PySpark 4.2 to 4.3` section of
`python/docs/source/migration_guide/pyspark_upgrade.rst`? There is a precedent
for this kind of change in the same file: `In Spark 3.2, the PySpark methods
from sql, ml, spark_on_pandas modules raise the TypeError instead of ValueError
when are applied to a param of inappropriate type.`
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1732,7 +1732,7 @@ def approxQuantile(
if isinstance(probabilities, tuple):
probabilities = list(probabilities)
for p in probabilities:
Review Comment:
Since the type check and the range check are interleaved per element, the
raised exception class depends on the element order:
- `approxQuantile("a", [1.5, "x"], 0.1)` raises `PySparkValueError`
(`VALUE_NOT_BETWEEN`)
- `approxQuantile("a", ["x", 1.5], 0.1)` raises `PySparkTypeError`
(`NOT_EXPECTED_TYPE`)
To follow the `relativeError` pattern mentioned in the PR description (type
check first, then value check), how about checking the types of all elements
first and then the ranges in a second loop? The same applies to the Connect
side.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1741,6 +1741,15 @@ def approxQuantile(
"arg_type": type(p).__name__,
},
)
+ if p < 0 or p > 1:
+ raise PySparkValueError(
Review Comment:
`VALUE_NOT_BETWEEN` doesn't show the offending value and doesn't say whether
the bounds are inclusive, so it's hard to tell which element is wrong in a long
`probabilities` list. How about using the existing `VALUE_OUT_OF_BOUNDS` error
condition instead, as `python/pyspark/sql/types.py` does?
```python
raise PySparkValueError(
errorClass="VALUE_OUT_OF_BOUNDS",
messageParameters={
"arg_name": "probabilities",
"lower_bound": "0",
"upper_bound": "1",
"actual": str(p),
},
)
```
This renders as ``Value for `probabilities` must be between 0 and 1
(inclusive), got 1.5``. The same applies to the Connect side.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1741,6 +1741,15 @@ def approxQuantile(
"arg_type": type(p).__name__,
},
)
+ if p < 0 or p > 1:
+ raise PySparkValueError(
+ errorClass="VALUE_NOT_BETWEEN",
+ messageParameters={
+ "arg_name": "probabilities",
+ "min": "0",
+ "max": "1",
+ },
+ )
probabilities = _to_list(self._sc, cast(List["ColumnOrName"],
probabilities))
if not isinstance(relativeError, (float, int)):
Review Comment:
FYI, the `relativeError` validation that this PR uses as a reference has the
same NaN hole. `if relativeError < 0:` below is `False` for `float("nan")`, so
`approxQuantile("a", [0.5], float("nan"))` passes the client-side validation
and then fails on the server side at `require(relativeError >= 0, ...)` with
`IllegalArgumentException`. `if not relativeError >= 0:` would catch NaN too.
The same applies to the Connect side.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1732,7 +1732,7 @@ def approxQuantile(
if isinstance(probabilities, tuple):
probabilities = list(probabilities)
for p in probabilities:
- if not isinstance(p, (float, int)) or p < 0 or p > 1:
+ if not isinstance(p, (float, int)):
Review Comment:
Since `bool` is a subclass of `int`, `isinstance(p, (float, int))` still
accepts `True` and `False`, so `approxQuantile("a", [True, False], 0.1)` passes
the client-side validation as if they were the probabilities `1` and `0`. Since
this PR is about separating type errors from value errors, shall we reject
`bool` here explicitly?
```python
if isinstance(p, bool) or not isinstance(p, (float, int)):
```
The same applies to the Connect side.
##########
python/pyspark/sql/connect/dataframe.py:
##########
@@ -1796,6 +1796,15 @@ def approxQuantile(
"arg_type": type(p).__name__,
},
)
+ if p < 0 or p > 1:
Review Comment:
This validation block (the `probabilities` and `relativeError` checks) is
duplicated verbatim between `classic/dataframe.py` and `connect/dataframe.py`,
and this PR adds the same 9 lines to both copies. The issues raised on the
classic side (NaN, element order, `bool`) need to be fixed in both places, and
missing one would make classic and Connect behave differently. Could we move
this validation into a shared helper (e.g., in `pyspark/sql/utils.py`) so the
two clients stay in sync?
##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1634,6 +1634,28 @@ def test_approxQuantile(self):
self.assertRaises(TypeError, lambda: df.stat.approxQuantile(("a",
123), [0.1, 0.9], 0.1))
self.assertRaises(TypeError, lambda: df.stat.approxQuantile(["a",
123], [0.1, 0.9], 0.1))
+ with self.assertRaises(PySparkTypeError) as pe:
+ df.stat.approxQuantile("a", [0.1, "0.9"], 0.1)
+
+ self.check_error(
+ exception=pe.exception,
+ errorClass="NOT_EXPECTED_TYPE",
+ messageParameters={
+ "arg_name": "probabilities",
+ "expected_type": "list[float, int]",
+ "arg_type": "str",
+ },
+ )
+
+ with self.assertRaises(PySparkValueError) as pe:
Review Comment:
The new tests cover only the upper-bound case (`1.5`). Could you add the
following cases too? This test runs for both classic and Connect (via
`test_parity_functions.py`), so these would cover both paths.
- a lower-bound violation such as `-0.1` (currently tested only in the
Connect-only `test_connect_stat.py`)
- `float("nan")`
- a list that mixes type and range errors, such as `[1.5, "x"]` and `["x",
1.5]`
- a `tuple` input for `probabilities`
--
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]