ahmed-mez opened a new pull request, #18906:
URL: https://github.com/apache/datafusion/pull/18906
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases. You can
link an issue to this PR using the GitHub syntax. For example `Closes #123`
indicates that this PR will close issue #123.
-->
- Closes #.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in
the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your
changes and offer better suggestions for fixes.
-->
When a `GroupedHashAggregateStream` finishes processing its input, it emits
the accumulated groups. The current implementation materializes **all groups**
into a single `RecordBatch` (via `emit(EmitTo::All)`) before slicing it into
smaller batches.
For queries with high-cardinality grouping keys (e.g., >500k groups) or
complex types (Strings, Lists), this single emission step becomes a blocking
operation that can stall the async runtime for seconds. This "long poll"
prevents other tasks from running, leading to latency spikes and reduced
concurrency.
This PR changes the emission strategy to respect the configured `batch_size`
during the drain phase, emitting groups incrementally instead of all at once.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is
sometimes worth providing a summary of the individual changes in this PR.
-->
1. Incremental Emission Logic:
- Introduced a new `ExecutionState::DrainingGroups` in
`GroupedHashAggregateStream`.
- Modified `poll_next` to handle this state by emitting `batch_size`
groups at a time.
- Added `input_done()` to the `GroupValues` trait to signal the
transition to drain mode.
2. GroupValues Updates:
- Implemented `input_done()` in `GroupValues` implementations (e.g.,
`GroupValuesRows`).
- In `GroupValuesRows`, `input_done()` clears the hash map (to free
memory immediately) and sets a `drain_mode` flag to optimize sequential
emission.
3. Test Case:
- Added `test_chunked_group_emission` to verify that the chunked
emission behavior works correctly:
- Verifies that groups are emitted in multiple batches (not all at
once).
- Confirms that batch sizes respect the configured `batch_size`
limit.
- Ensures all groups are eventually emitted.
- Added `test_long_poll_reproducer` to `aggregates/mod.rs` which
demonstrates the latency improvement (from ~2.8s to ~2.1s in local tests) and
verifies that results are emitted in multiple small batches rather than one
huge batch. This test case doesn't have to be committed, its purpose is mainly
to demo the issue and the fix.
## Are these changes tested?
Yes, two new test cases have been added.
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are
they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
- Performance: Users running large aggregations should see lower latency
to the first result and fewer "hiccups" (long stalls) in their query engine.
- Memory: Hash maps are cleared slightly earlier (when input is done),
potentially freeing memory sooner during emission.
- API: No public API changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]