RamonZhou opened a new pull request, #529:
URL: https://github.com/apache/spark-connect-rust/pull/529
### What changes were proposed in this pull request?
A session can set environment variables for the Python workers that run its
UDFs with `spark.pythonWorkerEnv.<NAME>` (SPARK-58752, SPARK-59233). This
client sends conf over the standard config RPC, so the variables already reach
Python UDFs with no client change. They do not reach a Rust UDF: it runs as a
`wasm32-unknown-unknown` module that the runner instantiates with no imports,
so `std::env::var` always fails inside it.
This PR lets a Rust UDF opt in to the worker's environment:
- `spark_connect_build::embed_wasm_udf_for(src, WasmTarget::Wasip1)` builds
the module for `wasm32-wasip1`. `embed_wasm_udf` is unchanged and still builds
for `wasm32-unknown-unknown`.
- When a module imports WASI, the runner in the embedded packer instantiates
it with a snapshot of the worker's `os.environ`, taken when the module is
instantiated. That is the same environment a Python UDF sees, including Spark's
own worker variables. The module gets no files, arguments or standard streams.
A module that imports nothing is instantiated as before.
- `docs/udfs.md` documents `spark.pythonWorkerEnv.*` and the WASI target.
- CI installs `wasm32-wasip1` and runs the new `tests/wasm-udf-wasi` crate.
### Why are the changes needed?
So a Rust UDF can read the same session-scoped environment a Python UDF
reads, through plain `std::env::var`.
### Does this PR introduce _any_ user-facing change?
Yes, opt-in only. There is a new build helper, `embed_wasm_udf_for` with
`WasmTarget`. A WASI module now gets the worker's environment when it is
instantiated. Before this change, a WASI module could not be instantiated at
all, because the runner provided no imports.
### How was this patch tested?
- New `tests/wasm-udf-wasi` runner test, with no server. It checks that:
- a WASI module reads `os.environ` through both the per-row and the Arrow
batch runner;
- the same function built for `wasm32-unknown-unknown` still sees nothing;
- a new instance picks up a changed value;
- the module cannot list any directory.
- New e2e test on a live server, driven from the Rust client through
`conf.set`. It checks:
- a Python UDF and the WASI UDF (per-row and batch) see the value, and the
unknown-target UDF sees `None`;
- a change takes effect on the next action, and unset clears it;
- an invalid or reserved name fails the set with its error class;
- a silent `set_all` sets the valid key and warns about the bad one
without echoing its value;
- a name that looks like a secret cannot be read back but is still
delivered;
- a second session does not see the first session's variables.
The e2e test skips on a server older than 4.4.0. CI runs against 4.3.0, so
it only compiles there. I checked that it runs fully against a `5.0.0-SNAPSHOT`
server built with SPARK-58752 and SPARK-59233, and that it skips against a
4.1.2 server.
- Existing `tests/wasm-udf` runner and e2e tests, plus the
`apache-spark-connect` and `apache-spark-connect-build` unit tests: they pass
unchanged. rustfmt and clippy are clean.
- Separately, the PySpark Connect parity suite for SPARK-58752
(`test_parity_python_worker_env.py`, 32 tests) passes with this repo's
`pyspark` drop-in as the client. That confirms Python UDFs need no client
change.
TODO before review: file a `connect-rust-x.y.z` JIRA and retitle.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (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]