Skip to content

[Exploration] DataFusion engine E0a: re-run a compiled ExecutionPlan per batch #1504

Backlog

Description

@milindsrivastava1997

Example

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.

Activity

  1. milindsrivastava1997 commented on Dec 2, 2026

    @milindsrivastava1997
    CollaboratorAuthor

    Findings (DataFusion 54.1.0, Arrow 58.4)

    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.

    Recommended pattern for E1 (#1507)

    1. Install: build each tree once into an Arc<dyn ExecutionPlan> template. The template is never executed itself.
    2. 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.
    3. 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().
    4. Do not use with_new_children as a reset: it leaves HashJoinExec and TopK stale.
    5. 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).
    • Release-build timing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions