tdcmeehan opened a new pull request, #449:
URL: https://github.com/apache/spark-connect-rust/pull/449

   ### What changes were proposed in this pull request?
   
   The Python client (`crates/pyspark-rs`) now draws the seed of 
`DataFrame.sample`, `DataFrame.randomSplit`, `DataFrame.sampleBy` and 
`DataFrameStatFunctions.sampleBy` from Python's `random` when the caller gives 
none, as PySpark 4.3.0's Spark Connect client does:
   
   - `python_random_seed` in `crates/pyspark-rs/src/dataframe.rs` calls 
`random.randint(0, sys.maxsize)`, the same call PySpark makes.
   - `sample` draws in the branches that PySpark's `_preapare_args_for_sample` 
draws in, after the arguments are matched, so a call that raises draws nothing.
   - `randomSplit` draws after the negative-weight check and before the sum 
check, as PySpark does, so a sum that is not positive uses one draw and a 
negative weight uses none.
   - `sampleBy` (both entry points) draws after `sample_by_arguments` validates 
its arguments.
   
   An explicit seed is sent as given and draws nothing. The core's Rust API 
default seed in `crates/spark-connect` is unchanged.
   
   ### Why are the changes needed?
   
   Without a seed, the Python client took a UUID-based seed for `sample` and 
`randomSplit`, and sent none for `sampleBy`. So `random.seed(...)` had no 
effect on their rows, and two processes returned different rows for the same 
seed. PySpark 4.3.0 returns the same rows, and this change makes the drop-in do 
the same.
   
   Repro against an OSS Spark 4.3.0 Connect server on loopback, 
`random.seed(42)` before each call, run in two processes each:
   
   - Before: `sample(0.5)` gave `[2, 3, 8, 10, ...]` and then `[1, 2, 3, 4, 
...]`; `randomSplit` and `sampleBy` differed between processes too.
   - After: `sample`, `randomSplit` and `sampleBy` give the same rows in both 
processes, and the same rows as the reference client: `[1, 2, 5, 7, 11, 12, 14, 
15, 17, 19, 26, 29, 34, 35, 38]` for `sample` and for `sampleBy`, and the same 
two splits for `randomSplit`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Python client only. Without a seed, `sample`, `randomSplit` and 
`sampleBy` now return the same rows for the same `random.seed(...)`, as PySpark 
does, instead of different rows in each process. The rows of a call with an 
explicit seed are unchanged. The Rust API is unchanged.
   
   ### How was this patch tested?
   
   - Offline, `python/tests/test_dropin_offline.py`: 
`test_sample_split_and_sample_by_draw_their_seed_from_python_random_as_pyspark`.
 After `random.seed(42)`, the seed of each call in a fixed order equals the 
draw of `random.randint(0, sys.maxsize)` in that order. It also checks that an 
explicit seed draws nothing, that a `randomSplit` whose sum is not positive 
uses one draw, and that a negative weight uses none. The expected draws come 
from Python's `random` in the test. It fails on the previous code.
   - Live, `python/tests/test_dropin_e2e.py`: 
`test_sample_split_and_sample_by_without_a_seed_follow_python_random_as_pyspark`.
 It runs the same script in two new interpreters and checks that both give the 
rows of the reference client. It fails on the previous code.
   - Reference check: the same call sequence on PySpark 4.3.0 gives the same 
seeds that the offline test expects, and the same rows as the e2e test.
   - `cargo test -p apache-spark-connect --release --lib`: 528 passed.
   - `cargo test -p apache-spark-connect --release --no-run` and `--features 
wasm-udf --no-run`: compile.
   - `cargo fmt --all --check`: passes.
   - `cargo clippy --workspace --all-targets --release --message-format short`: 
fails with pre-existing diagnostics in files this change does not touch; none 
are on lines changed here.
   - Offline Python (`test_dropin_offline.py`, `test_ml_offline.py`, 
`test_arrow_handoff.py`): 2928 passed, 8 skipped.
   - Live Python (`test_arrow_e2e.py`, `test_dropin_e2e.py`, `test_ml_e2e.py`): 
237 passed, 1 skipped.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5 and Claude Haiku 5.5
   
   This pull request and its description were written by Isaac.
   


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