RamonZhou opened a new pull request, #60474:
URL: https://github.com/apache/spark/pull/60474
### What changes were proposed in this pull request?
This PR adds `SparkSession.listFiles()` and `SparkSession.listArchives()` to
PySpark, for both Spark Connect and Spark Classic. They return what
`SparkContext.listFiles` and `SparkContext.listArchives` return: the URIs of
the files and archives that the session's jobs make available to their tasks.
- **Spark Classic**: they return `SparkContext.listFiles` /
`SparkContext.listArchives` directly.
- **Spark Connect**: they run the existing `LIST FILES` / `LIST ARCHIVES`
commands, so they need no new protocol message.
The server-side change makes `LIST FILES`, `LIST JARS` and `LIST ARCHIVES`
list the session's `JobArtifactSet`, the set its jobs actually ship to tasks,
instead of everything on the `SparkContext`:
- When the session's artifacts are isolated
(`spark.sql.artifact.isolation.enabled`, always on for Spark Connect), they
list only what the session added: with `addArtifacts(file=True / pyfile=True /
archive=True)`, `ADD FILE`, `ADD JAR` or `ADD ARCHIVE`.
- When they are not isolated, which is the default for Spark Classic,
`JobArtifactSet.getActiveOrDefault` falls back to everything on the
`SparkContext`. The commands return exactly what they return today.
As with `SparkContext`:
- Files added as Python files (`pyfile=True`) are listed as files.
- An archive keeps the `#fragment` it was added with.
- Adding the same file twice lists it once.
### Why are the changes needed?
`SparkContext.listFiles` / `listArchives` have no equivalent in Spark
Connect, which has no `SparkContext`. Code moving from Spark Classic to Spark
Connect has to track what it added itself.
The `LIST FILES` / `LIST ARCHIVES` commands do work over Spark Connect
today, but they list the files and archives of **every** session on the server.
That includes other users' sessions. It is also not what the session's tasks
receive: a session's tasks only get the session's own artifacts. With two Spark
Connect sessions on a Spark 4.1.2 server, the second session lists the first
session's artifacts:
```
s1.listFiles():
['spark://<host>:<port>/artifacts/<s1-uuid>/files/e2e_file.txt', ...]
s2.listFiles():
['spark://<host>:<port>/artifacts/<s1-uuid>/files/e2e_file.txt', ...] # s1's
files
```
### Does this PR introduce _any_ user-facing change?
Yes.
- New PySpark APIs `SparkSession.listFiles()` and
`SparkSession.listArchives()` (classic and Connect).
- With isolated artifacts (Spark Connect), `LIST FILES`, `LIST JARS` and
`LIST ARCHIVES` list only the session's own artifacts instead of every
session's.
- Spark Classic, whose sessions do not isolate artifacts, is unchanged.
### How was this patch tested?
New tests:
- `ArtifactManagerSuite` (isolated sessions, as on a Spark Connect server).
One session adds a file, a Python file, a jar and an archive with a fragment.
`LIST FILES/JARS/ARCHIVES` list exactly those in that session and nothing in a
second session, while `SparkContext.listFiles/listArchives` still list
everything.
- `ArtifactTestsMixin.check_list_files_and_archives`, shared by the classic
and Connect suites. It covers files, Python files, archives with a fragment,
and no duplicates on a second add.
- Connect tests (`test_artifact`, `test_artifact_localcluster`):
- A new session lists none of another session's artifacts and can add the
same ones.
- Files and archives added with `ADD FILE` / `ADD ARCHIVE` are listed in
that session only.
- Classic test (`test_artifact`): `SparkSession.listFiles()/listArchives()`
equal `SparkContext.listFiles/listArchives` in every session, including a file
added with `SparkContext.addFile`, and equal `LIST FILES` / `LIST ARCHIVES`.
Run locally:
- `ArtifactManagerSuite` 26/26.
- `HiveQuerySuite` ADD/LIST tests 10/10 (existing coverage of the classic
commands).
- `pyspark.sql.tests.test_artifact` 11/11.
- `pyspark.sql.tests.connect.client.test_artifact` 18/18.
- `pyspark.sql.tests.connect.client.test_artifact_localcluster` 9/9.
- `pyspark.sql.tests.test_connect_compatibility` 19/19.
- The `pyspark.sql.session` and `pyspark.sql.connect.session` doctests.
- `sql/scalastyle` and `sql/Test/scalastyle` are clean, and `ruff format` is
clean.
End to end:
- I started a Spark Connect server from this build
(`sbin/start-connect-server.sh`) and connected two sessions. Session 1 added a
file, a Python file, an archive `#e2e_alias`, and a file through `ADD FILE`.
`s1.listFiles()` / `s1.listArchives()` returned exactly those, and
`s2.listFiles()` / `s2.listArchives()` returned `[]`. A UDF in session 1
imported the listed Python file; the same UDF in session 2 failed with
`ModuleNotFoundError`, matching its empty list.
- Against an unmodified Spark 4.1.2 Connect server, the new client methods
work as well (they only use `LIST FILES` / `LIST ARCHIVES`) but return every
session's artifacts, as above.
- In Spark Classic, `spark.listFiles()`, `spark.newSession().listFiles()`
and `LIST FILES` all equaled `sc.listFiles`, and `spark.listArchives()` equaled
`sc.listArchives`.
### 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]