Add Python bindings for accessing ExecutionMetrics#1381
Add Python bindings for accessing ExecutionMetrics#1381ShreyeshArangath wants to merge 2 commits intoapache:mainfrom
Conversation
timsaucer
left a comment
There was a problem hiding this comment.
At a high level, I think this could bring a lot of value. Thank you for putting in the work!
From an implementation perspective, did you consider instead of caching the prior execution plan that instead we simply add the collect() and execute_stream() and so forth on PyExecutionPlan? It seems like that would more closely mirror the upstream repo and simplify the code. I haven't spent a lot of time going through the details of why you're caching the prior plan, so it's very possible I missed something.
|
@timsaucer Thanks for the suggestion! Initially when I designed the change, I did consider moving Today, I think the users naturally treat a dataframe as the primary handle for a query: df = ctx.sql("SELECT * FROM t WHERE column1 > 1")
batches = df.collect()Requiring metrics to go through ExecutionPlan would effectively change the model to look something like so df = ctx.sql("SELECT * FROM t WHERE column1 > 1")
plan = df.execution_plan()
batches = plan.collect()
metrics = plan.collect_metrics()I thought that this would require users to restructure pipelines and thread a plan object through call chains purely to have access to metrics. The LoE required to get people to use it seemed high to me. My goal was to make minimal changes to how users can add support for metrics without changing how they run queries df = ctx.sql("SELECT * FROM t WHERE column1 > 1")
batches = df.collect()
plan = df.execution_plan()
metrics = plan.collect_metrics()I’m happy to switch to the plan-based approach if we prefer stronger alignment with the upstream API, but I leaned toward this design to make observability easier to adopt without disrupting current usage patterns — lmk what you think |
Which issue does this PR close?
Closes #1379
Rationale for this change
Today, DataFusion Python only exposes execution metrics through formatted console output via
explain(analyze=True). This makes it difficult to programmatically inspect execution behavior.There is currently no structured python API to access per-operator metrics such as
output_rows,elapsed_compute,spill_countand other runtime metrics collected during execution.This PR introduces APIs to surface the execution metrics, mirroring the Rust API in
datafusion::physical_plan::metrics.What changes are included in this PR?
PyDataFrameso the physical plan used during execution is retained and available for metrics access.metrics()method and addedcollect_metrics()helper to walk the execution plan tree and aggregate metrics from all operators.Are there any user-facing changes?
Users can now programmatically access execution metrics