spark-test commented on code in PR #60248:
URL: https://github.com/apache/spark/pull/60248#discussion_r4533517153
##########
python/pyspark/sql/dataframe.py:
##########
@@ -78,6 +79,45 @@
__all__ = ["DataFrame", "DataFrameNaFunctions", "DataFrameStatFunctions"]
+def validate_approx_quantile_probabilities(probabilities: Any) ->
List[Union[float, int]]:
Review Comment:
**Nit (P3):** This helper is now a module-level function of
`pyspark.sql.dataframe`, the module that defines the public `DataFrame`
classes, and without a leading underscore it reads as API of that module: `from
pyspark.sql.dataframe import validate_approx_quantile_probabilities` works, and
the name shows up in completion. PySpark follows PEP 8 here (see the
contributing guide), and the other module-level helpers in the public
`pyspark.sql` modules are underscore-prefixed, for example
`pyspark.sql.udtf._validate_udtf_handler` and
`pyspark.sql.pandas.functions._validate_vectorized_udf`, which
`connect/dataframe.py` already imports. The unprefixed names in
`pyspark/sql/utils.py`, its previous home, follow that helper module's own
convention. Could you rename it to `_validate_approx_quantile_probabilities`
and update the imports and calls in `classic/dataframe.py` and
`connect/dataframe.py`? Behavior stays the same.
**Verification:**
- **Inspection:** No PySpark source still refers to the validator by its
unprefixed name, and the parent DataFrame module defines it only under the
underscore-prefixed name.
- **Compatibility:** Invalid and valid probabilities produce the same errors
and results as before the rename on both the classic and Spark Connect clients,
showing that both implementations still reach the shared validator.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -1720,27 +1725,7 @@ def approxQuantile(
)
col = _to_list(self._sc, cast(List["ColumnOrName"], col))
- if not isinstance(probabilities, (list, tuple)):
- raise PySparkTypeError(
- errorClass="NOT_EXPECTED_TYPE",
- messageParameters={
- "arg_name": "probabilities",
- "expected_type": "list or tuple",
- "arg_type": type(probabilities).__name__,
- },
- )
- if isinstance(probabilities, tuple):
- probabilities = list(probabilities)
- for p in probabilities:
- if not isinstance(p, (float, int)) or p < 0 or p > 1:
- raise PySparkTypeError(
- errorClass="NOT_EXPECTED_TYPE",
- messageParameters={
- "arg_name": "probabilities",
- "expected_type": "list[float, int]",
- "arg_type": type(p).__name__,
- },
- )
+ probabilities = validate_approx_quantile_probabilities(probabilities)
Review Comment:
**Nit (P3):** This call now rejects NaN and `True` in `probabilities`, but
the `relativeError` checks just below are unchanged on both clients. From the
code, `approxQuantile("a", [0.5], float("nan"))` still passes the client check
and then fails in the server-side `require(relativeError >= 0)` with an
IllegalArgumentException, and `True` still runs as relative error 1.0. Handling
that in #60306 is fine, but only the review thread says so; a reader of either
`approxQuantile` sees the two arguments treated differently with no sign that
the gap is known. Could you add a short TODO at the `relativeError` checks in
both `classic/dataframe.py` and `connect/dataframe.py` that names NaN and bool
and points to the follow-up (`TODO(SPARK-xxxxx)` with #60306's JIRA, the form
these files use for TODOs, or #60306 itself)? If #60306 merges first, this
isn't needed.
This extends existing discussions
[1](https://github.com/apache/spark/pull/60248#discussion_r4521497450),
[2](https://github.com/apache/spark/pull/60248#pullrequestreview-5837284877).
**Verification:**
- **Inspection:** The relativeError check in each of the classic and Spark
Connect approxQuantile implementations carries a comment naming NaN and bool
relativeError values as a known gap and pointing to the follow-up change, and
the change adds no executable 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]