dongjoon-hyun commented on code in PR #60306:
URL: https://github.com/apache/spark/pull/60306#discussion_r4533564597
##########
python/pyspark/sql/dataframe.py:
##########
@@ -78,6 +79,31 @@
__all__ = ["DataFrame", "DataFrameNaFunctions", "DataFrameStatFunctions"]
+def validate_approx_quantile_relative_error(relativeError: Any) -> float:
+ """
+ Validate the ``relativeError`` argument of ``DataFrame.approxQuantile``,
shared by the
+ classic and Connect implementations so both stay in sync. Returns it as a
``float``.
+ """
+ if isinstance(relativeError, bool) or 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 not relativeError >= 0:
+ raise PySparkValueError(
+ errorClass="NEGATIVE_VALUE",
+ messageParameters={
+ "arg_name": "relativeError",
+ "arg_value": str(relativeError),
+ },
+ )
+ return float(relativeError)
Review Comment:
**[3] Minor: a huge int still escapes as a raw `OverflowError`.**
This predates the PR, but the check now lives in one new helper. An int such
as `10**400` passes both checks and then fails here with `OverflowError: int
too large to convert to float` instead of a PySpark error. The type check at
L87 is also uneven for numpy scalars: `np.float64` passes because it subclasses
`float`, while `np.int64` is rejected with `NOT_EXPECTED_TYPE`. Could the
helper turn the `OverflowError` into a `PySparkValueError`, and is rejecting
`np.int64` intended?
##########
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:
**[4] Nit: `col` and `probabilities` reach the JVM before `relativeError` is
checked.**
This order predates the PR. L1726 and L1749 convert `col` and
`probabilities` to JVM lists through `_to_list` (`PythonUtils.toList` via py4j)
before this line validates `relativeError`, so an invalid `relativeError` costs
two py4j calls that are thrown away. Could the two `_to_list` calls move down
next to `self._jdf.stat().approxQuantile(...)`, so all three arguments are
validated first, in the same order as the Connect side?
--
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]