Skip to content

feat(physical-plan): expose finalized Accumulator state on BoundedWindowAggExec - #24031

Draft
avantgardnerio wants to merge 1 commit into
apache:mainfrom
avantgardnerio:brent/bwag-finalized-state-slim
Draft

feat(physical-plan): expose finalized Accumulator state on BoundedWindowAggExec#24031
avantgardnerio wants to merge 1 commit into
apache:mainfrom
avantgardnerio:brent/bwag-finalized-state-slim

Conversation

@avantgardnerio

Copy link
Copy Markdown
Contributor

Summary

Adds per-output-partition slots on BoundedWindowAggExec that capture each aggregate window function's final Accumulator::state() at partition-close, plus a getter (finalized_partition_state) for downstream operators to read once a task has drained. Intended to support prefix scans over ordered window aggregates.

Slim re-do of #24007 addressing the review there. Public surface is a single new pub fn on BWAG plus one method on the already-public WindowState; no new pub types or re-exports.

What changed vs. #24007

Concern from review This PR
pub use window_expr::WindowFn leaked internal enum with Box<dyn Accumulator> / Box<dyn PartitionEvaluator> variants Removed. Added WindowState::aggregate_state() -> Result<Option<Vec<ScalarValue>>> on the already-public type.
pub type FinalizedPartitionState alias Removed from public API; private alias exists only to satisfy clippy's type_complexity.
~30-line getter with Empty/Single/Multi state machine + out-of-range error + mutex-poison error 1-line getter returning Option<&[Option<Vec<ScalarValue>>]>.
Arc<[Mutex<FinalStateSlot>]> Arc<[OnceLock<Vec<Option<Vec<ScalarValue>>>>]> — no Mutex, no state machine, first-close-wins.
"At most one PARTITION BY group" doctrine enforced with an error branch Enforcement dropped, doctrine kept as one line of docstring. Additional groups on the same output partition are silently dropped.

Diff: 429 → 168 lines.

Test plan

  • cargo test -p datafusion-physical-plan --lib windows::bounded_window_agg_exec::tests::finalized_partition_state_returns_single_group_state
  • cargo test -p datafusion-physical-plan --lib windows (11 passed)
  • cargo clippy -p datafusion-physical-plan -p datafusion-physical-expr --all-targets (clean)
  • cargo fmt --all --check

…dowAggExec

Per-output-partition slots hold each aggregate window function's final
Accumulator::state() at partition-close, plus a getter that operators
can call once a task has drained. Intended to support prefix scans over
ordered window aggregates.

The stream snapshots state at the top of prune_state — the last moment
state.is_end entries are live before either prune_out_columns or
prune_partition_batches drops them — so both mid-stream partition close
(as SQL PARTITION BY groups end) and EOS are covered by one write site.
Non-aggregate window functions (row_number, rank, lead/lag, ...) occupy
None at their slot.

Storage is Arc<[OnceLock<Vec<Option<Vec<ScalarValue>>>>]> — one slot per
output partition, first-close-wins. The getter is scoped to the
at-most-one-PARTITION-BY-group case; additional groups are silently
dropped rather than merged (no well-defined semantics for combining).

No trait changes: uses the existing Accumulator::state() surface already
relied on by the group-by Partial/Final protocol, exposed through a new
method on the already-public WindowState so no internal enum leaks into
the public API.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate labels Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant