Seeing AQE in the Spark UI
Concepts covered: Adaptive Query Execution
Knowing the flag is on tells you a plan may have changed. The Spark UI tells you whether it did, and at which node. The UI is a set of web pages that the driver serves while an application runs, on port 4040 by default. The page for this job is the tab labelled SQL / DataFrame (plain SQL on older releases), which lists one entry for each query the application has run.
| Step | Where to click | What to read |
|---|---|---|
| 1 | The SQL / DataFrame tab | The row for your query, found by its description and duration |
| 2 | The query's description link | The plan drawn as a graph of boxes |
| 3 | Details, below the graph | The same plan as text, root node first |
| 4 | The first line of that text | `AdaptiveSparkPlan` and its `isFinalPlan` value |
The root node says whether the plan is settled
When AQE handles a query, the root of its plan is a node named AdaptiveSparkPlan, and that node carries one field, isFinalPlan. While the query is running the field reads false: Spark may still rewrite what has not run, and the page updates as it does. Once the query finishes it reads true, and the text under Details splits into 2 labelled parts, == Final Plan == and == Initial Plan ==.
The Initial Plan is what the optimizer wrote from estimates. The Final Plan is what ran. Spotting a runtime change is a comparison of the 2, and for the new-releases join it comes out as follows.
| What to compare | Initial Plan | Final Plan |
|---|---|---|
| Join node | `SortMergeJoin` | `BroadcastHashJoin` |
| Sort nodes under the join | 2, one per side | None |
| Nodes ending in `QueryStage` | None | One at each measured pause |
| Nodes starting with `AQE` | None | `AQEShuffleRead` |
SortMergeJoin is the plan's name for the shuffle join Spark chose from its 2GB estimate. BroadcastHashJoin is the broadcast it switched to once the new titles measured 3MB. A different operator at the same position in the tree is how a runtime change shows up in the plan.
3 kinds of difference, and what each one means
- A changed operator: The node at one position has a different name in each plan, as the join does above.
- A node starting with AQE: It never appears in an Initial Plan. It marks a place where Spark adjusted how shuffle output is read.
- A node ending in QueryStage: A shuffle or broadcast where Spark paused and measured. Every adaptive Final Plan has these, so on their own they say nothing about a change.
On Spark 3.0 and 3.1 the AQEShuffleRead node is printed as CustomShuffleReader.
The same check from code
explain() prints the same plan that the UI draws. Called before the action, it shows the Initial Plan under isFinalPlan=false, a draft that can mislead you about what will run. Called on the same DataFrame after collect(), it shows both plans. count() and show() build a separate plan of their own and run that, so they leave this DataFrame's plan marked as a draft.
new_titles = titles.filter(titles.is_new == 1)
minutes = (
views.join(new_titles, "title_id")
.groupBy("name")
.sum("minutes")
)
minutes.explain()
minutes.collect()
minutes.explain()A plan with no AdaptiveSparkPlan root
2 situations produce a plan without that root node. In one, the flag is false. In the other, the query is built only from narrow transformations, a filter followed by a select for example, so it has no shuffle to pause at. Either way, the plan on the page is the only plan there was.
A finished query's Details begin with AdaptiveSparkPlan isFinalPlan=true. The join node is SortMergeJoin in the Initial Plan and in the Final Plan. What does that tell you?
- AQE was off for this query, because an enabled AQE always replaces a SortMergeJoin with a broadcast.
- The query is still running, because the 2 plans stay identical until the last task has finished.
- The join never executed, because matching Initial and Final Plans mean the action was skipped.
- AQE measured the shuffle output and kept the join as planned, because no measured side was small enough to broadcast.
The routine has 3 moves: find the root, read isFinalPlan, then put the join node of each plan next to the other. Do it on a few of your own queries while nothing is wrong, so the layout is familiar on the day a run is slow.
About this lesson
This section is part of the Adaptive Query Execution: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.