You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Exploration] DataFusion engine E0a: re-run a compiled ExecutionPlan per batch #1504
The engine compiles each tree of an installed plan once (D6) and runs it for every batch, e.g. sum(rate(http_requests_total[5m])) evaluated every 15 s. Today nothing confirms DataFusion 54.1 allows this.
Question and hypothesis
Can an Arc<dyn ExecutionPlan> built once be executed again for a later batch, with the batch's inputs (evaluation times, store handles) supplied through the TaskContext/session config rather than baked into the plan? Hypothesis: yes, if leaf operators read their inputs in execute(), but some built-in operators (e.g. HashJoinExec's build side OnceFut, RepartitionExec state) cache state across execute() calls. (D6)
Success signal
A throwaway binary that runs one compiled plan (custom leaf → FilterExec → AggregateExec → HashJoinExec → SortExec) for three batches with different leaf data and gets three correct answers, or a list of the operators that cannot be re-run and the workaround (reset_state(), with_new_children, or re-instantiating per batch).
Scope or time boundary
Two days. DataFusion 54.1.0 / Arrow 58.4 as pinned in the workspace (D15).
Cheapest discriminating experiment
Build the plan above by hand, run it three times, and compare with a fresh plan per run.
Expected artifact
A comment on this issue: what works, what doesn't, the recommended pattern for E1. Throwaway code is not merged.
Production shortcuts and limitations
None; findings only.
Exit decision: adopt, investigate, or stop
Adopt: E1 compiles once. Otherwise: E1 rebuilds the tree per batch from a cached description and D6 is updated.
Short answer: yes, if every batch runs reset_plan_states(compiled.clone()) before execute(). Executing the same Arc again gives wrong answers or panics.
Setup: throwaway example (not committed) with a custom leaf that holds no data and reads its rows in execute() from TaskContext → session_config().get_extension::<BatchInputs>(). The tree is leaf → FilterExec → AggregateExec → HashJoinExec → SortExec, plus variants that test single operators. Each variant is compiled once and run for 3 batches in sequence and 2 batches concurrently, and each result is compared with a freshly built plan. No optimizer rules run (D6).
Results
Operator
Same Arc again
with_new_children (same children)
reset_plan_states
BatchLeafExec (custom, no state)
ok
ok
ok
FilterExec
ok
ok
ok
AggregateExec (Single, grouped; Partial min with no grouping)
ok
ok
ok
SortExec (no fetch)
ok
ok
ok
HashJoinExec CollectLeft
wrong: reuses batch 1's build side (left_fut: Arc<OnceAsync>)
wrong: the builder copies the same left_fut Arc
ok
HashJoinExec Partitioned
panic (from its RepartitionExec inputs)
ok
ok
SortExec with fetch (TopK)
wrong: the shared TopKDynamicFilters threshold from batch 1 drops rows of later batches
wrong: the filter Arc is copied
ok
RepartitionExec
panic"partition not used yet" (state in Arc<Mutex<RepartitionExecState>>)
ok
ok
CrossJoinExec
wrong (left_fut: OnceAsync)
ok
ok
NestedLoopJoinExec
wrong (build_side_data: OnceAsync)
ok
ok
Running two batches concurrently on one compiled Arc is wrong for every join. With a reset_plan_states copy per batch, both concurrent batches are correct.
AggregateExec has no reset_state override. Its min/max dynamic filter (Partial mode, no grouping) persists across runs, but it only affects results when filter pushdown has wired it into a child, and we don't run that rule.
Metrics accumulate across runs on the same Arc. reset_plan_states clears them.
Cost: reset_plan_states takes about 18 µs on the 7-node tree versus about 300 µs for a full rebuild (debug build). It reuses the PhysicalExprs, schemas and cached properties.
Under the hood, reset_plan_states is transform_up(|p| p.reset_state()). DataFusion's own RecursiveQueryExec uses it to re-run its recursive term.
Install: build each tree once into an Arc<dyn ExecutionPlan>template. The template is never executed itself.
Per batch: let plan = reset_plan_states(Arc::clone(&template))?; plan.execute(0, task_ctx). Here task_ctx carries the batch's inputs (evaluation times, scratch store, view-store and sketch-registry handles) as a SessionConfig extension.
Leaf and custom ASAP operators read their inputs in execute() and keep per-run state only in the stream they return. An operator that must hold state in its struct overrides reset_state().
Do not use with_new_children as a reset: it leaves HashJoinExec and TopK stale.
Acceptance test for E1: run each compiled tree for 2+ batches with different inputs, both in sequence and concurrently, and compare with a fresh build. The joins and TopK catch a missing reset.
Does D6 ("compile once") need to change?
The decision stands; only its wording needs to change: "each tree is compiled once at install; each batch executes a reset_plan_states copy of it, never the compiled plan itself." E1 does not need to rebuild per batch from a cached description.
Not covered
Dynamic-filter pushdown into leaves (the rule is off per D6).
Sort-merge join, symmetric hash join and window operators (not in D13's first set).
Activity
milindsrivastava1997 commentedon Dec 2, 2026
Findings (DataFusion 54.1.0, Arrow 58.4)
Short answer: yes, if every batch runs
reset_plan_states(compiled.clone())beforeexecute(). Executing the sameArcagain gives wrong answers or panics.Setup: throwaway example (not committed) with a custom leaf that holds no data and reads its rows in
execute()fromTaskContext → session_config().get_extension::<BatchInputs>(). The tree is leaf →FilterExec→AggregateExec→HashJoinExec→SortExec, plus variants that test single operators. Each variant is compiled once and run for 3 batches in sequence and 2 batches concurrently, and each result is compared with a freshly built plan. No optimizer rules run (D6).Results
Arcagainwith_new_children(same children)reset_plan_statesBatchLeafExec(custom, no state)FilterExecAggregateExec(Single, grouped; Partialminwith no grouping)SortExec(no fetch)HashJoinExecCollectLeftleft_fut: Arc<OnceAsync>)left_futArcHashJoinExecPartitionedRepartitionExecinputs)SortExecwith fetch (TopK)TopKDynamicFiltersthreshold from batch 1 drops rows of later batchesRepartitionExec"partition not used yet"(state inArc<Mutex<RepartitionExecState>>)CrossJoinExecleft_fut: OnceAsync)NestedLoopJoinExecbuild_side_data: OnceAsync)Arcis wrong for every join. With areset_plan_statescopy per batch, both concurrent batches are correct.AggregateExechas noreset_stateoverride. Its min/max dynamic filter (Partial mode, no grouping) persists across runs, but it only affects results when filter pushdown has wired it into a child, and we don't run that rule.Arc.reset_plan_statesclears them.reset_plan_statestakes about 18 µs on the 7-node tree versus about 300 µs for a full rebuild (debug build). It reuses thePhysicalExprs, schemas and cached properties.reset_plan_statesistransform_up(|p| p.reset_state()). DataFusion's ownRecursiveQueryExecuses it to re-run its recursive term.Recommended pattern for E1 (#1507)
Arc<dyn ExecutionPlan>template. The template is never executed itself.let plan = reset_plan_states(Arc::clone(&template))?; plan.execute(0, task_ctx). Heretask_ctxcarries the batch's inputs (evaluation times, scratch store, view-store and sketch-registry handles) as aSessionConfigextension.execute()and keep per-run state only in the stream they return. An operator that must hold state in its struct overridesreset_state().with_new_childrenas a reset: it leavesHashJoinExecand TopK stale.Does D6 ("compile once") need to change?
The decision stands; only its wording needs to change: "each tree is compiled once at install; each batch executes a
reset_plan_statescopy of it, never the compiled plan itself." E1 does not need to rebuild per batch from a cached description.Not covered