Commit 1e09a2a
authored
perf: Avoid copying when materializing output in OrderedPartialAggregateStream (#25312)
## 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 #25157
## 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.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
### Cause
See issue for the target query.
The query plan looks like
<details>
<summary>Query plan, Click to expand</summary>
```
> explain SELECT count(*) FROM (
SELECT DISTINCT d_year, brand, class, cat, manu, cnt, amt FROM src
);
+---------------+-------------------------------+
| plan_type | plan |
+---------------+-------------------------------+
| physical_plan | ┌───────────────────────────┐ |
| | │ ProjectionExec │ |
| | │ -------------------- │ |
| | │ count(*): │ |
| | │ count(Int64(1)) │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ AggregateExec │ |
| | │ -------------------- │ |
| | │ aggr: count(1) │ |
| | │ mode: Final │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ CoalescePartitionsExec │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ AggregateExec │ |
| | │ -------------------- │ |
| | │ aggr: count(1) │ |
| | │ mode: Partial │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ ProjectionExec │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ AggregateExec │ |
| | │ -------------------- │ |
| | │ group_by: │ |
| | │ d_year, brand, class, cat,│ |
| | │ manu, cnt, amt │ |
| | │ │ |
| | │ mode: │ |
| | │ FinalPartitioned │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ RepartitionExec │ |
| | │ -------------------- │ |
| | │ partition_count(in->out): │ |
| | │ 14 -> 14 │ |
| | │ │ |
| | │ partitioning_scheme: │ |
| | │ Hash([d_year@0, brand@1, │ |
| | │ class@2, cat@3, manu@4 │ |
| | │ , cnt@5, amt@6], 14) │ |
| | │ │ |
| | │ preserve_order: true │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ AggregateExec │ |
| | │ -------------------- │ |
| | │ group_by: │ |
| | │ d_year, brand, class, cat,│ |
| | │ manu, cnt, amt │ |
| | │ │ |
| | │ mode: Partial │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ DataSourceExec │ |
| | │ -------------------- │ |
| | │ files: 14 │ |
| | │ format: parquet │ |
| | └───────────────────────────┘ |
| | |
+---------------+-------------------------------+
```
</details>
It's slow due to inefficient output materializing in partial and final
aggregation
For internal mechanism, this comment explains 'why not X, and do Y
instead' -- X is the existing impl, Y is what this PR does.
-
https://github.com/2010YOUY01/arrow-datafusion/blob/0f165d0129820a3bd66641609d9efb954c0cc018/datafusion/physical-plan/src/aggregates/ordered_partial_stream.rs#L219-L248
### Fix
To fully restore the performance, we have to fix:
1. Ordered partial aggregation (this PR)
2. Ordered final aggregation (maybe a follow-up PR)
2 uses almost the same mechanism as 1, so once this PR is reviewed, we
can apply the pattern mechanically.
After this PR, the query runs in: (on an M4 Pro MacBook Pro)
```
-- Still some gap due to final aggregation is not fixed yet
Current main: 3.5s
PR: 0.45s
DataFusion 54.0: 0.37s
```
## 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. Refactor the ordered-partial aggregation, so it's easier to implement
incremental outputting with slicing
2. Implement the output materializing strategy mentioned above
Note to read this PR, I suggest directly reading the new impl start from
the entry point of state machine (`into_stream()`), instead of the diff,
due to a large refactor.
This refactor is necessary because its easier to implement this feature
with a different state machine pattern.
## What is the testing strategy for this PR?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
4. Serve as another way to document the expected behavior of the code
Briefly describe how this PR is tested, and point to the specific tests
you added. For example: 'This new feature is covered by the
`sqllogictest` cases added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change
is already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
For correctness, existing tests have covered it.
To prevent similar perf regression, we can do
- #25310
## 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.
-->1 parent 993b3f3 commit 1e09a2a
3 files changed
Lines changed: 271 additions & 207 deletions
File tree
- datafusion/physical-plan/src/aggregates
- aggregate_hash_table
Lines changed: 25 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | 365 | | |
384 | 366 | | |
385 | 367 | | |
| |||
455 | 437 | | |
456 | 438 | | |
457 | 439 | | |
458 | | - | |
459 | | - | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
460 | 450 | | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
461 | 459 | | |
462 | 460 | | |
463 | 461 | | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
472 | 468 | | |
473 | 469 | | |
474 | 470 | | |
| |||
484 | 480 | | |
485 | 481 | | |
486 | 482 | | |
487 | | - | |
| 483 | + | |
488 | 484 | | |
489 | 485 | | |
Lines changed: 12 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
107 | 95 | | |
108 | 96 | | |
109 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
110 | 105 | | |
111 | 106 | | |
112 | 107 | | |
| 108 | + | |
113 | 109 | | |
114 | 110 | | |
0 commit comments