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

   ### What changes were proposed in this pull request?
   
   This is the third sub-task of SPARK-62207, which closes the rows "The 
`__dict__` of the objects of a session" and "Subclasses" of 
`dev/parity/known-differences.md`. This PR covers the four objects of a 
DataFrame that hold the DataFrame itself: the `GroupedData` of `groupBy`, 
`rollup`, `cube`, `groupingSets` and `pivot`, the `PandasCogroupedOps` of 
`cogroup`, and the `DataFrameNaFunctions` and `DataFrameStatFunctions` of 
`df.na` and `df.stat`.
   
   In PySpark these are plain Python classes. `__init__` stores its arguments 
as instance attributes:
   - `GroupedData`: `_df`, `_group_type`, `_grouping_cols`, `_pivot_col`, 
`_pivot_values` (the `F.lit` of each pivot value) and `_grouping_sets`, after 
its assertions;
   - `PandasCogroupedOps`: `_gd1` and `_gd2`, after 
`gd1._df._check_same_session(gd2._df)`;
   - `DataFrameNaFunctions` and `DataFrameStatFunctions`: `df`.
   
   Their methods read those attributes each time they run: the grouped data 
build their `Aggregate`, `GroupMap` or `CoGroupMap` plan of them, and every 
method of `na` and `stat` calls the method of the same name of `self.df`. In 
the drop-in, these classes held a native clone of the plan and of the grouping 
instead. As a result:
   - `vars()` was empty, and only `_df`, `_pivot_values` and `df` were 
readable, as getters;
   - an assignment of the state had no effect on the plans;
   - none of the four could be constructed or subclassed (`cannot create`, `not 
an acceptable base type`);
   - `na` and `stat` ran the native method of the DataFrame they were made of, 
so a proxy or a subclass of a DataFrame was not called.
   
   The object-state test file of SPARK-62208 compares the objects with goldens 
captured from PySpark 4.3.0:
   
   | | Passed | Strict xfails |
   |---|---|---|
   | Before, master ab03fcff | 164 | 259 |
   | After, this PR | 221 | 207 |
   
   The 52 generic cases that this PR fixes are all of the four classes: their 
`vars`, `attributes`, `copies`, `construct`, `uninit` and `foreign` cases all 
pass, and their group is deleted from `_DIFFERS_BY_CHANGE`. The other 5 passing 
tests are new (below). The remaining 207 belong to the later sub-tasks.
   
   **The changes:**
   - **`GroupedData`** (`crates/pyspark-rs/src/group.rs`):
     - The native object is empty. `__init__` makes PySpark's assertions, a 
bare `AssertionError` of a `df` that is not a DataFrame, a group type that is 
not one of PySpark's names, grouping columns that are not a list of Columns, a 
pivot of no Column or of values that are not a list, and grouping sets that are 
not a list. It then sets PySpark's six attributes in PySpark's order, with the 
pivot values as the `F.lit` of each.
     - `agg` and the numeric aggregations read `_df`, `_group_type`, 
`_grouping_cols`, `_pivot_col`, `_pivot_values` and `_grouping_sets` at the 
call, with the assertions of PySpark's `Aggregate` (a pivot column on a 
grouping of another type, say). `applyInPandas`, `applyInArrow`, 
`applyInPandasWithState` and the `transformWithState` pair read `_df` and 
`_grouping_cols`, as PySpark's `GroupMap` does. The initial state of 
`transformWithState` may be any object of a `_df` and `_grouping_cols`, as in 
PySpark.
     - The methods that PySpark builds from others call those methods on 
`self`: `count` calls `self.agg(...)`, and `apply` calls 
`self.applyInPandas(...)`.
     - `pivot` reads `_group_type` first, so `UNSUPPORTED_OPERATION` names the 
type that the grouped data hold (`PIVOT after ROLLUP`, `Repeated PIVOT 
operation`). It then makes `GroupedData(df=self._df, group_type="pivot", 
grouping_cols=self._grouping_cols, pivot_col=self._df[pivot_col], 
pivot_values=values)`, of the base class, which shares the DataFrame and the 
list of grouping columns, as PySpark's.
     - `__repr__` is built of `str(c._expr)` of each grouping column, 
`self._df.dtypes` and `_group_type`, as PySpark's.
     - A `_df` that is not a DataFrame, such as a proxy of one, is grouped as 
`DataFrame(_df._plan, _df._session)`, with PySpark's `AttributeError` of an 
object that has no `_plan`.
   - **`PandasCogroupedOps`:** `__init__(gd1, gd2)` checks 
`gd1._df._check_same_session(gd2._df)`, natively for a drop-in DataFrame, and 
sets `_gd1` and `_gd2`. `applyInPandas` and `applyInArrow` read `_df` and 
`_grouping_cols` of each, of any object.
   - **`DataFrameNaFunctions` and `DataFrameStatFunctions`:** `__init__(df)` 
sets `df`, and each method calls the method of `self.df` as PySpark calls it: 
`drop` is `self.df.dropna(how=how, thresh=thresh, subset=subset)` of PySpark's 
default `how="any"`, `fill` is `self.df.fillna(value=value, subset=subset)`, 
and `replace` is `self.df.replace(to_replace, value, subset)`, of PySpark's 
`_NoValue` for an omitted value. `DataFrame.replace` takes `_NoValue` for an 
omitted value, as PySpark's does.
   - **`DataFrame`:** `groupBy`, `groupby`, `rollup`, `cube`, `groupingSets`, 
