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

   ### What changes were proposed in this pull request?
   
   A follow-up of SPARK-62071, which made the classic modules of the objects of 
a session PySpark's. Five classic names still named the drop-in's native 
Connect classes, and the classic `Window` was a native class: 
`pyspark.sql.Column`, `DataFrameNaFunctions`, `DataFrameStatFunctions`, 
`WindowSpec` and `pyspark.sql.table_arg.TableArg`. In PySpark 4.3.0 each is a 
classic Python class, which its Connect class subclasses (`class 
Column(ParentColumn)` in `pyspark/sql/connect/column.py`, and likewise of the 
others). Each row was probed on both clients against a Spark 4.3.0 Connect 
server:
   
   | | before | PySpark 4.3.0, and after |
   |---|---|---|
   | `pyspark.sql.Column is pyspark.sql.connect.column.Column`, and of the 
other four | True | False |
   | `__module__` and `__qualname__` of the classic class | 
`pyspark.sql.connect.column`, `Column` (`.dataframe`, `.window`, `.table_arg`) 
| `pyspark.sql.column`, `Column` (`pyspark.sql.dataframe`, 
`pyspark.sql.window`, `pyspark.sql.table_arg`) |
   | MRO of the classic `Column` and `TableArg` | the native class, `object` | 
the class, `TableValuedFunctionArgument`, `object` |
   | MRO of `TableValuedFunctionArgument` | the class, `abc.ABC`, `object` | 
the class, `object` |
   | `isinstance(F.col("a"), pyspark.sql.Column)`, of `df.na`, `df.stat`, a 
WindowSpec and `df.asTable()` too; `issubclass` of their Connect classes | True 
| True |
   | docstring of the classic class | the native class's (`Python wrapper for a 
Spark Column.`) | PySpark's (`A column in a DataFrame.`) |
   | `pyspark.sql.Column()`, `WindowSpec(None)` | `TypeError: cannot create 
'...' instances` | `ModuleNotFoundError: No module named 'pyspark.sql.classic'` 
|
   | `DataFrameNaFunctions(df)`, `DataFrameStatFunctions(df)`, `TableArg()` | 
`TypeError: cannot create '...' instances` | an object of the classic class (of 
`vars` `{'df': df}`) |
   | `class C(pyspark.sql.Column)`, and of the other four | `TypeError: type 
