Skip to content

refactor(hash-aggr): Migrate partial-reduce hash aggregation#23233

Merged
2010YOUY01 merged 5 commits into
apache:mainfrom
2010YOUY01:split-aggr-partial-reduce
Jul 3, 2026
Merged

refactor(hash-aggr): Migrate partial-reduce hash aggregation#23233
2010YOUY01 merged 5 commits into
apache:mainfrom
2010YOUY01:split-aggr-partial-reduce

Conversation

@2010YOUY01

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

Part of #22710

This PRs implements the partial-reduce aggregation.

Comments at datafusion/physical-plan/src/aggregates/partial_reduce_stream.rs explains the high-level idea.

What changes are included in this PR?

  • Adds an AggregateHashTable<PartialReduceMarker> variant to handle partial-reduce aggregation.
  • Adds PartialReduceHashAggregateStream to implement the partial-reduce state machine.

Are these changes tested?

Covered by existing tests

Are there any user-facing changes?

No

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Jun 29, 2026
@2010YOUY01
2010YOUY01 requested a review from alamb June 29, 2026 11:41
@alamb

alamb commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

This is on my review radar

…duce

# Conflicts:
#	datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs
#	datafusion/physical-plan/src/aggregates/aggregate_hash_table/final_table.rs
#	datafusion/physical-plan/src/aggregates/aggregate_hash_table/partial_table.rs
@github-actions github-actions Bot added the execution Related to the execution crate label Jun 30, 2026
@alamb

alamb commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

I merged up from main to resolve a conflict

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me -- thank you @2010YOUY01

The only thing I found (with codex) is that PartialReduceHashAggregateStream is also used regardless of the memory pool sizes even though it doesn't implement memory reservations / OOM behavior yet

So I suggest we disable this special stream when there is a memory limit -- similarly to Partial hash stream guard:

fn should_use_partial_hash_stream(&self, context: &TaskContext) -> bool {
// TODO: implement memory-limited path and remove this limitation
if matches!(context.memory_pool().memory_limit(), MemoryLimit::Finite(_)) {
return false;
}

This is similar to the other refactor (and the fix is 2010YOUY01#4)

/// ------ RepartitionExec(hash(k))
/// -------- AggregateExec(stage=partial)
///
/// Note: the example plan is only intended to demonstrate this stream's semantics;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

use datafusion_common::{Result, internal_err};
use datafusion_expr::EmitTo;

use crate::aggregates::AggregateExec;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that this code is covered by existing tests:

cargo llvm-cov -p datafusion-physical-plan --lib --html -- partial_reduce

@2010YOUY01

Copy link
Copy Markdown
Contributor Author

The only thing I found (with codex) is that PartialReduceHashAggregateStream is also used regardless of the memory pool sizes even though it doesn't implement memory reservations / OOM behavior yet

So I suggest we disable this special stream when there is a memory limit -- similarly to Partial hash stream guard:

Thanks for the review. I agree we should not use the new path if there is the memory limit.

One thing I noticed is that the original PR added spilling and streaming merge support for PartialReduce aggregation, but I don’t see any tests for the PartialReduce + memory limit case, or much discussion about the expected OOM behavior.

For this use case, I think early emit is probably both more efficient and simpler than supporting spilling / streaming merge.

Should we make this path explicitly error on OOM instead? That would keep the behavior more constrained for now. Since the existing feature does not seem to have test coverage, relying on the current implementation feels a bit risky, and we can reopen the discussion later if users depend on this behavior. 🤔

@2010YOUY01

Copy link
Copy Markdown
Contributor Author

So I suggest we disable this special stream when there is a memory limit -- similarly to Partial hash stream guard:

disabled in 08558c6

@alamb

alamb commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Shall we merge it?

@2010YOUY01
2010YOUY01 added this pull request to the merge queue Jul 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 3, 2026
@2010YOUY01
2010YOUY01 added this pull request to the merge queue Jul 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 3, 2026
@2010YOUY01
2010YOUY01 added this pull request to the merge queue Jul 3, 2026
Merged via the queue into apache:main with commit e4aa41d Jul 3, 2026
60 of 61 checks passed
@2010YOUY01
2010YOUY01 deleted the split-aggr-partial-reduce branch July 3, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

execution Related to the execution crate physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants