spark-test commented on code in PR #60306:
URL: https://github.com/apache/spark/pull/60306#discussion_r4533581287


##########
python/pyspark/sql/dataframe.py:
##########
@@ -78,6 +79,31 @@
 __all__ = ["DataFrame", "DataFrameNaFunctions", "DataFrameStatFunctions"]
 
 
+def validate_approx_quantile_relative_error(relativeError: Any) -> float:

Review Comment:
   **Nit (P3):** This is the first module-level function in 
`pyspark.sql.dataframe`, the module users import `DataFrame` from, and nothing 
but its absence from `__all__` marks it as internal: `from 
pyspark.sql.dataframe import validate_approx_quantile_relative_error` works, 
and the name shows up in `dir()` and IDE completion next to `DataFrame`. The 
other helpers that classic and Spark Connect share carry a leading underscore 
(`_preapare_args_for_sample` and `_preapare_cols_for_sort` in this module, 
`_validate_vectorized_udf` and `_validate_udtf_handler` elsewhere), as PEP 8 
asks for internal functions even when `__all__` is set. Without it, users can 
come to rely on the name, and renaming or moving it later, for example when the 
`probabilities` helper from #60248 lands, becomes a compatibility question. 
Could you rename it to `_validate_approx_quantile_relative_error` and update 
the imports in `classic/dataframe.py` and `connect/dataframe.py`? It can stay a 
module-level function her
 e.
   
   **Verification:**
   - **Inspection:** pyspark.sql.dataframe defines no new function without a 
leading underscore, and no reference to the old helper name remains in the 
Python sources.
   - **Behavior:** approxQuantile on classic and Spark Connect still accepts 
valid relativeError values and raises the same errors as before the rename for 
NaN, bool, negative and non-numeric values.



##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1743,24 +1748,7 @@ def approxQuantile(
                 )
         probabilities = _to_list(self._sc, cast(List["ColumnOrName"], 
probabilities))
 
-        if not isinstance(relativeError, (float, int)):
-            raise PySparkTypeError(
-                errorClass="NOT_EXPECTED_TYPE",
-                messageParameters={
-                    "expected_type": "float or int",
-                    "arg_name": "relativeError",
-                    "arg_type": type(relativeError).__name__,
-                },
-            )
-        if relativeError < 0:
-            raise PySparkValueError(
-                errorClass="NEGATIVE_VALUE",
-                messageParameters={
-                    "arg_name": "relativeError",
-                    "arg_value": str(relativeError),
-                },
-            )
-        relativeError = float(relativeError)
+        relativeError = validate_approx_quantile_relative_error(relativeError)

Review Comment:
   **Nit (P3):** After this change the two numeric arguments of 
`approxQuantile` are validated differently: `relativeError` now rejects NaN and 
bool, but the `probabilities` check a few lines above (`not isinstance(p, 
(float, int)) or p < 0 or p > 1`, here and in `connect/dataframe.py`) still 
lets both through, so `approxQuantile("a", [float("nan")], 0.1)` passes the 
client check and fails on the server with `IllegalArgumentException`, and a 
`True` or `False` probability passes it too. Leaving that half to #60248, as 
the description says, is fine, but only the description says it: a reader of 
either implementation sees the asymmetry with no sign that it is known, and a 
later change to one argument's rules can easily miss the other. Could you add a 
TODO at both `probabilities` checks naming the NaN and bool probabilities and 
#60248 (or its JIRA)? If #60248 has already replaced those checks when you 
rebase, no TODO is needed.
   
   **Verification:**
   - **Inspection:** Both approxQuantile implementations carry the TODO at 
their probabilities check, it names NaN and bool probabilities and the 
follow-up that fixes them, and it does not depend on the relativeError helper's 
name.



##########
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(PySparkValueError) as pe:
+            df.stat.approxQuantile("a", [0.1, 0.5, 0.9], float("nan"))
+
+        self.check_error(
+            exception=pe.exception,
+            errorClass="NEGATIVE_VALUE",
+            messageParameters={"arg_name": "relativeError", "arg_value": 
"nan"},
+        )
+
+        with self.assertRaises(PySparkTypeError) as pe:
+            df.stat.approxQuantile("a", [0.1, 0.5, 0.9], True)

Review Comment:
   **Nit (P3):** This pins the rejected side of the new bool carve-out but not 
the accepted side: no PySpark test or doctest passes a plain `int` as 
`relativeError` (the values used are `0.1`, `0.05`, `"str"`, `-0.1`, NaN and 
`True`). If a later edit narrowed the check to `not isinstance(relativeError, 
float)`, this `True` case would still pass with `arg_type` `"bool"`, while 
`approxQuantile("a", [0.5], 1)` and `approxQuantile("a", [0.5], 0)` would start 
raising in classic and Spark Connect with CI still green. Could you add a case 
next to this one that passes a plain int, such as `1`, and checks that it 
returns one float per requested probability?
   
   See **Shared repair plan 1** in the review body.



##########
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(PySparkValueError) as pe:
+            df.stat.approxQuantile("a", [0.1, 0.5, 0.9], float("nan"))

Review Comment:
   **Nit (P3):** This pins the NaN side of the rewritten sign test but not the 
boundary it has to keep: `0`, which the docstring documents as the setting for 
exact quantiles. No PySpark test or doctest passes `0` or `0.0`, so if the 
check later became `not relativeError > 0`, every existing assertion (NaN and 
`-0.1` rejected, `0.1` and `0.05` accepted) would still pass while 
`approxQuantile("a", [0.5], 0)` raised `NEGATIVE_VALUE` in classic and Spark 
Connect. Could you add a case with `relativeError=0` that checks it returns one 
float per requested probability? Passing the int `0` covers plain-int 
acceptance as well.
   
   See **Shared repair plan 1** in the review body.



-- 
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]

Reply via email to