Contributing¶
Contributions are welcome. This is an Apache Software Foundation project, so a few conventions apply.
The repository holds the Rust client crates under crates/ and the Python drop-in:
the pyspark package under python/pyspark, around the pyspark._pyspark extension
that the crates/pyspark-rs crate builds. Architecture shows how
they fit together.
Issues and discussion¶
Bugs and feature requests are tracked in ASF JIRA under the
SPARK project - GitHub Issues are
disabled. Reference the JIRA key in your pull request title, e.g.
[SPARK-XXXXX][CONNECT] Short summary, with [PYTHON] after [CONNECT] for a change
to the Python drop-in.
Building and testing¶
Requires a Rust toolchain and protobuf-compiler. Non-Windows aarch64 builds
also need the target C tools described in the
source build requirements.
cargo build # build the library crates
cargo test # unit + golden-proto tests
cargo build -p examples # the Rust-native examples
Builder correctness is covered by golden-proto tests: plans, expressions,
all SQL functions, the reads, the catalog calls and ML transforms are asserted
byte-for-byte against captured reference protos. The goldens under tests/golden/
are captured from the official pyspark-client by the scripts/capture_*_golden.py
scripts, against a Spark Connect server: run each with that client's Python, isolated,
as python -I scripts/capture_golden.py --remote sc://localhost:15002.
Transport and Arrow paths are exercised by the end-to-end tests below and by the
official Apache Spark Connect test suite in CI - see the CI notes under
dev/design/.
The end-to-end tests (crates/spark-connect/tests/e2e_*.rs and a few others) run only
when SPARK_REMOTE names a Spark Connect server;
without it they pass without checking anything. They share one server session, so CI
runs them on one thread:
SPARK_REMOTE=sc://localhost:15002 \
cargo test -p apache-spark-connect --test e2e_dataframe -- --test-threads=1
The wasm-udf example crates need the wasm32 target and are built by manifest
path (they're excluded from the workspace):
rustup target add wasm32-unknown-unknown
cargo build --manifest-path examples/wasm-udf-inline/Cargo.toml
A local Spark Connect server¶
The end-to-end tests, the live Python tests and the official test suites run against a
Spark 4.3.0 Connect server. The Spark distribution bundles the server, so no
--packages is needed: unpack spark-4.3.0-bin-hadoop3-connect.tgz (the one CI uses)
or spark-4.3.0-bin-hadoop3.tgz from the
Apache CDN, which serves the current releases,
or the Apache archive, which keeps every
release but is slower. scripts/download_spark_dist.sh 4.3.0 spark-4.3.0-bin-hadoop3-connect DIR
downloads and unpacks it as CI does, from the CDN or else from the archive. Start it with
Java 17 and CI's settings:
export SPARK_HOME=/path/to/spark-4.3.0-bin-hadoop3-connect
(cd "$SPARK_HOME" && TZ=UTC SPARK_TESTING=1 PYSPARK_PYTHON="$(command -v python)" \
./sbin/start-connect-server.sh \
--driver-memory 3g --conf spark.sql.session.localRelationSizeLimit=1073741824)
It listens on sc://localhost:15002 and logs to $SPARK_HOME/logs; stop it with
"$SPARK_HOME"/sbin/stop-connect-server.sh. The settings:
TZ=UTCmakes UTC the session time zone, as on CI's runners. The test commands below set it too.SPARK_TESTING=1turns on Spark's test-mode defaults, which some tests rely on.PYSPARK_PYTHONis the Python that runs UDFs on the server. It needs pandas and pyarrow, and the same minor version as the Python that runs the tests, or their UDFs fail withPYTHON_VERSION_MISMATCH. Start the server with the virtual environment active.- Starting it from
$SPARK_HOMElets the official tests' relative data paths resolve.
Python drop-in¶
python/pyspark holds Rust-backed shims over pyspark._pyspark and code vendored from
PySpark (see below). Its tests assert the behavior of
official PySpark 4.3.0: where the two differ, PySpark is the reference (see
Compatibility).
Set up Python¶
The package supports Python 3.9 or later (requires-python in pyproject.toml); CI
runs Python 3.11. Create a virtual environment with the packages CI installs. pandas
stays below 3.0, which PySpark 4.3 does not fully support yet:
python3 -m venv .venv
source .venv/bin/activate
pip install pytest coverage 'pandas>=2.2,<3.0' pyarrow numpy grpcio grpcio-status \
googleapis-common-protos cloudpickle zstandard py4j ruff==0.14.0
Tests that need an optional package skip without it; for the official suites, CI also
installs 'plotly<6.0.0' and graphviz==0.20.3.
Build the extension¶
Build the extension with the Rust requirements above, and copy it into the package.
Repeat this after every change to the Rust code, including the Python sources that the
extension embeds (crates/pyspark-rs/src/row.py and values.py):
cargo build -p pyspark-rs --release
cp target/release/lib_pyspark.dylib python/pyspark/_pyspark.so # macOS
cp target/release/lib_pyspark.so python/pyspark/_pyspark.so # Linux
python/ is then a working pyspark package. CI puts it on PYTHONPATH rather than
installing it, which keeps it apart from any other pyspark in the environment:
To build a wheel instead, run maturin build --release --out dist, which reads
[tool.maturin] in pyproject.toml (see
Installation).
Run the tests¶
Run the tests from python/. The offline suites, tests/test_*_offline.py and
tests/test_arrow_handoff.py, need no server:
cd python
TZ=UTC PYTHONPATH=$(pwd) python -m pytest tests/test_*_offline.py tests/test_arrow_handoff.py
TZ=UTC PYTHONPATH=$(pwd) python -m pytest tests/test_dropin_offline.py -k pickle # a subset
The live suites, tests/test_*_e2e.py, run against the
local server and skip without SPARK_REMOTE:
CI runs the test files that its workflows name (coverage.yml for the offline suites,
build_python_connect.yml for the live ones), so add a new test to an existing file, or
add a new file to the workflow: a guard step of each fails when an offline file
(test_*_offline.py or test_arrow_handoff.py) or a live one (test_*_e2e.py) is in no list.
CI also measures the coverage of the hand-written shim within the scope of
python/.coveragerc, over the offline suites, and fails below the floor that coverage.yml
sets:
TZ=UTC PYTHONPATH=$(pwd) python -m coverage run --rcfile=.coveragerc -m pytest \
tests/test_*_offline.py tests/test_arrow_handoff.py
python -m coverage report --rcfile=.coveragerc
Run the official PySpark tests¶
On every pull request, CI runs Apache Spark's own Connect test suites twice: against the
drop-in's classes, and with official PySpark sending its requests through the Rust
transport. Run the files that cover your change with scripts/run_official_tests.py,
from the repository root. --spark is an Apache Spark tree with the tests under
python/: CI clones the v4.3.0 tag, and the unpacked distribution has the same test
files.
RUST_PYSPARK_SO=$(pwd)/python/pyspark/_pyspark.so TZ=UTC python scripts/run_official_tests.py \
--dropin --spark "$SPARK_HOME" --remote sc://localhost:15002 \
--only pyspark/sql/tests/connect/test_parity_column.py
Without --dropin, the file runs through the transport instead. Without --only, the
whole suite runs, which takes about an hour on CI (build_python_connect.yml has CI's
exact options). The known failures are listed in scripts/dropin_known_failures.txt and
scripts/parity_known_failures.txt. Any other failure fails CI, and the runner reports
listed tests that now pass, to be removed from the list.
Vendored and generated code¶
Many modules under python/pyspark are verbatim copies of PySpark 4.3.0, such as
pyspark.pandas, pyspark.cloudpickle and much of pyspark.ml and pyspark.errors, and so
is Spark's spatial reference system registry that the Rust crate embeds,
crates/spark-connect/src/srs_registry.csv. Do not edit them: they are listed in
scripts/check_vendored_upstream.sh (and the Python ones in the ruff excludes of
pyproject.toml), and CI fails when one that the script lists differs from the v4.3.0 tag
(the script fetches it from GitHub):
Fix a behavior in the Rust core (crates/spark-connect), in the bindings
(crates/pyspark-rs) or in a hand-written shim instead, and not by monkey-patching a
Rust class from Python.
python/pyspark/sql/connect/functions/_generated.py and
crates/pyspark-rs/src/dispatch_generated.rs are generated from
crates/spark-connect/src/functions.rs. Regenerate them after changing a function there;
CI runs the same command with --check:
Each optional argument of a PySpark function has a typed form there too:
scripts/tests/test_gen_fn_dispatch.py fails on an optional parameter of a function of
python/pyspark/sql/connect/functions/builtin.pyi that no Rust function of its name or of its
forms takes, unless OPTIONAL_ARGS_OTHERWISE of the generator gives the reason. A form of more
trailing Columns goes into OPTIONAL_ARG_VARIANTS, whose arm picks it by the number of arguments,
and another form into NOT_DISPATCHED and OPTIONAL_ARG_FORMS.
The type stubs (.pyi) of the modules whose classes are native or whose names are made at
import time, such as python/pyspark/sql/functions/*.pyi, python/pyspark/sql/dataframe.pyi and
python/pyspark/sql/connect/proto/__init__.pyi, are generated from PySpark 4.3.0's modules, so
that type checkers see PySpark's API, and those of the modules of pyspark.ml.connect of native
ML classes (ML_CONNECT_MODULES of the generator, and their package) from the drop-in's modules
and PySpark's classes of pyspark.ml that their public classes subclass. A stub re-exports a name
of another module only when the drop-in's module has it, so regenerate them after a change to the
generator, to the tracked version or to the names of a module of the drop-in; the generator also
writes their block of ruff excludes in pyproject.toml, and CI runs it with --check:
A module that comes to re-export a native class gets a stub when you add it to STUB_MODULES of
the generator, and a public name that the drop-in's module has and PySpark's does not goes into its
EXTRAS. A module that becomes PySpark's own, vendored as it is, leaves STUB_MODULES, as a stub
would only shadow PySpark's annotated source, and a name of EXTRAS that the drop-in's module no
longer has leaves EXTRAS. A star import of PySpark's module of a module that the drop-in does not
have goes into DROPPED_STAR_IMPORTS, with the reason. A method that assigns an instance attribute
keeps its body in the stub, from which type checkers infer the attribute; the generator fails on a
public instance attribute that a stub would miss, and one whose type PySpark narrows before it
assigns it goes into ATTRIBUTE_TYPES. python/tests/test_type_stubs_offline.py runs mypy, and
pyright where it is installed (as coverage.yml installs it), on a user's application and on
probes of the stubbed modules, and fails:
- on a public name of a stubbed module, or a public member of a class of a stub, that the drop-in
has at run time and the stub misses (a member that only PySpark Connect's class of a classic
import path has goes into UNDECLARED_MEMBERS of the test, with the reason), or an instance
attribute that mypy does not type;
- on a name that a stub declares and the drop-in does not have at run time: give the drop-in the
name if it is PySpark's API, list it in DROPPED of the generator if it is PySpark's plumbing
that no annotation needs, or in TYPE_ONLY of the test, with the reason, if PySpark's
annotations need it;
- on a name of DROPPED that the drop-in has at run time, which the stub would miss;
- on a docstring of a stub that is not that of PySpark's object at run time, of
tests/golden/stub_docstrings.json, which scripts/capture_stub_docstrings_golden.py takes
with pyspark-client 4.3.0: recapture it when the declarations of the stubs change.
The stubs keep the docstring of each of PySpark's objects as it has it at run time, of its
decorators too: one that the generator does not know is an error, which DOC_KEEPING_DECORATORS
takes when the decorator keeps the docstring.
The classic classes whose Connect classes are native (pyspark.sql.Column and the others of
_BINDINGS of python/pyspark/sql/_classic_bases.py) forward a patch to the native class unless
PySpark's Connect class has the name itself or its __init__ sets it on each object, of the names
that _BINDINGS keeps of PySpark's classes. CI checks those names against the source of PySpark at
the tracked tag, of which the script works them out, and prints the difference and the names to
keep when they differ, as after a change of the tracked version:
For local collection measurements and their interpretation, see Row conversion throughput and responsiveness.
Style¶
cargo fmt --all # format (CI runs it with --check)
cargo clippy --workspace --all-targets -- -D warnings # lint (blocking in CI)
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect \
-p apache-spark-connect-core -p apache-spark-connect-macros -p apache-spark-connect-build \
--features apache-spark-connect/wasm-udf # API docs (blocking in CI)
Rust CI builds with RUSTFLAGS="-D warnings", so a compiler warning fails it, and so
does any clippy finding. Clippy over the workspace needs the wasm32-unknown-unknown
target (rustup target add wasm32-unknown-unknown), because it builds
examples/wasm-udfs. CI runs rustfmt and clippy with a pinned toolchain, Rust 1.100.0
(.github/workflows/lint.yml), so that their verdicts do not change with a new release:
run cargo +1.100.0 clippy ... to match it. CI also lints the datafusion and polars
features of apache-spark-connect, and the crates that the workspace excludes
(examples/wasm-udfs, examples/wasm-udf-inline, tests/wasm-udf and tests/wasm-udf-wasi,
which needs the wasm32-wasip1 target too) through their manifests, and builds the API docs of
the published crates but the generated apache-spark-connect-proto with rustdoc's warnings as
errors, such as an intra-doc link that does not resolve or that names a private item. The lints
this codebase triggers by design are allowed once, with a reason, in [workspace.lints.clippy] of the root Cargo.toml; a targeted #[allow]
needs a comment saying why. CI also fails on deferral
markers such as TODO, FIXME, todo!() or "for now" in crates/*/src
(bash scripts/audit_no_stubs.sh). Match the surrounding code, keep changes focused,
and add or update golden tests when you change plan or expression building.
Workspace builds, tests and Clippy include the experimental JNI crate. It needs
macOS or Linux, a C compiler and JAVA_HOME set to a JDK with JNI headers.
JDK 21 is the validated adapter and CI baseline.
The frontend-only cargo test --locked -p spark-connect-server uses injected
engines without a JDK. JNI, packaging and live Spark checks are documented in the
server guide.
On Windows, or to build/test/lint the other crates without a JDK, exclude it:
cargo build --workspace --exclude spark-connect-jni
cargo test --workspace --exclude spark-connect-jni
cargo clippy --workspace --all-targets --exclude spark-connect-jni -- -D warnings
Python is checked with ruff 0.14.0, the version CI pins, within the scope and excludes
of pyproject.toml. Run it from the repository root without paths, as CI does:
ruff check
ruff format --check
python python/tests/test_ruff_excludes.py # the excludes match the vendored list
ruff format covers scripts/ and the Row and value sources that the extension
embeds: the shim and the tests are linted but not reformatted, so that open pull
requests don't conflict. When you pass paths, for example from an editor, add
--force-exclude: without it, ruff checks and formats every path it is given,
vendored ones included.
Pull requests¶
Native JAR license assets record whole-file hashes for the inputs in
crates/spark-connect-server/jvm/native-licenses/dependencies.json. Run
python3 scripts/gen_native_binary_licenses.py --check before pushing. If it reports
stale assets, run python3 scripts/gen_native_binary_licenses.py and commit the
regenerated native-license assets, even if the JNI dependency closure is unchanged.
CI and native packaging reject stale assets.
Open PRs against
apache/spark-connect-rust.
Keep the PR description tied to its JIRA. Before you push, make sure cargo test and
cargo fmt --all --check pass and, for a change to the drop-in, the offline Python
tests and ruff; CI runs these as well as the end-to-end and official suites, coverage
and the vendored-code check.