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

   ### What changes were proposed in this pull request?
   
   A follow-up of #881 (SPARK-62071) and #882 (SPARK-62074), which made the 
classic modules of the objects of a session and `pyspark.sql.utils` PySpark's. 
`pyspark.sql.functions` was still the drop-in's module `functions.py`, of its 
own functions of the Rust core, with the stubs of PySpark's package next to it 
in `pyspark/sql/functions/`, which Python did not import. 
`docs/compatibility.md` kept it a module on purpose, as a package would change 
its run-time facts. This makes it PySpark's package, so that those facts are 
pyspark-client's.
   
   There were two ways: PySpark's three files as they are, or the drop-in's 
functions of the Rust core in the modules of a package. Only the first gives 
pyspark-client's `__wrapped__`, docstrings, signatures and code of the 
functions, what a call runs after a patch of `pyspark.sql.connect.functions`, 
and `PYSPARK_NO_NAMESPACE_SHARE`: an implementation that gives them runs 
PySpark's `try_remote_functions` wrapper over PySpark's function, which is what 
`builtin.py` is. Its cost is the wrapper's at each call, which pyspark-client 
pays too (see the timings below).
   
   Each row was probed on both clients, without a server (`F` is 
`pyspark.sql.functions`, `CF` and `CB` are `pyspark.sql.connect.functions` and 
its `builtin`, `CP` its `partitioning`):
   
   | | pyspark-client 4.3.0 | drop-in before | drop-in after |
   |---|---|---|---|
   | `F.__file__`, `F.__path__` | `functions/__init__.py`, `[functions]` | 
`functions.py`, `AttributeError` | as pyspark-client |
   | `pkgutil.iter_modules(pyspark.sql.__path__)` of the functions | 
`functions`, a package | `functions` and `functions_generated`, modules | as 
pyspark-client |
   | `pkgutil.iter_modules(F.__path__)` | `builtin`, `partitioning` | 
`AttributeError` (no `__path__`) | as pyspark-client |
   | `F.builtin` and `F.partitioning` | modules of `builtin.py` and 
`partitioning.py` | `ModuleType` objects of no `__file__` or `__spec__` | as 
pyspark-client |
   | `importlib.util.find_spec("pyspark.sql.functions.builtin")`, 
`importlib.reload(F.builtin)` | the spec of `builtin.py`, a module | 
`ValueError: ...__spec__ is None`, `AttributeError` | as pyspark-client |
   | `F.col.__module__`, `F.col is F.builtin.col` | 
`pyspark.sql.functions.builtin`, True | the same | the same |
   | `F.col.__wrapped__` | `builtin.col` of PySpark | none | as pyspark-client |
   | `F.col.__code__` | `wrapped` of `pyspark/sql/utils.py` 
(`try_remote_functions`) | `col` of `functions.py` | as pyspark-client |
   | `F.col.__doc__` | PySpark's, 454 characters | "Returns a Column based on 
the given column name.", 48 | as pyspark-client |
   | `inspect.signature(F.col)` | `(col: str) -> pyspark.sql.column.Column` | 
`(col)` | `(col: str) -> pyspark.sql.connect.column.Column` (1) |
   | `inspect.getsource(F.col)` | PySpark's `col`, of 29 lines | the drop-in's 
| as pyspark-client |
   | `pickle.dumps(F.col)`, `cloudpickle.dumps(F.col)` | by reference to 
`pyspark.sql.functions.builtin` | the same | the same |
   | `F.col("a")` after `CF.col = f` | `f("a")` | `Column<'a'>` | `f("a")` |
   | `F.col("a")` after `F.builtin.col = f`, or `CB.col = f` | `Column<'a'>` | 
the same | the same |
   | `F.array_contains("a", 1)` after `CB.lit = f`, of a non-Column | `f` is 
called (`AssertionError`) | `f` is not called | `f` is called 
(`PySparkTypeError`) |
   | `F.partitioning.bucket(2, "a")` after `CP.bucket = f` | `f(2, "a")` | 