'...' is not an acceptable base type` | a subclass, of PySpark's MRO, which a 
UDF can use in the server's Python worker |
   | `Column.alias(object(), "x")` | `TypeError` of a native descriptor | 
`NOT_IMPLEMENTED` (`dispatch_col_method`) |
   | `DataFrameNaFunctions.drop(df.na)`; `DataFrameNaFunctions.drop(df)` | a 
DataFrame; `TypeError` of a native descriptor | `NOT_IMPLEMENTED` 
(`dispatch_df_method`); `df.drop()` |
   | `WindowSpec.partitionBy(spec, "id")` | a WindowSpec | None (PySpark's 
classic method has an empty body) |
   | `pyspark.sql.Window`, `Window.partitionBy` | a native class, native static 
methods | PySpark's class, whose static methods dispatch to PySpark Connect's 
`Window` |
   | `pyspark.sql.Column.f = g`, then `F.col("a").f()` | `g` | `g`, as the 
Connect objects inherit it |
   | `pyspark.sql.Column.alias = g`, then `F.col("a").alias("x")` | `g` | the 
Connect class's `alias` |
   
   How:
   - **The classic classes are PySpark's.** `python/pyspark/sql/column.py` and 
`table_arg.py` are vendored from v4.3.0 as they are, under the drift gate, the 
ruff excludes and the coverage omits. `window.py` is PySpark's file, with the 
metaclass on `WindowSpec` and the native `FrameBound` (the drop-in's name of 
the frame bounds) added. `dataframe.py` gets PySpark's `DataFrameNaFunctions` 
and `DataFrameStatFunctions`, with the metaclass, after its public DataFrame, 
and imports PySpark's classic `Column` and `TableArg` for its annotations, as 
PySpark's module does. `tvf_argument.py` is PySpark's class with the metaclass, 
in place of an `abc.ABC`. Their methods dispatch through PySpark's 
`dispatch_col_method`, `dispatch_df_method`, `dispatch_window_method` and 
`dispatch_table_arg_method`, of `pyspark.sql.utils`, which SPARK-62074 made 
PySpark's module.
   - **The native Connect classes are their virtual subclasses.** The new 
`pyspark/sql/_classic_bases.py` has the metaclass of the classic classes, an 
`ABCMeta` subclass, as the public DataFrame's is. When PySpark's class of a 
native Connect class is made, it registers the native class, of 
`pyspark._pyspark`, as its virtual subclass.
     - A patch of a classic class or of `TableValuedFunctionArgument` reaches 
the native objects as it reaches PySpark's Connect objects through their MRO. A 
name that PySpark's Connect class defines itself (of the lists the module 
keeps, taken from PySpark 4.3.0) is not forwarded. Neither is a name that the 
objects set on themselves (`_expr`, `df`), unless the patch is a data 
descriptor. A patch of the class nearer the object wins, and a patch of the 
native class itself hides one of a classic class. Deleting a patch gives the 
native class its own attribute back.
     - A class that a user makes of a classic class is of the standard 
`abc.ABCMeta`, which a by-value cloudpickle of it names and PySpark's Python 
workers import. It is of the caller's module, also of `type(name, bases, {})`.
   - **The Connect modules export the native classes.** 
`pyspark.sql.connect.dataframe`, `table_arg` and `window` export the native 
`DataFrameNaFunctions`, `DataFrameStatFunctions`, `TableArg` and `WindowSpec`, 
as `pyspark.sql.connect.column` exports the native `Column`. 
`pyspark.sql.connect.window.Window` is PySpark Connect's code: a subclass of 
PySpark's classic `Window`, whose static methods make WindowSpecs of `_spec`, 
the native specification of no partitioning, ordering and frame.
   - **The native Window is removed.** `window.rs` loses the native `Window` 
class, which PySpark's classic `Window` and PySpark Connect's replace, for 
`_empty_window_spec`, which makes the `_spec` of the Connect `Window`.
   - **The stubs.** `scripts/gen_type_stubs.py` no longer stubs the vendored 
`pyspark.sql.column` and `pyspark.sql.table_arg`, and the docstring golden is 
captured again. `pyspark.sql.window` keeps its stub, for `FrameBound`.
   - **The names that `_BINDINGS` keeps are checked against PySpark's source** 
(the second commit). The new `scripts/check_classic_bindings.py` parses 
PySpark's source, and imports none of it. Of each Connect class of `_BINDINGS`, 
it works out the names of the class's own `__dict__` (those that its body 
binds, its `__module__` and `__doc__`, and the `__hash__` of a body of 
`__eq__`) and the attributes that its `__init__` sets on `self`. It checks too 
that the bases of the Connect class are the classic class alone, and that the 
bases of the classic classes are those of `_PUBLIC_BASES`. It fails with the 
difference and the names to keep. `generated-drift.yml` runs it against the 
PySpark of the tracked tag, after the check of the docstrings, and 
`docs/contributing.md` describes it.
   
   What stays different, documented in `docs/compatibility.md` and 
`dev/parity/known-differences.md`:
   - The MRO of an object of a session is its native class and `object`, where 
PySpark's has the classic class and its bases after the Connect class: a native 
class cannot have a Python base.
   - The classic classes are of an `ABCMeta` subclass, of an `_abc_impl` among 
their attributes and a `register()`, and the subclasses that a user makes are 
of `abc.ABCMeta`, where PySpark's are of `type`.
   - A data descriptor without a setter patched over `_expr` or `df` makes 
PySpark's `__init__` of a Connect object raise, where the drop-in's objects, 
which no Python `__init__` makes, read the patch.
   - A native method called on an object of another class raises CPython's 
`TypeError` of a descriptor, such as `TableArg.partitionBy(object(), "id")`, 
which `dispatch_table_arg_method` sends to the Connect class's method, where 
PySpark's raises the `AttributeError` of `_is_partitioned`.
   
   ### Why are the changes needed?
   
   So that the classic classes of a Column, the `na` and `stat` of a DataFrame, 
a window specification and a table argument are PySpark's, as in 
pyspark-client: their identity, module, name, MRO, docstrings, construction, 
subclassing and the dispatch of their explicit calls. pandas-on-Spark calls the 
classic `Column`'s methods through `pyspark_column_op`, and a library that 
patches `pyspark.sql.Column` reaches the Columns of a session as with 
pyspark-client.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as listed above. `pyspark.sql.Column` and the others are PySpark's 
classes, so `pyspark.sql.Column is pyspark.sql.connect.column.Column` is False, 
`type(F.col("a")) is pyspark.sql.Column` is False, the classic classes 
construct and subclass as PySpark's, and their explicit calls dispatch as 
PySpark's. `isinstance` checks, the objects of a session and their methods are 
unchanged.
   
   ### How was this patch tested?
   
   - **The rows of the table** were probed on pyspark-client 4.3.0 and on the 
drop-in, before and after this change, against a Spark 4.3.0 Connect server. 
After it, the drop-in differs from pyspark-client only as listed under "What 
stays different".
   - **New tests that fail on master:**
     - In `python/tests/test_dropin_class_modules_offline.py`, 5 tests (9 
cases):
       - of each of the five classes, the classic class is PySpark's, of its 
module, name, MRO and docstring, and is not the native Connect class, which is 
its virtual subclass, of `isinstance` and `issubclass`;
       - the classic classes construct as PySpark's (`Column()` and 
`WindowSpec(None)` raise PySpark's `ModuleNotFoundError`; 
`DataFrameNaFunctions(df)`, `DataFrameStatFunctions(df)`, `TableArg()` and 
`Window()` make objects of the classes), and subclass as PySpark's, also of 
`type(name, bases, {})`: of PySpark's MRO and the caller's module, and of a 
metaclass that a by-value cloudpickle names and PySpark's Python workers import;
       - the explicit calls of the classic methods dispatch as PySpark's: to 
the Connect class's method of an object of it, and `NOT_IMPLEMENTED` of 
another; those of the classic `WindowSpec` return None;
       - `pyspark.sql.connect.window.Window` is PySpark Connect's class;
       - a patch of a classic class or of `TableValuedFunctionArgument` reaches 
the objects of a session as through PySpark's MRO, and its deletion restores 
the native attribute.
     - In `test_dropin_e2e.py`, 2 tests, of a live session: 
`test_the_classic_column_window_and_table_arg_classes_are_pysparks`, and 
`test_subclasses_of_the_classic_classes_run_in_the_python_workers_as_pysparks`, 
a UDF that uses subclasses of the classic `Column`, `WindowSpec` and 
`TableArg`. The same bodies pass with pyspark-client 4.3.0.
   - **Tests of the pickles of Columns and table arguments with the classic 
classes in place**, which pass on master too:
     - `test_pickled_columns_and_table_args_load_as_their_classes_as_pysparks` 
(offline): `pickle` and `cloudpickle` give back a Column and a table argument 
(partitioned, ordered, of a single partition) of the same native class, of the 
same plan, and an instance of the classic class;
     - 
`test_columns_and_table_args_that_udfs_use_load_in_the_python_workers_as_pysparks`
 (e2e): a UDF that uses a Column, and one that uses a table argument, load them 
in the server's Python worker as PySpark Connect's, instances of the classic 
classes there. The same body passes with pyspark-client 4.3.0. PySpark 4.3.0's 
`pyspark.sql.connect.table_arg` imports itself through 
`pyspark.sql.connect.functions`, so a worker that loads a table argument first 
fails with the same `ImportError` on both clients, and the UDF of the table 
argument loads the functions first;
     - a subclass of a classic class gives itself back of a `cloudpickle` 
(offline), and a UDF of subclasses runs in the server's Python worker (the e2e 
test above).
   - **The script.** `scripts/tests/test_check_classic_bindings.py`, 12 tests, 
of the names that the script works out of a class body and of an `__init__`, of 
the bindings that it reads, and of its check on PySpark source trees of a few 
modules: its pass, the difference that it prints, a Connect class of relative 
imports, and its errors. A copy of `_classic_bases.py` with a name added to, 
and one taken out of, a binding, an instance name added and another public base 
fails the check with each difference.
   - **Updated tests.** `test_dropin_offline.py`, `test_ml_offline.py` and 
`test_type_stubs_offline.py` reached the native classes through the classic 
paths, or the stubs of `pyspark.sql.column` and `pyspark.sql.table_arg`, which 
are PySpark's modules now.
   - **The official suites of `build_python_connect.yml`:**
     - the drop-in suite (`run_official_tests.py --dropin`, 118 files), on this 
change's first base (SPARK-62071 and SPARK-62074 as they were gated) and the 
change, back to back on the same server: 97 files pass, the manifest skips 14, 
and 7 have the same 43 failures (2720 passed, 43 failed, 496 skipped). The 
failures are those of this environment: the server ran outside a Spark source 
tree, so the test data that the tests read by relative paths 
(`python/test_support/...`, `data/mllib/...`) does not exist there. One more 
file timed out on the base, `test_parity_pandas_transform_with_state.py`, which 
passes in 357 of its 360 seconds on this machine;
     - after the rebase onto master, the 19 official files that use these 
classes the most (of `Column`, the functions, the DataFrame and its `na` and 
`stat`, the windows, the table arguments and UDTFs, the subqueries, the types, 
serde and observation) and `test_parity_pandas_transform_with_state.py` again, 
master f9157d72 and this change back to back: the same results file by file 
(1006 passed, 1 failed, 220 skipped). The failure is 
`test_input_file_name_udf`, of the same environment, and 
`test_parity_pandas_transform_with_state.py` timed out on both;
     - the drop-in live-server tests (the 31 files) on the first base: 1257 
passed. `test_dropin_e2e.py` again on the final base: 343 passed.
   - **pandas-on-Spark**, whose `pyspark_column_op` calls the classic 
`Column`'s methods: the official data type ops tests 
(`pyspark/pandas/tests/connect/data_type_ops`) through the drop-in, 476 passed 
on master f9157d72 and on this change.
   - **The pipelines tests of `pipelines-parity.yml`:** through the drop-in, 72 
passed on the first base and on this change, with the same failure of 
`test_register_definitions_unsupported_file_extension_matches_glob`, whose glob 
over `/tmp` met a directory that another process removed; the pure-Python ones, 
5 passed.
   - **The gates**, after the rebase onto master 682c3014:
     - `scripts/check_vendored_upstream.sh` against the `v4.3.0` tag from a 
local clone of apache/spark (183 paths, `column.py` and `table_arg.py` among 
them);
     - `check_classic_bindings.py`, `gen_type_stubs.py --check`, 
`gen_fn_dispatch.py --check` and `gen_connect_docstrings.py --check` against 
PySpark 4.3.0's source, `scripts/tests` (125 tests of `unittest discover`, 2 
skipped) and the guards;
     - `test_type_stubs_offline.py` with mypy 2.4.1 and pyright 1.1.414 (18 
passed);
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and 
`audit_no_stubs.sh`;
     - `cargo build -p pyspark-rs --release`, `cargo fmt --all --check`, clippy 
1.100.0 with `-D warnings`, rustdoc with `-D warnings`, and `cargo test -p 
pyspark-rs --lib` as `rust.yml` runs it (15 passed);
     - the offline suite of `coverage.yml`: 6808 passed and 77 skipped (68 for 
"not a public constructor", 8 that need `SPARK_REMOTE`, and the stub test, 
which needs mypy and ran above);
     - its Python coverage: 93.19%, above the floor of 93.0 (master 682c3014: 
93.26%). The lines of the new code that the suite does not run are PySpark's 
own, which no call on Spark Connect runs: the `...` bodies of the classic 
`Window`'s static methods, which `dispatch_window_method` does not call, the 
`return` of the classic `WindowSpec.__new__` after its import of 
`pyspark.sql.classic`, and the `@overload` declarations of 
`DataFrameNaFunctions` and `DataFrameStatFunctions`.
   
   ### 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