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

   ### What changes were proposed in this pull request?
   
   [SPARK-61405](https://issues.apache.org/jira/browse/SPARK-61405): three 
fixes to the Rust API's `StatFunctions::sample_by` and 
`DataFrame::fillna_value`, to behave as PySpark's `sampleBy` and `fillna` do.
   
   - `sample_by(col, fractions, None)` sends a seed that the client draws 
(`random_seed()`, as `sample` and `random_split` already do), instead of no 
seed.
   - `sample_by` accepts a stratum only if it is a literal of a bool, an 
integer, a float or a string, the Rust kinds of PySpark's `float`, `int` and 
`str` keys. A stratum of another type, or one that is not a literal, is 
`DISALLOWED_TYPE_FOR_CONTAINER` with PySpark's `allowed_types` of `float, int, 
str`, and with the Python name of its type as `item_type` (for example `date`, 
`datetime`, `Decimal`, `bytes`, `NoneType`, `Column`).
   - `fillna_value(Value::Map(..), subset)` fills the columns of the map's keys 
with its values, as PySpark's `fillna` of a dict does, and ignores `subset` as 
PySpark does. It returns PySpark's errors:
     - a key that is not a string: `NOT_EXPECTED_TYPE` for `key type of dict`;
     - a value of another type: `NOT_EXPECTED_TYPE` for `value type of dict`;
     - an empty map: `CANNOT_BE_EMPTY`.
   
     The Python names of the value types move from `check_fill_value` into a 
`python_type_name` function that both errors use.
   
   ### Why are the changes needed?
   
   - Without a seed, the server draws one (`Utils.random.nextLong`) each time 
it plans the relation. So one DataFrame of `sample_by(.., None)` returned 
different rows in different actions: `count()` changed from call to call and 
disagreed with `collect()`, and a self-join paired two different samples.
     - Over `range(0, 200, 1, 2)` with strata `id % 2` at 0.5 each, on a local 
Spark 4.3.0 server, three `count()`s gave `[108, 101, 101]` and `collect()` 
gave 109 rows.
     - PySpark draws the seed on the client: `seed = seed if seed is not None 
else random.randint(0, sys.maxsize)`.
     - The drop-in always passed a seed, so it was not affected.
   - The server reads a stratum by its Catalyst internal value, and Spark's 
`sampleBy` looks it up by the external value of the column. So a date, 
timestamp, decimal or binary stratum matched no row, and the sample was empty 
without an error (0 of 1 row at fraction 1.0 for each of them). PySpark takes 
only `float`, `int` and `str` keys and raises `DISALLOWED_TYPE_FOR_CONTAINER` 
for others.
   - `fillna_value` of a map raised `[NOT_EXPECTED_TYPE] Argument `value` 
should be bool, dict, float, int or str, got dict.`, which names `dict` both as 
an accepted type and as the rejected one. PySpark's `fillna` takes a dict.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Rust API:
   
   - A sample of `sample_by` without a seed returns the same rows in every 
execution. Its rows are those of a seed that the client draws, so they differ 
from before; before, they changed with each execution anyway.
   - A stratum of a type other than bool, integer, float or string is an error 
instead of an empty sample.
   - `fillna_value` of a map of string keys fills those columns.
   
   The Python client is not changed.
   
   ### How was this patch tested?
   
   - New unit tests in `crates/spark-connect/src/group.rs`, placed before 
`sample_by_rejects_a_stratum_that_is_not_a_literal_as_pyspark`:
     - `sample_by_takes_a_seed_of_the_client_without_one_as_pyspark`: the 
relation carries the given seed, and a non-negative seed of the client without 
one, which stays the same for the DataFrame.
     - `sample_by_takes_the_strata_types_of_pyspark`: the accepted literal 
kinds, and the rejected ones with their `item_type` and `allowed_types`.
   - `sample_by_rejects_a_stratum_that_is_not_a_literal_as_pyspark` now expects 
PySpark's `[float, int, str]`.
   - A new unit test in `crates/spark-connect/src/dataframe.rs`, placed after 
`fill_values_are_those_of_pyspark_and_sent_as_its_literals`: 
`a_map_fill_value_fills_its_columns_as_pyspark` checks the FillNa of a map (its 
columns and values, without the subset), and the three errors with their 
messages.
   - A new live test in `crates/spark-connect/tests/e2e_new_methods.rs`, placed 
after `stat_corr_cov_sample_by`: 
`sample_by_without_a_seed_returns_the_same_rows_in_every_execution_as_pyspark` 
checks that three `count()`s and a `collect()` of one sample without a seed 
agree, against a local Spark 4.3.0 server.
   - With master's `sample_by` and `fillna_value` put back, the four unit tests 
above fail, and the live test fails with `[108, 101, 101] and 109`. With the 
fix, all of them pass.
   - The lib tests of `sample_by`, `fill` and `stat_`; `e2e_new_methods` 
against the server (9 passed); the build of all the tests without warnings; 
`cargo fmt --all --check`. Clippy reports nothing in the changed lines.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 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