`Column<'bucket(2, a)'>` | `f(2, "a")` |
   | `F.col("a")` with `PYSPARK_NO_NAMESPACE_SHARE` | `ImportError: cannot 
import name 'SparkContext' from 'pyspark'` | `Column<'a'>` | as pyspark-client |
   | `F.sum("a")`, `F.partitioning.bucket(2, "a")` with 
`PYSPARK_NO_NAMESPACE_SHARE` | `ModuleNotFoundError: No module named 
'pyspark.sql.classic'` | Columns | as pyspark-client |
   | `F.udf(f)` with `PYSPARK_NO_NAMESPACE_SHARE` | a function (PySpark's 
classic UDF, which needs a SparkContext when called) | `ImportError` of 
`SparkContext` | as pyspark-client |
   | `F.partitioning.bucket`: `__module__`, `__wrapped__`, `__doc__` | 
`pyspark.sql.functions.partitioning`, PySpark's `bucket`, PySpark's (813) | 
`pyspark.sql.functions.partitioning`, none, none | as pyspark-client |
   | `F.udf.__doc__`, `F.udtf.__doc__`, `F.arrow_udtf.__doc__` | PySpark's: 
6127, 5251, 2038 characters | the drop-in's: 793, 360, 233 | as pyspark-client |
   | `len(dir(F))`, `len(dir(F.builtin))`, `len(dir(F.partitioning))` | 614, 
631, 23 | 654, 604, 11 | 614, 631, 23, the same lists |
   | `F.partitioning.__all__` | none | `["years", "months", "days", "hours", 
"bucket"]` | none |
   | `CF.col.__doc__`, `inspect.signature(CF.array_join) == 
inspect.signature(F.array_join)` | PySpark's (454), True | the drop-in's (48), 
True | PySpark's (454), True |
   | `CB.array_contains.__globals__` | of `CB` | of the module 
`functions_generated` | of `CB` |
   | `CP.bucket.__doc__`, `__file__` of `CP` | PySpark's, 
`connect/functions/partitioning.py` | none, `connect/functions/partitioning.py` 
(a copy of the drop-in's) | as pyspark-client (PySpark Connect's module) |
   | mypy on `pyspark/sql/functions.py pyspark/sql/functions` (the source tree 
by path) | (no `functions.py`) | `Duplicate module named 
"pyspark.sql.functions"`, nothing checked | checks the 3 files of the package |
   | pyright on `from pyspark.sql.functions import partitioning` and `import 
pyspark.sql.functions.builtin` | no warning | `reportMissingModuleSource` | as 
pyspark-client |
   | pyright on `F.partitioning.bucket` | `reportPrivateImportUsage` (PySpark's 
`__all__` does not list it) | no error (the stub) | as pyspark-client |
   | `import pyspark; import pyspark.sql.functions`: modules of 
`pyspark.sql.connect` | none | none | none |
   
   (1) The annotations name the class of `pyspark.sql.column.Column`, which is 
the Connect class in the drop-in (see the first difference that remains).
   
   The names of every module agree too: `dir()` of `F`, `F.builtin`, 