`na` and `stat` make the objects of PySpark's constructors, of the DataFrame 
object: `GroupedData(df=self, group_type=..., grouping_cols=..., 
grouping_sets=...)`, of lists of Columns, and `DataFrameNaFunctions(self)`.
   - **Shared by all four:**
     - `__new__` takes any arguments, and the signature is PySpark's.
     - `__getstate__` returns the `__dict__`, and `__reduce_ex__` is that of a 
plain object. A copy shares the DataFrame and the lists, a deep copy and a 
pickle copy them, and a pickle reaches the DataFrame first, as PySpark's does.
     - The previous getters, `__copy__`, `__reduce__`, `_of_state`, 
`__traverse__` and `__clear__` are removed, and so are `grouped_reduce` and 
`cogrouped_reduce` of `_state_reduce.py`.
     - All four can be subclassed. An object whose `__init__` never ran raises 
the `AttributeError` of the first attribute that PySpark's method reads (`_df`, 
`_gd1`, `df`).
   - **Garbage collection:** the four objects hold no `Parts` of their own any 
more. Their DataFrame and Columns are in their `__dict__`, which the collector 
traverses, and those hold their parts, as gc-shared's design has it. The 
numeric aggregations build grouped data of the core at the call, and their 
requests without the GIL name the parts of that value 
(`interruptible_of_values`).
   - **Docs:**
     - `dev/parity/known-differences.md`: the `__dict__` row lists the four 
classes as PySpark's, with the residue below; the Subclasses row lists them 
among the subclassable classes, with their assertions and their first 
attributes, and drops them from the native list; the "Public base and native 
MRO" row no longer counts the `df` of `na` and `stat`, which `__init__` now 
sets, so a data descriptor without a setter patched over it on the classic 
class raises as in PySpark.
     - `docs/compatibility.md` gets the same changes, a paragraph on the four 
classes, and the garbage-collection paragraph says that they hold their 
DataFrames and Columns in their `__dict__`.
   
   **What remains** is the inherent residue (a) of the design: the plan that a 
method of grouped data makes is a value of the core, made of the state at the 
call. A later change of the lists of that state, such as 
`gd._grouping_cols.clear()` after `agg = gd.count()`, does not change `agg`, 
where PySpark's `Aggregate` and `GroupMap` hold the lists and build their 
messages at each request. The plans are the core's ("Plans of a DataFrame's 
relation"). `na` and `stat` have no residue: they call the methods of their 
`df`.
   
   ### Why are the changes needed?
   
   In PySpark, code that reads or writes the state of grouped data and of `na` 
and `stat` works because the state is in their `__dict__`: `vars()`, an 
assignment of `_grouping_cols`, a subclass of `GroupedData` whose `agg` is its 
own, a proxy of a DataFrame given to `DataFrameNaFunctions`, copies and 
pickles. So do `GroupedData(df, "groupby", [F.col("id")])` and 
`PandasCogroupedOps(gd1, gd2)`. Until now, none of this worked in the drop-in.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The four classes keep their state in their `__dict__`, as PySpark's do:
   - `vars(df.na)` is `{'df': df}`, and `vars(df.groupBy("id"))` holds 
PySpark's six entries;
   - an assignment of the state, or a subclass's property or method, changes 
what the methods use;
   - the four classes can be constructed from PySpark's arguments, and 
subclassed;
   - `na` and `stat` call the methods of their `df`, of any object;
   - copies and pickles carry the `__dict__`.
   
   ### How was this patch tested?
   
   - **`tests/test_dropin_object_state_offline.py`:** the group of the four 
classes is deleted from `_DIFFERS_BY_CHANGE`: 221 passed and 207 xfailed on 
this branch, and 428 passed with PySpark 4.3.0. Five new tests cover what this 
PR adds, and pass with both clients:
     - `GroupedData` asserts its arguments, and keeps the `F.lit` of its pivot 
values;
     - `pivot` makes grouped data of the base class, of the same DataFrame and 
list of grouping columns, and raises `UNSUPPORTED_OPERATION` of the group type 
that it reads;
     - the aggregations of grouped data are of the state that they hold then 
(the sent `Aggregate` of an assigned grouping), with the assertions of the 
plan, of a proxy of a DataFrame too, and `count` calls `agg` of a subclass;
     - `PandasCogroupedOps` takes any grouped data of the same session, and 
maps them;
     - `na` and `stat` call the methods of their `df` with PySpark's arguments, 
`_NoValue` included, and work of a proxy of a DataFrame.
   - **`tests/test_dropin_e2e.py`:** a new live test,
     
`test_grouped_data_na_and_stat_make_their_dataframes_of_their_dict_as_pysparks`.
 Against a Spark 4.3.0 server, it aggregates grouped data of an assigned 
grouping, of PySpark's constructor and of a pivot, maps the cogroups of grouped 
data of another class, and runs `na` and `stat` of a proxy of a DataFrame. It 
passes with both clients.
   - **The gates:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`;
     - `cargo test -p pyspark-rs --lib`;
     - ruff 0.14.0, the no-stub audit, `gen_type_stubs.py --check`, 
`gen_fn_dispatch.py --check`, the license checks, the guards, and 
`test_type_stubs_offline.py`;
     - the offline suite of `coverage.yml`: 7313 passed, 121 skipped, 207 
xfailed;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server: 353 passed, and the 
new test.
   
   ### 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