james-smith-uk opened a new pull request, #60618:
URL: https://github.com/apache/spark/pull/60618
### What changes were proposed in this pull request?
This PR makes the Python Spark Connect client log every `AnalyzePlan` RPC at
INFO level, together with its duration and the user code that triggered it:
```
AnalyzePlan(schema) took 412 ms, triggered by columns at /app/my_job.py:19
```
- The hook is in `SparkConnectClient._analyze()`, so it covers every eager
analysis path (`schema`, `columns`, `dtypes`, `printSchema`, `explain`,
`isStreaming`, `isLocal`, `inputFiles`, `sameSemantics`, `semanticHash`,
`persist`, ...) as well as internal callers.
- The call site comes from the existing `_retrieve_stack_frames()` helper,
the one already used for `SPARK_CONNECT_DEBUG_CLIENT_CALL_STACK`. The last user
frame gives the file and line, and the PySpark function called from that frame
(e.g. `columns`) names the API that triggered the analysis.
- The duration includes retries. Failed calls are logged too.
- Logging uses the existing Spark Connect logger and
`SPARK_CONNECT_LOG_LEVEL`. No new configuration is added. When INFO is not
enabled, the client does not inspect the stack or time the call.
### Why are the changes needed?
In Spark Connect, accessing `df.schema`, `df.columns`, `df.dtypes` and
similar APIs sends a synchronous `AnalyzePlan` RPC that resolves the plan on
the server. In client code these calls look like cheap attribute accesses, but
each one is a network round trip plus server-side analysis. In loops, helper
functions or library code they can dominate a job's runtime, especially against
remote or serverless servers.
Today the only way to see these calls is `SPARK_CONNECT_LOG_LEVEL=DEBUG`.
That dumps the full request and response protos for every RPC, and it does not
show which user code issued the call or how long it took. With this change,
`SPARK_CONNECT_LOG_LEVEL=INFO` gives one concise line per analysis round trip,
pointing at the code that caused it, so users can find and remove unnecessary
analysis.
### Does this PR introduce _any_ user-facing change?
Yes, but only when the Spark Connect logger is enabled at INFO or a more
verbose level (e.g. `SPARK_CONNECT_LOG_LEVEL=INFO`). In that case, one INFO
line is logged per `AnalyzePlan` RPC. Behavior is unchanged by default, because
the Spark Connect logger is disabled unless configured.
Example (timings are illustrative):
```python
df = spark.range(3)
def transform(df):
return [c.upper() for c in df.columns] # line 19
transform(df)
transform(df) # schema is cached, no RPC, nothing logged
df.select("id").dtypes # line 23, new DataFrame
```
```
AnalyzePlan(schema) took 35 ms, triggered by columns at /app/my_job.py:19
AnalyzePlan(schema) took 31 ms, triggered by dtypes at /app/my_job.py:23
```
### How was this patch tested?
New unit tests in `python/pyspark/sql/tests/connect/client/test_client.py`,
using the existing mock stub:
- `test_analyze_logs_duration_and_call_site`: checks the log format, method,
triggering API and `file:line`.
- `test_analyze_logs_unknown_call_site`: no user frames are available.
- `test_analyze_logs_failed_call`: the RPC fails and is still logged.
- `test_analyze_does_not_capture_call_site_when_logging_disabled`: no log
and no stack inspection when logging is disabled.
```
python/run-tests --testnames 'pyspark.sql.tests.connect.client.test_client'
```
I also checked it manually with a script outside the PySpark source tree
against a mocked server: a cached schema is not logged again, and the reported
call sites match the user code lines.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Code, 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]