`F.partitioning` and `CP`, and `F.__all__`, are pyspark-client's lists. Of the 
`__doc__` of the 621 names of `F` and `F.partitioning`, 612 are 
pyspark-client's (before: 32 of 660), among them those of `udf`, `udtf` and 
`arrow_udtf`, which were the drop-in's own ("Create a user-defined function 
(UDF).", 793 characters, where PySpark's is 6127). The other 9 are classes of 
other modules, which PySpark's `builtin.py` imports: `Column` (of `F` and of 
`F.partitioning`), the native class of `pyspark.sql.column`, and `ArrayType`, 
`ByteType`, `DataType`, `MapType`, `NumericType`, `StringType` and 
`StructType`, the native classes of `pyspark.sql.types`, whose docstrings are 
empty or the drop-in's (see what remains different, below).
   
   The cost of a call, in microseconds, the best of 7 repeats of `timeit` (the 
same machine, one after the other; the drop-in before is #882's tree, whose 
functions master has):
   
   | call | pyspark-client 4.3.0 | drop-in before | drop-in after |
   |---|---|---|---|
   | `F.col("a")` | 5.5 | 0.6 | 3.2 |
   | `F.lit(1)` | 6.8 | 0.5 | 3.1 |
   | `F.expr("a + 1")` | 4.3 | 0.4 | 3.0 |
   | `F.sum("a")` | 8.9 | 1.6 | 4.3 |
   | `F.array_contains("a", 1)` | 14.0 | 1.7 | 4.5 |
   | `F.when(c > 1, 1)` | 14.3 | 2.3 | 5.0 |
   | `F.concat_ws("-", "a", "b")` | 24.6 | 2.7 | 5.4 |
   | `F.date_format("a", "yyyy")` | 13.6 | 1.7 | 4.4 |
   | `F.transform("a", lambda x: x + 1)` | 37.6 | 15.8 | 19.1 |
   | `F.partitioning.bucket(2, "a")` | 11.8 | 2.0 | 4.7 |
   | `F.count("*")` | 8.9 | 0.7 | 3.4 |
   
   A call runs PySpark's wrapper first, as in pyspark-client: `is_remote()`, 
which reads `os.environ` twice, a third read of `os.environ` 
(`PYSPARK_NO_NAMESPACE_SHARE`), and `from pyspark.sql.connect import 
functions`. That is about 3 microseconds here, which most functions of the Rust 
core take less than. The import of `pyspark.sql.functions` and the first call:
   
   | milliseconds, the least of 9 runs (`-X importtime`) | pyspark-client 4.3.0 
| drop-in before | drop-in after |
   |---|---|---|---|
   | `import pyspark.sql.functions`, of a fresh interpreter | 348.6 | 310.8 | 
316.7 |
   | of which the modules of `pyspark.sql.functions` | 9.0 | 7.3 | 10.3 |
   | the first `F.col("a")`, which imports `pyspark.sql.connect.functions` 
(PySpark Connect, in pyspark-client) | 781.5 | 0.7 | 4.6 |
   
   How:
   - **The package.** 
