Visitar URL original
fix: count early emitted groups in the partial aggregation skip probe by comphead · Pull Request #27351 · apache/datafusion · GitHub
Skip to content

fix: count early emitted groups in the partial aggregation skip probe - #27351

Draft
comphead wants to merge 1 commit into
apache:mainfrom
comphead:fix-27350-skip-probe-early-emit
Draft

comphead wants to merge 1 commit into
apache:mainfrom
comphead:fix-27350-skip-probe-early-emit

Conversation

@comphead

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Under memory pressure the Partial aggregate emits its groups early and continues with an empty table. The skip probe then compared all input rows with only the groups since the last emit, so it stayed below skip_partial_aggregation_probe_ratio_threshold even for unique keys. The query sends the same rows downstream either way, but spends several times the Partial CPU hashing them.

What changes are included in this PR?

PartialHashAggregateStream and the legacy GroupedHashAggregateStream count the groups they emit early and pass them to SkipAggregationProbe together with the groups still in the table. The sum is the output row count so far, so the probe measures the reduction the Partial actually achieves.

What is the testing strategy for this PR?

test_partial_aggregation_skips_after_early_emits runs both streams over unique keys with a memory limit that forces early emits before the probe threshold. On main both skip 0 rows.

datafusion-cli with the query from the issue (2M unique keys, target_partitions = 2), AggregateExec: mode=Partial, 3 runs:

Memory pool main emits / skipped / CPU This PR emits / skipped / CPU
None 0 / 1.79 M / 6.4 ms 0 / 1.79 M / 6.7 ms
memory_limit = '4M' 87–102 / 0 / 33 ms 5–7 / 1.79 M / 6.9 ms
-m 16M --mem-pool-type fair 46–47 / 0 / 32 ms 4 / 1.79 M / 6.8 ms

With 20M rows over 200K keys, 8 partitions and a fair 32M pool, Partial CPU drops from 345 ms to 46 ms. Queries that still reduce well under memory pressure (20K or 2K keys) keep aggregating.

Are there any user-facing changes?

No API changes. Memory-limited queries over high-cardinality keys now skip partial aggregation, as they already do without a limit.

Under memory pressure the Partial aggregate emits its groups early and
continues with an empty table. The skip probe then paired the cumulative
input rows with only the groups since the last emit, so it stayed below
the ratio threshold even for unique keys and the stream kept hashing.

Count the groups emitted early and pass them to the probe together with
the groups still in the table, in both `PartialHashAggregateStream` and
the legacy `GroupedHashAggregateStream`.

Closes apache#27350
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Nov 12, 2026

@vinycalves vinycalves left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One logic question; no code suggestions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could counting early-emitted groups make the probe skip a low-cardinality query before later duplicates reduce it? Is there a test for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good question. I was actually thinking about the same yesterday, what would happen if high cardinality groups come first and break the pattern, planning to resume this today

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i'll wait for your next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partial aggregation stops skipping after early emits under memory pressure

2 participants