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

   ### What changes were proposed in this pull request?
   
   The artifact methods of `SparkSession` share the binding of the drop-in with 
those of `SparkSession.client` (#779): `addArtifacts`, `addArtifact` and 
`copyFromLocalToFs`, and `add_artifacts` and `copy_from_local_to_fs`. They took 
their arguments differently from PySpark's in three ways.
   
   **1. Each path in turn.** PySpark's `SparkConnectClient.add_artifacts`, 
which `SparkSession.addArtifacts` calls, gives its `ArtifactManager` one path 
at a time (`core.py:2461-2470`). For each path:
   1. the path is parsed and its file opened;
   2. it is uploaded, in an `AddArtifacts` of its own;
   3. only then is the next path parsed.
   
   So when a later path fails, of a missing file or of a format that the flags 
do not take, the paths before it are uploaded, and the server keeps them. The 
drop-in's binding parsed and checked every path before it uploaded any, so it 
uploaded none of them. Its doc comment said this was PySpark's behavior, which 
it is only of `ArtifactManager.add_artifacts` given several paths, which 
PySpark's client never does. `upload_artifacts` 
(`crates/pyspark-rs/src/session.rs`) now parses, checks and uploads each path 
in turn, as the core's `add_artifacts_of_flags` already did.
   
   **2. Flags of any truth.**
   - PySpark's `addArtifacts` checks its flags first with `sum([file, pyfile, 
archive]) > 1` (`connect/session.py:1113`), as Python adds and compares them.
   - PySpark's client does not check them.
   - Both then test their truth (`if pyfile and ...`, `artifact.py:209-241`).
   
   The drop-in took them as PyO3 bools. Now:
   - `addArtifacts` and `addArtifact` take them as given, of `False` when 
omitted, and check them with Python's `sum` and comparison 
(`session_artifact_flags`).
   - The client's `_add_artifacts_of_flags` takes them as given too.
   - `upload_artifacts` tests their truth.
   - The signatures still show PySpark's defaults, `(*path, pyfile=False, 
archive=False, file=False)`.
   
   **3. A `Path`-like local path.** PySpark reads the local path of 
`copy_from_local_to_fs`, which `copyFromLocalToFs` calls, with 
`Path(local_path).absolute()` (`artifact.py:257`), and uploads the file of 
`str()` of it. The drop-in took a str only.
   - The binding's `copyFromLocalToFs` now takes the local path as given, 
checks it as `Path(local_path).absolute()`, and uploads the file of that 
absolute path.
   - `spark.client.copy_from_local_to_fs`, which calls it, takes a `Path` too.
   - The destination is still a str. A `Path` destination is a `TypeError`, as 
PySpark's client raises one.
   
   Checked on both clients against a Spark 4.3.0 server (`p` is an existing 
file, `/tmp/n.txt` a text file):
   
   | Call | pyspark-client 4.3.0 | drop-in before |
   |---|---|---|
   | `spark.addArtifacts(first.py, missing.py, after.py, pyfile=True)` | 
`FileNotFoundError`; `first` uploaded, `after` not | `FileNotFoundError`; 
nothing uploaded |
   | `spark.client.add_artifacts(second.py, note.txt, pyfile=True, 
archive=False, file=False)` | `UNSUPPORTED_OPERATION`; `second` uploaded | 
`UNSUPPORTED_OPERATION`; nothing uploaded |
   | `spark.addArtifacts(p, file=1)`, `spark.addArtifact(p, archive=0, 
file=1)`, `spark.addArtifacts(p, file=0.5)` | OK | `TypeError: 'int' object is 
not an instance of 'bool'` |
   | `spark.client.add_artifacts(p, pyfile=0, archive=0, file=1)` | OK | the 
same `TypeError` |
   | `spark.client.add_artifacts(p, pyfile=None, archive="", file=[1])` | OK | 
`TypeError` |
   | `spark.addArtifacts(p, pyfile=1, file=1)`, `(p, pyfile=2)`, `(p, 
pyfile=1.5)` | `INVALID_MULTIPLE_ARGUMENT_CONDITIONS` | `TypeError` |
   | `spark.addArtifacts(p, pyfile="yes")`, `(p, pyfile=None)`, `(p, 
file=[1])`, `(pyfile="yes")` | `TypeError: unsupported operand type(s) for +: 
'int' and 'str'` (`'NoneType'`, `'list'`) | `TypeError: ... is not an instance 
of 'bool'` |
   | `spark.copyFromLocalToFs(Path(p), "/tmp/d")`, and through `spark.client` | 
sent; the server refuses a local destination by default | `TypeError: 
'PosixPath' object is not an instance of 'str'` |
   | `spark.copyFromLocalToFs(5, ...)`, of bytes, of a missing `Path` | the 
`TypeError` of `Path`; `AssertionError` | `TypeError: ... is not an instance of 
'str'` |
   
   After this PR, the drop-in gives PySpark's results for all of these. Two 
differences that were there already remain:
   - the server's refusal of a local destination is a converted 
`UnsupportedOperationException`, where PySpark leaves grpc's 
`_InactiveRpcError` (`dev/parity/known-differences.md`, "Errors that PySpark's 
client leaves to grpc");
   - a `Path` destination of `spark.copyFromLocalToFs` is a `TypeError`, where 
PySpark's `urlparse` raises `AttributeError`.
   
   The docs do not describe these arguments beyond the PySpark behavior they 
now have.
   
   ### Why are the changes needed?
   
   So that the artifacts of a session are added as PySpark adds them: with 
PySpark's errors, of the arguments that PySpark takes, and with the uploads 
that PySpark's makes before an error.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - When a path of `spark.addArtifacts`, `addArtifact` or 
`spark.client.add_artifacts` fails, the paths before it are uploaded. A call 
whose paths are all valid sends the same requests as before.
   - Their flags take any object, of its truth, with PySpark's errors.
   - `spark.copyFromLocalToFs` and `spark.client.copy_from_local_to_fs` take a 
`Path`-like local path.
   
   ### How was this patch tested?
   
   - **Probes of both clients against a Spark 4.3.0 server:** the table above 
(`p_art_order.py`, `p_flags_paths.py`). The 14 flag cases and the local-path 
cases give identical results, apart from the two differences above.
   - **New tests in `test_arrow_handoff.py`**, against the recording artifact 
service of that file:
     - `test_artifact_upload_sends_each_path_before_it_parses_the_next`: the 
artifacts and uploads after a missing path, and after a path whose format the 
flags do not take, for both `spark.addArtifacts` and 
`spark.client.add_artifacts`; a path that fails first uploads nothing.
     - `test_artifact_flags_are_any_objects_as_pyspark`:
       - flags of 1, 0.5, `None`, `""` and `[1]`;
       - the client taking the first flag that matches;
       - `INVALID_MULTIPLE_ARGUMENT_CONDITIONS` of `pyfile=1, file=1`, 
`pyfile=2` and `archive=1.5`;
       - `sum`'s `TypeError` of a str, `None` and a list, also without a path;
       - the signatures;
       - that nothing is sent on an error.
     - `test_copy_from_local_to_fs_takes_a_path_like_local_path_as_pyspark`:
       - a `Path`, also a relative one, for both the session and the client;
       - the `TypeError` of `Path` for an int and for bytes;
       - the `AssertionError` of a missing `Path`;
       - the `TypeError` of a `Path` destination.
   - **New tests in `test_dropin_e2e.py`**, whose bodies also pass on 
pyspark-client 4.3.0:
     - `test_add_artifacts_upload_the_paths_before_one_that_fails_as_pyspark`: 
a UDF imports the modules uploaded before the failing path, and not the one 
after it;
     - `test_add_artifacts_take_flags_of_any_truth_as_pyspark`: a module added 
with `pyfile=1` and a file added with `file=1` reach a UDF.
   - There is no e2e test of a `Path` local path. A server refuses a local 
destination unless it starts with the static 
`spark.sql.artifact.copyFromLocalToFs.allowDestLocal`, which CI's does not, and 
the error of that refusal is a documented difference. The offline test checks 
the upload.
   - Each new test fails on master and passes with this change.
   - `cargo test -p pyspark-rs --lib`: 13 passed.
   - The offline suite of `coverage.yml`: 6270 passed, 77 skipped.
   - `test_dropin_e2e.py` against a Spark 4.3.0 server: 314 passed.
   - **Lint and guards:**
     - `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`;
     - ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`;
     - `scripts/gen_type_stubs.py --check` and `scripts/gen_fn_dispatch.py 
--check` against the v4.3.0 source, and `test_type_stubs_offline.py` with mypy 
2.4.1: OK, and 7 passed (its 2 pyright tests skip without pyright, which CI 
runs);
     - the guards of `coverage.yml` and `build_python_connect.yml`.
   
   ### 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