`python/pyspark/sql/functions/{__init__,builtin,partitioning}.py` are vendored 
from v4.3.0 as they are (`VENDORED_DIRS` of the drift gate: the directory is 
PySpark's), under the ruff excludes and the coverage omits, and replace 
`functions.py` and the stubs of the directory.
   - **The functions of Spark Connect.** The drop-in's `functions.py` moves to 
`pyspark/sql/connect/functions/builtin.py`, the module of the functions that 
those of the package run, as PySpark Connect's. To review the move: `git diff 
883afd07:python/pyspark/sql/functions.py 
HEAD:python/pyspark/sql/connect/functions/builtin.py` (114 lines added, 771 
removed: the `__all__` of 562 names, the modules that it made, and the reading 
of `udf`, `udtf` and `arrow_udtf` on first use of #882, which PySpark's package 
does not need, as it imports no module of `pyspark.sql.connect`). As PySpark 
Connect's module:
     - it has no `__all__`, so `pyspark.sql.connect.functions` star-imports all 
its names, the deprecated functions too, and no `builtin` and `partitioning` 
modules of its own (`sys.modules`);
     - `functions_generated.py` moves to 
`pyspark/sql/connect/functions/_generated.py`, and the module runs its code in 
its own namespace (of the cached bytecode of the file), so that the generated 
functions are of the module and call its `lit`, `column`, `_to_col` and 
`_invoke_function` as it has them at the call, as PySpark Connect's do;
     - `_to_col` is PySpark Connect's, and the helper of `_dispatch` that 
passes a value that is not a str as it is becomes `_name_to_col`;
     - the functions get the docstrings of PySpark's (`col.__doc__ = 
pysparkfuncs.col.__doc__` of PySpark Connect), but `date_part`, `datepart` and 
`uuid`, which PySpark Connect leaves without one, and the annotations of 
PySpark's, when they take the same parameters, which all 562 do: so 
`inspect.signature(CF.array_join) == inspect.signature(F.array_join)`, as in 
pyspark-client;
     - `udf`, `udtf` and `arrow_udtf`, of `pyspark.sql.connect.udf` and `udtf`, 
are of the module, where the drop-in made them of 
`pyspark.sql.functions.builtin`;
     - the names that it had of copying the classic module and PySpark 
Connect's does not have leave it: `UserDefinedFunction`, 
`UserDefinedTableFunction`, `ArrowUDFType`, `PandasUDFType`, `OrderingColumn`, 
`PartitioningColumn`, `SelectedColumn`, `SkipRestOfInputTableException`, 
`ByteType`, `NumericType` and `Iterable`.
   
     `pyspark/sql/connect/functions/partitioning.py` is PySpark Connect's, 
vendored as it is, of the module above; the package's `__init__.py` is 
unchanged.
   - **The callers.** The Avro and Protobuf functions take their helpers of 
that module; `DataFrame.foreach` builds its query of its functions, as PySpark 
Connect's `DataFrame` does; and the Rust `lit` of a list or a NumPy array 
(`functions.rs`) calls its `lit`, as PySpark Connect's functions call theirs. 
`pyspark/pipelines/api.py`, whose only adaptation was to import `expr` and 
`col` of `pyspark.sql.functions` for `pyspark.sql.connect.functions.builtin`, 
is vendored as it is.
   - **The type checkers.** `scripts/gen_type_stubs.py` no longer stubs 
`pyspark.sql.functions`, its `builtin` and `partitioning`, nor 
`pyspark.sql.connect.functions.partitioning`, PySpark's annotated source, which 
type checkers read as with pyspark-client. `EXECUTED_SOURCES` gives the 
generator the names that `_generated.py` binds as names of 
`pyspark.sql.connect.functions.builtin`, so its stub keeps PySpark Connect's 
`_to_col`, `_invoke_function_over_columns` and the other helpers that the 
module has; the names above leave its `EXTRAS`; `LambdaFunction` joins 
`UNRESOLVED_IMPORTS`, as the drop-in builds lambdas in Rust. The docstring 
golden is captured again with pyspark-client (the stubs of the vendored modules 
leave it).
   - **The dispatch generator.** `scripts/gen_fn_dispatch.py` writes 
`pyspark/sql/connect/functions/_generated.py`, whose header says how the module 
runs it.
   
   What remains different, documented in `docs/compatibility.md` and 
`dev/parity/known-differences.md`:
   - The annotations, of `F` as of `CF`, name the class of 
`pyspark.sql.column.Column`, which is PySpark Connect's class in the drop-in 
(`pyspark.sql.connect.column.Column`), where pyspark-client's name its classic 
class; the classic `Column` is the follow-up of the classic Column, 
DataFrameNaFunctions, DataFrameStatFunctions, WindowSpec and TableArg classes, 
which also gives `F.Column` PySpark's docstring.
   - The docstrings of the data types that `F` names (`ArrayType`, 
`StringType`, ...): the native classes of `pyspark.sql.types` have empty 
docstrings, or the drop-in's (`DataType`).
   - Of the functions of Spark Connect, which are the drop-in's: a traceback of 
a generated one has the frames of `_generated.py`, where PySpark Connect's are 
of `builtin.py`; `CF.col` has no `__wrapped__`, as PySpark Connect's 
`_with_origin` does not wrap it (the Rust core captures the call site); `CB` 
has none of the expression classes of PySpark Connect that its module imports 
(`CaseWhen`, `ColumnReference`, ...), nor `np`, and its private helpers are its 
own; a patch of `CB.lit` that returns no Column fails a function that calls it 
with the drop-in's `PySparkTypeError` (`UNSUPPORTED_DATA_TYPE`), where PySpark 
Connect's assertion fails; and `schema_of_csv`, `schema_of_json` and 
`schema_of_xml` are annotated `Union[Column, str]` as PySpark's classic 
functions, where PySpark Connect writes `Union[str, Column]`.
   
   ### Why are the changes needed?
   
   So that `pyspark.sql.functions` is pyspark-client's: its modules, what 
`pkgutil` and `importlib` find of them, the docstrings, signatures and pickles 
of its functions, what they run when `pyspark.sql.connect.functions` is 
patched, as tests and libraries do, and `PYSPARK_NO_NAMESPACE_SHARE`. Type 
checkers read PySpark's source, as with pyspark-client: mypy run on the source 
tree by path reported `Duplicate module named "pyspark.sql.functions"` (of 
`functions.py` and `functions/__init__.pyi`) and checked nothing, and pyright 
warned that `from pyspark.sql.functions import partitioning` "could not be 
resolved from source".
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as listed above. The functions build the same Columns. A function of 
`pyspark.sql.functions` now runs the function of 
`pyspark.sql.connect.functions` of its name at each call, so a patch of that 
module changes what it runs, and `PYSPARK_NO_NAMESPACE_SHARE` makes it run 
PySpark's classic body, which fails, as in pyspark-client. Each call costs 
PySpark's wrapper. Code that read the private names of the drop-in's 
`pyspark.sql.functions` (`_FN_COL_ARGS`, `_dispatch`, `_invoke_function`, ...) 
finds them in `pyspark.sql.connect.functions.builtin`. pyright reports 
`F.partitioning` as a private import (`reportPrivateImportUsage`), as with 
pyspark-client, where the drop-in's stub declared it.
   
   ### How was this patch tested?
   
   - **New tests that fail on master (883afd07):**
     - `python/tests/test_dropin_functions_package_offline.py`, 11 tests, of 
which 10 fail on master: the package and its modules (`__file__`, `__path__`, 
`pkgutil`, `find_spec`, `__doc__`, no `__all__`); their reload, in a process of 
its own; every function of `__all__` is PySpark's of `builtin`, the 
`try_remote_functions` wrapper of PySpark's function, of its docstring and 
signature, with a function of its name in `pyspark.sql.connect.functions`; the 
docstrings of `udf`, `udtf` and `arrow_udtf`; a call runs the function of Spark 
Connect of its name at the call, after a patch of `CF`, `F`, `builtin`, `CB` 
and `CP`; the generated functions call the `lit` of their module; 
`PYSPARK_NO_NAMESPACE_SHARE`; the names of the package; the docstrings and the 
annotations of the 562 functions of `CB`; and `CP` as PySpark Connect's. The 
eleventh, that the functions pickle by reference to PySpark's modules, passes 
on master too.
     - In `test_type_stubs_offline.py`, 3 tests: the directory 
`pyspark/sql/functions/` is PySpark's package of no stub, which Python imports; 
mypy on the paths of `pyspark/sql/functions*` reports no duplicate module (on 
master: `Duplicate module named "pyspark.sql.functions"`); pyright resolves the 
modules of the functions from source, with the one error of `F.partitioning` 
that it gives with pyspark-client.
     - In `scripts/tests/test_gen_type_stubs.py`, a test of `EXECUTED_SOURCES`.
     - In `test_dropin_e2e.py`, 
`test_the_functions_run_those_of_spark_connect_of_their_names_as_pysparks`: a 
query of `F.upper` after a patch of `CF.upper` selects the patched function, 
and `PYSPARK_NO_NAMESPACE_SHARE` makes `F.col` raise PySpark's `ImportError`. 
It fails on master, and the same body passes with pyspark-client 4.3.0.
   - **Updated tests.** 4 tests of `test_dropin_offline.py` read the private 
names of the drop-in's module (`_FN_COL_ARGS`, `_create_wrapper`, `_UNSET`, 
`_to_col`), which are those of `pyspark.sql.connect.functions.builtin` now, or 
the `__all__` of its `partitioning` module, which PySpark's has not, and that a 
reload of the package reloaded its `builtin` module, which PySpark's does not. 
In `test_type_stubs_offline.py`, the test that Python imported `functions.py` 
and not the stubs becomes the first test above, and the count of the names of 
the stubbed modules, of which `pyspark.sql.functions` is no longer one, is 
above 1500 (1838), not 2000. #882's tests of `pyspark.sql.connect` and of 
`udf`, `udtf` and `arrow_udtf` pass as they are.
   - **The official suites of `build_python_connect.yml`**, each of master and 
this branch back to back on the same Spark 4.3.0 server:
     - the drop-in suite (`run_official_tests.py --dropin`, 118 files), of 
master f9157d72 and of this branch on it: the same results file by file and 
test by test. 97 files pass, the manifest skips 14, and 7 have the same 43 
failures (2720 passed, 43 failed, 496 skipped in both), 
`test_parity_functions.py` among them, of `test_input_file_name_udf`. The 
failures are those of this environment, as on #881 and #882: the server runs 
outside a Spark source tree, so the test data that the tests read by relative 
paths (`python/test_support/...`) does not exist there (`PATH_NOT_FOUND`), 
which is what the streaming files fail on too;
     - after master moved to 883afd07 (docstrings of the native classes, 
SPARK-62078 and SPARK-62080, and a retry jitter), the 23 files of the drop-in 
suite that exercise the functions most (`--only`: the functions, columns, 
DataFrames, their query context, UDFs, UDTFs, grouping, subqueries, TVFs, 
readers and writers, types, statistics and errors), of master and of this 
branch on it: the same, 1004 passed, 2 failed (`input_file_name`, as above) and 
307 skipped in both;
     - the drop-in live-server tests (the 31 files, `test_dropin_e2e.py` among 
them) and the drop-in step of `pipelines-parity.yml` (the 6 files of the CLI 
and of `test_spark_connect.py` and `test_auto_cdc_flow.py`, which run the 
vendored `pyspark/pipelines/api.py`), on f9157d72: all pass, 1258 and 1259 
tests, the one more being the new test of `test_dropin_e2e.py`, and 73 tests of 
the pipelines on both; and `test_dropin_e2e.py` on 883afd07: 338 and 339 passed.
   - **The gates**, run again after the rebase onto master baca16f0 
(SPARK-62081, which rebased without conflicts), with `test_dropin_e2e.py` 
against a Spark 4.3.0 server (340 passed):
     - `scripts/check_vendored_upstream.sh` against the `v4.3.0` tag from a 
local clone of apache/spark (181 paths: `sql/functions`, 
`sql/connect/functions/partitioning.py` and `pipelines/api.py` are new);
     - `gen_type_stubs.py --check`, `gen_fn_dispatch.py --check`, 
`gen_connect_docstrings.py --check`, `scripts/tests` (111 passed, 2 skipped) 
and the guards of SPARK-61851;
     - `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`: 6797 passed and 77 skipped on 
baca16f0 (6769 on 883afd07, whose master had 6758);
     - its Python coverage, with its `.coveragerc`: 93.33%, above the floor of 
93.0 (master 883afd07: 93.44%). `functions.py` (268 statements, 98.8%) leaves 
the measured files for PySpark's vendored package, and 
`pyspark.sql.connect.functions.builtin`, which was the 10 statements of its 
copies, is the drop-in's code (222 statements, 97.4%): what it does not run is 
what `functions.py` did not either (the `ImportError` of an extension without 
`_pyspark.functions`, and `_unwrap`), and two branches of the loop that gives 
the functions PySpark's docstrings and annotations, of a function of no classic 
function of its name and of one of other parameters, which none of the 562 is. 
`pyspark.sql.connect.functions.partitioning` is PySpark Connect's, omitted as 
vendored, and `_copies.py`, which only the Avro and Protobuf functions of Spark 
Connect use now, no longer takes its branch of a second name of a function 
(`random = rand`).
   - **The cost of the calls and of the imports**: the tables above, of 
`timeit` (the best of 7 repeats) and `python -X importtime` (the least of 9 
runs), of the three clients one after the other on the same machine.
   
   ### 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