This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 2eca51ca18c9 [SPARK-61464][PYTHON] Fix `ClassCastException` in classic
`approxQuantile` with int probabilities
2eca51ca18c9 is described below
commit 2eca51ca18c93ea303c8a11203f401b096799490
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Wed Nov 11 16:38:19 2026 -0800
[SPARK-61464][PYTHON] Fix `ClassCastException` in classic `approxQuantile`
with int probabilities
### What changes were proposed in this pull request?
This PR aims to fix `ClassCastException` in the classic
`DataFrame.approxQuantile` when `probabilities` contains Python `int` values
like `0` and `1`. To do that, the shared
`_validate_approx_quantile_probabilities` helper returns the probabilities as a
list of `float`, as `relativeError` is already converted with
`float(relativeError)`.
### Why are the changes needed?
Py4J passes a Python `int` as `java.lang.Integer`, and the Python-friendly
`DataFrameStatFunctions.approxQuantile(cols: List[String], probabilities:
List[Double], relativeError: Double)` fails at `probabilities.toArray` because
it cannot unbox `java.lang.Integer` to `Double`. The validation accepts `int`
(its error message says `list[float, int]`), and `0` and `1` are the only valid
`int` values, so natural calls such as `[0, 0.5, 1]` fail. Spark Connect is not
affected because it put [...]
This has been the case since the Python API was added by SPARK-13479 in
[v2.0.0](https://github.com/apache/spark/releases/tag/v2.0.0) (2016-07-19). For
example, on [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0)
(2026-10-27):
```python
>>> df = spark.range(10)
>>> df.approxQuantile("id", [0.0, 0.5, 1.0], 0.0)
[0.0, 4.0, 9.0]
>>> df.approxQuantile("id", [0, 0.5, 1], 0.0)
...
java.lang.ClassCastException: class java.lang.Integer cannot be cast to
class java.lang.Double (java.lang.Integer and java.lang.Double are in module
java.base of loader 'bootstrap')
```
### Does this PR introduce _any_ user-facing change?
Yes. `int` probabilities work in the classic `approxQuantile` like in Spark
Connect, instead of failing with `ClassCastException`.
### How was this patch tested?
Pass the CIs with the newly added test case in `test_approxQuantile`, which
runs on both classic and Spark Connect via `test_parity_functions.py`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60346 from dongjoon-hyun/SPARK-61464.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 99a5f5fc987f53c64cb19d06c72722870f415305)
Signed-off-by: Dongjoon Hyun <[email protected]>
skip-checks: true
---
python/pyspark/sql/dataframe.py | 7 ++++---
python/pyspark/sql/tests/test_functions.py | 1 +
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/python/pyspark/sql/dataframe.py b/python/pyspark/sql/dataframe.py
index 96d67b354068..576b3550f97a 100644
--- a/python/pyspark/sql/dataframe.py
+++ b/python/pyspark/sql/dataframe.py
@@ -78,10 +78,11 @@ if TYPE_CHECKING:
__all__ = ["DataFrame", "DataFrameNaFunctions", "DataFrameStatFunctions"]
-def _validate_approx_quantile_probabilities(probabilities: Any) ->
List[Union[float, int]]:
+def _validate_approx_quantile_probabilities(probabilities: Any) -> List[float]:
"""
Validate the ``probabilities`` argument of ``DataFrame.approxQuantile``,
shared by the
- classic and Connect implementations so both stay in sync. Returns it as a
``list``.
+ classic and Connect implementations so both stay in sync. Returns it as a
``list`` of
+ ``float``, since classic passes it to a JVM method that expects ``Double``
elements.
"""
from pyspark.errors import PySparkTypeError, PySparkValueError
@@ -116,7 +117,7 @@ def _validate_approx_quantile_probabilities(probabilities:
Any) -> List[Union[fl
"actual": str(p),
},
)
- return probabilities
+ return [float(p) for p in probabilities]
class DataFrame:
diff --git a/python/pyspark/sql/tests/test_functions.py
b/python/pyspark/sql/tests/test_functions.py
index 260af6692170..ab6a61bd5278 100644
--- a/python/pyspark/sql/tests/test_functions.py
+++ b/python/pyspark/sql/tests/test_functions.py
@@ -1630,6 +1630,7 @@ class FunctionsTestsMixin:
self.assertTrue(isinstance(aqt[1], list))
self.assertEqual(len(aqt[1]), 3)
self.assertTrue(all(isinstance(q, float) for q in aqt[1]))
+ self.assertEqual(df.stat.approxQuantile("a", [0, 0.5, 1], 0.0), [0.0,
4.0, 9.0])
self.assertRaises(TypeError, lambda: df.stat.approxQuantile(123, [0.1,
0.9], 0.1))
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))
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]