|
| 1 | +# How Adaptive Query Execution Works |
| 2 | + |
| 3 | +The normal distributed planner decides all stage boundaries and task counts |
| 4 | +before execution begins. Adaptive Query Execution (AQE) delays those decisions: |
| 5 | +it starts at the leaves, executes enough of each producer stage to measure its |
| 6 | +output, and uses those measurements to size the next stage. |
| 7 | + |
| 8 | +AQE sizes each stage using a cost model that combines the amount of data flowing |
| 9 | +through each node with the estimated compute cost of that node. A stage that |
| 10 | +moves a large amount of data through fully streaming operators with little |
| 11 | +per-row computation may need only a few tasks. In contrast, a stage containing |
| 12 | +compute-intensive operators may be assigned more tasks even when relatively |
| 13 | +little data flows through it. |
| 14 | + |
| 15 | +AQE estimates the amount of data flowing through a stage in two ways: |
| 16 | + |
| 17 | +1. For leaf stages, it uses the statistics returned by |
| 18 | + `ExecutionPlan::partition_statistics`, relying on DataFusion's upstream |
| 19 | + statistics machinery. |
| 20 | +2. For intermediate stages, it infers statistics by sampling data at runtime as |
| 21 | + the stages below them execute. |
| 22 | + |
| 23 | +## Building the plan from the leaves up |
| 24 | + |
| 25 | +Unlike the static distributed planner, AQE does not build every stage before |
| 26 | +execution starts. It begins with the original DataFusion physical plan, where no |
| 27 | +network boundaries or distributed task counts have been assigned yet. A |
| 28 | +simplified plan might look like this: |
| 29 | + |
| 30 | +```text |
| 31 | +SortPreservingMergeExec |
| 32 | + SortExec |
| 33 | + AggregateExec: Final |
| 34 | + RepartitionExec |
| 35 | + AggregateExec: Partial |
| 36 | + DataSourceExec |
| 37 | +``` |
| 38 | + |
| 39 | +The coordinator walks this plan from the leaves upward. When it reaches the |
| 40 | +first point where a network boundary is required, it has enough information to |
| 41 | +close the plan below that boundary as the first stage. Because this stage |
| 42 | +contains a data-source leaf, its cost is calculated from |
| 43 | +`ExecutionPlan::partition_statistics`. The cost model and the registered |
| 44 | +`TaskEstimator` are then used to choose its task count: |
| 45 | + |
| 46 | +```text |
| 47 | +SortPreservingMergeExec |
| 48 | + SortExec |
| 49 | + AggregateExec: Final |
| 50 | + ▲ |
| 51 | + │ future network boundary |
| 52 | + │ |
| 53 | +┌───── Stage 1 ── tasks=4 ──────────┐ |
| 54 | +│ RepartitionExec │ |
| 55 | +│ AggregateExec: Partial │ |
| 56 | +│ DataSourceExec │ |
| 57 | +└───────────────────────────────────┘ |
| 58 | + ▲ |
| 59 | + └── sized from leaf statistics |
| 60 | +``` |
| 61 | + |
| 62 | +The coordinator inserts a `SamplerExec` directly below the producer-head |
| 63 | +`RepartitionExec` and sends Stage 1 to its workers. The workers begin executing |
| 64 | +it before the stage above has been sized. The sampler holds on to the batches |
| 65 | +that reach it and reports information about them back to the coordinator: |
| 66 | + |
| 67 | +```text |
| 68 | + coordinator |
| 69 | + ▲ |
| 70 | + │ sampled rows, bytes, |
| 71 | + │ distinct values, and nulls |
| 72 | + │ |
| 73 | +┌───── Stage 1 ── tasks=4 ──────────┐ |
| 74 | +│ RepartitionExec │ |
| 75 | +│ SamplerExec │◀── injected by AQE |
| 76 | +│ AggregateExec: Partial │ |
| 77 | +│ DataSourceExec │ |
| 78 | +└───────────────────────────────────┘ |
| 79 | +``` |
| 80 | + |
| 81 | +The sampled batches are not discarded. They remain buffered until the consumer |
| 82 | +stage starts and are then returned as part of the normal execution stream. |
| 83 | + |
| 84 | +The coordinator turns the sample into DataFusion `Statistics` and attaches them |
| 85 | +to the network boundary that represents Stage 1. From the point of view of the |
| 86 | +operators above it, that boundary now behaves like a leaf with runtime-derived |
| 87 | +statistics. AQE can therefore calculate the cost of Stage 2 and choose its task |
| 88 | +count using the runtime behavior observed from Stage 1: |
| 89 | + |
| 90 | +```text |
| 91 | +┌───── Stage 2 ── tasks=2 ──────────┐ |
| 92 | +│ SortExec │ |
| 93 | +│ AggregateExec: Final │ |
| 94 | +│ [Stage 1] NetworkShuffleExec │◀── runtime statistics |
| 95 | +└───────────────────────────────────┘ |
| 96 | + ▲ |
| 97 | + │ reads from |
| 98 | + │ |
| 99 | +┌───── Stage 1 ── tasks=4 ──────────┐ |
| 100 | +│ RepartitionExec │ |
| 101 | +│ SamplerExec │ |
| 102 | +│ AggregateExec: Partial │ |
| 103 | +│ DataSourceExec │ |
| 104 | +└───────────────────────────────────┘ |
| 105 | +``` |
| 106 | + |
| 107 | +Stage 2 is then started and sampled in the same way. This process repeats until |
| 108 | +the coordinator reaches the head of the plan. The resulting distributed plan |
| 109 | +might look like this, with each task count decided using the best statistics |
| 110 | +available at that point: |
| 111 | + |
| 112 | +```text |
| 113 | +┌───── Head stage ── tasks=1 ───────┐ |
| 114 | +│ SortPreservingMergeExec │ |
| 115 | +│ [Stage 2] NetworkCoalesceExec │ |
| 116 | +└───────────────────────────────────┘ |
| 117 | + ┌───── Stage 2 ── tasks=2 ──────────┐ |
| 118 | + │ SortExec │ |
| 119 | + │ AggregateExec: Final │ |
| 120 | + │ [Stage 1] NetworkShuffleExec │ |
| 121 | + └───────────────────────────────────┘ |
| 122 | + ┌───── Stage 1 ── tasks=4 ──────────┐ |
| 123 | + │ RepartitionExec │ |
| 124 | + │ SamplerExec │ |
| 125 | + │ AggregateExec: Partial │ |
| 126 | + │ DataSourceExec │ |
| 127 | + └───────────────────────────────────┘ |
| 128 | +``` |
| 129 | + |
| 130 | +The task counts in this example are illustrative. Depending on the observed |
| 131 | +data and the operators in each stage, AQE may make an intermediate stage wider |
| 132 | +or narrower than the stage below it. |
| 133 | + |
| 134 | +## Implications of progressive planning |
| 135 | + |
| 136 | +Interleaving planning, sampling, and execution has several consequences: |
| 137 | + |
| 138 | +1. Plan fragments are sent to workers progressively. A producer stage is sent |
| 139 | + and started first, but fragments that consume its output are not sent until |
| 140 | + sampling has produced the runtime statistics needed to size them. |
| 141 | +2. Producer tasks may start before they have consumers. The sampler buffers the |
| 142 | + batches produced during this period and releases them when the downstream |
| 143 | + fragment starts consuming the stage. |
| 144 | +3. The final distributed plan does not exist at the beginning of the query. |
| 145 | + Network boundaries, task counts, and worker assignments for later stages are |
| 146 | + decided as execution moves up the plan. |
| 147 | +4. Independent branches can be sampled concurrently, but a stage that consumes |
| 148 | + several branches must wait until the required runtime statistics are |
| 149 | + available from all of them. |
| 150 | + |
| 151 | +## Deep dive into sampling |
| 152 | + |
| 153 | +Sampling needs to answer two separate questions: |
| 154 | + |
| 155 | +1. How much output has the stage produced so far? |
| 156 | +2. How far has the stage progressed through the input that drives that output? |
| 157 | + |
| 158 | +`SamplerExec` answers the first question from the batches that reach it. It |
| 159 | +buffers those batches and measures their row count, total and per-column byte |
| 160 | +sizes, distinct-value percentages, and null percentages. |
| 161 | + |
| 162 | +The leaves on the stage's **driver path** answer the second question. Their |
| 163 | +`output_rows` metrics report how many input rows have been pulled so far, while |
| 164 | +`ExecutionPlan::partition_statistics` provides an estimate of how many rows |
| 165 | +they will produce in total: |
| 166 | + |
| 167 | +```text |
| 168 | +┌───── Stage 1 ─────────────────────────────────────────────┐ |
| 169 | +│ RepartitionExec │ |
| 170 | +│ SamplerExec ◀── output produced so far │ |
| 171 | +│ AggregateExec: Partial │ |
| 172 | +│ DataSourceExec ◀── driver rows consumed │ |
| 173 | +└───────────────────────────────────────────────────────────┘ |
| 174 | + ▲ |
| 175 | + └── estimated total rows |
| 176 | + from partition_statistics |
| 177 | +``` |
| 178 | + |
| 179 | +These measurements deliberately count rows at different points in the plan. |
| 180 | +An aggregate or filter may consume many input rows while yielding only a small |
| 181 | +number of output rows. AQE therefore does not compare the sampler's output row |
| 182 | +count directly with the leaf estimate. Instead, it uses the leaf measurements |
| 183 | +to estimate progress, then applies that progress to the output observed by the |
| 184 | +sampler. |
| 185 | + |
| 186 | +### The driver path |
| 187 | + |
| 188 | +The driver path contains the operators whose continued input makes the stage |
| 189 | +produce more output. For most operators, it follows every child. For |
| 190 | +pipeline-breaking joins such as `HashJoinExec`, `NestedLoopJoinExec`, and |
| 191 | +`CrossJoinExec`, it follows only the right-hand probe side: |
| 192 | + |
| 193 | +```text |
| 194 | +SamplerExec |
| 195 | + HashJoinExec |
| 196 | + left: build side ── ignored for progress |
| 197 | + right: probe side ◀─ driver path |
| 198 | + DataSourceExec |
| 199 | +``` |
| 200 | + |
| 201 | +The build side is excluded because it must be materialized before the join can |
| 202 | +yield its first output batch. Consuming build-side rows is setup work, not an |
| 203 | +indication of how far the join has progressed through the stream that drives |
| 204 | +its output. When a driver path reaches several leaves, their estimated and |
| 205 | +consumed row counts are added together. |
0 commit comments