dongjoon-hyun commented on code in PR #60714:
URL: https://github.com/apache/spark/pull/60714#discussion_r4647023534
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
)
seed = seed if seed is not None else random.randint(0, sys.maxsize)
+ # The JVM method takes a list of Doubles, which a Python int in the
list does not match.
+ double_weights = [float(w) for w in weights]
Review Comment:
nit: `float(w)` raises an unhandled `OverflowError: int too large to convert
to float` for an `int` beyond the double range, e.g. `df.randomSplit([10**400,
10**400])`. Connect normalizes the same input to `[0.5, 0.5]` because `x /
total` is an int/int true division. It is an edge case, but since this PR
aligns classic with Connect for `int` weights, the two still differ here.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
)
seed = seed if seed is not None else random.randint(0, sys.maxsize)
+ # The JVM method takes a list of Doubles, which a Python int in the
list does not match.
+ double_weights = [float(w) for w in weights]
Review Comment:
This iterates `weights` a second time after the validation loop above. The
signature is `List[float]`, so it is outside the contract, but with a one-shot
iterable such as a generator, the loop consumes it and `double_weights` becomes
`[]`, so the JVM fails with `Sum of weights must be positive, but got []`.
Converting once before the loop avoids the second pass and the extra variable:
```python
weights = [float(w) for w in weights]
for w in weights:
if w < 0.0:
...
```
(This would show `-1.0` instead of `-1` as `arg_value` of
`VALUE_NOT_POSITIVE` for a negative `int`.)
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
Review Comment:
The negative-weight check here is duplicated in `connect/dataframe.py`
(`randomSplit`), and this PR adds the `float` conversion only on the classic
side. SPARK-61464 solved the same kind of problem for `approxQuantile` with a
shared helper, `_validate_approx_quantile_probabilities` in
`python/pyspark/sql/dataframe.py`. Could we do the same here, e.g.
`_validate_random_split_weights`, so that both implementations validate and
convert the weights in one place? It would also be the natural place to fix the
parity gaps in the other comments (`sum(weights) <= 0`, `Decimal`, huge `int`).
##########
python/pyspark/sql/tests/test_dataframe.py:
##########
@@ -818,6 +818,18 @@ def test_sample_with_random_seed(self):
cnts = [df.count() for i in range(10)]
self.assertEqual(1, len(set(cnts)))
+ def test_random_split_int_weights(self):
+ # SPARK-62073: integer weights split the rows like the equal float
weights.
+ df = self.spark.range(100)
+ int_splits = df.randomSplit([1, 2], 24)
Review Comment:
`[1, 2]` covers the `java.lang.Integer` path only. Py4J sends an `int` of
`2**31` or more as `java.lang.Long`, which had the same `ClassCastException`.
Could we also cover it, e.g. `[2**40, 2**41]`, and a mix of `int` and `float`
like `[1, 2.0]`? Both should split the rows like `[1.0, 2.0]` with the same
seed.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
)
seed = seed if seed is not None else random.randint(0, sys.maxsize)
+ # The JVM method takes a list of Doubles, which a Python int in the
list does not match.
+ double_weights = [float(w) for w in weights]
df_array = self._jdf.randomSplit(
- _to_list(self.sparkSession._sc, cast(List["ColumnOrName"],
weights)), int(seed)
+ _to_list(self.sparkSession._sc, cast(List["ColumnOrName"],
double_weights)),
Review Comment:
nit: `_to_list` is a helper for a list of Columns, so
`cast(List["ColumnOrName"], ...)` on a list of floats is misleading, and this
PR carries it over to the new variable. Without a converter, `_to_list` only
calls `sc._jvm.PythonUtils.toList(cols)`, so we could call it directly. With
the single-pass conversion in the other comment, `double_weights` would not be
needed either.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
)
seed = seed if seed is not None else random.randint(0, sys.maxsize)
+ # The JVM method takes a list of Doubles, which a Python int in the
list does not match.
+ double_weights = [float(w) for w in weights]
Review Comment:
`float(w)` also makes classic accept `Decimal` weights now:
`df.randomSplit([Decimal(1), Decimal(2)], 24)` succeeds with `[1.0, 2.0]`,
while Connect fails for the same input with `TypeError: unsupported operand
type(s) for +: 'float' and 'decimal.Decimal'` at `normalizedCumWeights[-1] +
v`. So the two implementations diverge in the opposite direction for this
input. Should we either accept only `int` and `float`, or convert with
`float()` on the Connect side too?
##########
python/pyspark/sql/tests/test_dataframe.py:
##########
@@ -818,6 +818,18 @@ def test_sample_with_random_seed(self):
cnts = [df.count() for i in range(10)]
self.assertEqual(1, len(set(cnts)))
+ def test_random_split_int_weights(self):
+ # SPARK-62073: integer weights split the rows like the equal float
weights.
+ df = self.spark.range(100)
+ int_splits = df.randomSplit([1, 2], 24)
+ float_splits = df.randomSplit([1.0, 2.0], 24)
+ self.assertEqual(len(int_splits), 2)
+ self.assertEqual(
+ [sorted(row.id for row in split.collect()) for split in
int_splits],
+ [sorted(row.id for row in split.collect()) for split in
float_splits],
+ )
+ self.assertEqual(sum(split.count() for split in int_splits), 100)
Review Comment:
nit: `split.count()` runs two more Spark jobs although the rows are already
collected above. Keeping the sorted ids in a variable and checking
`sum(len(ids) for ids in int_ids) == 100` would save two jobs in both the
classic and the Connect parity runs.
##########
python/pyspark/sql/classic/dataframe.py:
##########
@@ -751,8 +751,11 @@ def randomSplit(
messageParameters={"arg_name": "weights", "arg_value":
str(w)},
)
seed = seed if seed is not None else random.randint(0, sys.maxsize)
+ # The JVM method takes a list of Doubles, which a Python int in the
list does not match.
+ double_weights = [float(w) for w in weights]
df_array = self._jdf.randomSplit(
Review Comment:
This is not introduced by this PR (`[0.0, 0.0]` already behaves this way),
but there is a related parity gap: classic has no Python-side `sum(weights) >
0` check, so `df.randomSplit([0, 0], 1)` now reaches the JVM and fails with
`IllegalArgumentException: Sum of weights must be positive, ...` through Py4J.
Connect raises `PySparkValueError` with `VALUE_NOT_POSITIVE`
(`arg_name="sum(weights)"`) for the same input. It might be worth checking the
sum on the Python side in classic too.
--
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]