2010YOUY01 opened a new pull request, #23657: URL: https://github.com/apache/datafusion/pull/23657
## 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. --> Part of https://github.com/apache/datafusion/issues/22710 ## 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. Clearly explaining why changes are proposed helps reviewers understand your changes and offer better suggestions. --> This PR implements larger-than-memory execution for ordered partial and ordered final aggregation. These two modes are implemented first because: - #22710 splits ordered and unordered aggregation into separate implementations. - Unordered aggregation internally reuses ordered aggregation for its larger-than-memory execution logic. So larger-than-memory support for ordered aggregation become the groundwork for other aggregation modes. The high-level plan for two-stage ordered hash aggregation is: ``` AggregateExec(mode=final, ordered) --RepartitionExec(hash) ----AggregateExec(mode=partial, ordered) ``` #### Group keys are fully ordered (input ordered by `a, b`; query uses `GROUP BY a, b`) Execution must use bounded memory because only one group is active at any given time. Therefore, the implementation simply returns an execution error if a memory reservation fails. #### Group keys are partially ordered (input ordered by `a`; query uses `GROUP BY a, b`) At any given time, we must hold all groups for a particular value of `a` in memory, so it is possible to run out of memory. This is handled as follows: If the partial aggregate runs out of memory, it directly materializes the accumulated groups as aggregate states and emits them early to the final stage. The final aggregate continues aggregating until its memory budget is reached, then: 1. Sorts the aggregated batches by the group keys and writes them to disk. 2. Repeats this process until the input is exhausted. 3. Constructs a sort-preserving merge stream over all spill files. 4. Constructs an `OrderedFinalAggregateStream` using the sort-preserving merge stream as its input. The new stream's group keys are fully ordered, whereas those of the original stream were only partially ordered. It then evaluates this ordered stream to produce the final result. ### Design Tradeoffs Note that the same implementation is currently shared by two execution paths, controlled by flags: aggregation with fully ordered group keys and aggregation with partially ordered group keys. This is not an ideal pattern. However: - Fully ordered aggregation can later be split into a separate implementation with potentially more aggressive optimizations—for example, it does not require a hash table. The the current implementation only have to handle partially-ordered cases. - For now, the behavior remains consistent with the original implementation, allowing us to complete the migration sooner. ## 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. --> The core changes are in `ordered_partial_stream.rs` and `ordered_final_stream.rs`, for the above algorithm. See code comments for implementation details. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 6. 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)? --> After implementing this change, I checked the coverage using `cargo llvm-cov` and found that this feature was supported by the original implementation but was not covered by either regular tests or fuzz tests. This is a bit of a horror story. I think the old implementation is heavily multiplexed, so its overall line coverage is probably good. However, spilling for ordered aggregation follows a specific execution path that was never exercised. Therefore, this PR adds `slt` test coverage. I also think we need more fuzzing cases (TODO: open an issue). ## 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. --> -- 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]
