You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "mslapek (via GitHub)" <gi...@apache.org> on 2023/03/26 13:24:30 UTC
[GitHub] [arrow-datafusion] mslapek opened a new pull request, #5745: Add PushDownAggregate optimization
mslapek opened a new pull request, #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745
# 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 #4772
# 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.
-->
See the issue.
# 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.
-->
Added a new optimization `PushDownAggregate`
Added it to the default list in `optimizer.rs`.
# 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
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)?
-->
Unit tests inside `push_down_aggregate.rs`.
Added tests to `pg_compat_union.slt` to ensure, that computation results are still correct.
# Are there any user-facing changes?
No.
<!--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mingmwang commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1486341433
> @mingmwang You gave a great idea - it should be **a physical plan optimization!** 💡
>
> The optimization should push down partial aggregate though union:
>
> ```
> ...
> | logical_plan | Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]] |
> | | Union |
> | | TableScan: my_table projection=[c1] |
> | | Projection: my_table2.c1 AS my_table.c1 |
> | | TableScan: my_table2 projection=[c1]
> ...
> | physical_plan | AggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))] |
> | | CoalescePartitionsExec | |
> | | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=2 |
> | | UnionExec
> | | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))] |
> | | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple.csv]]}, has_header=true, limit=None, projection=[c1] |
> | | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))]
> | | ProjectionExec: expr=[c1@0 as my_table.c1] |
> | | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple2.csv]]}, has_header=true, limit=None, projection=[c1] |
> | | |
> +------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> ```
>
> Optimization in the logical plan gave unnecessary doubled final-partial aggregate pair.
>
> @mingmwang What do you think?
I think even during the physical planning phase, push down the partial aggregate though repartition + union might not give performance benefit, because it might reduce the parallism of the partial aggregate.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mingmwang commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1484802514
No, I'm afraid this optimization is useless. Could you please check the physical plan ?
I think in the physical plan phase, there was already the partial agg and final agg and actually the partial agg is already pushed down.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mslapek commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1489737150
@mingmwang Thank you for a thorough explanation! 🙂 It makes a perfect sense.
Yeah. Union is only a concatenated list of partitions... itself it does no performance impact - that's what I've forgotten! 😬
So the only performance issue is: when `RepartitionExec(4)` can be avoided? When we have in all branches equally-sized partitions, and their amount allows for a sensible parallelization - then the reshuffle could be avoided.
But:
1. it is not related in a specific way to the union, it's a general issue
2. it would require data from some Metastore
---
To sum up, it looks like this PR should be closed without merge. 😭
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mslapek commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1487131159
My intuitions says, that 2 partial aggregates in **PARALLEL** branches would be more susceptible to parallelization - than one big partial aggregate. 🧐
Could you clarify, why that's not the case?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on pull request #5745: Add PushDownAggregate optimization
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1494270599
Closing per comments above
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mslapek commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1486282656
@mingmwang You gave a great idea - it should be **a physical plan optimization!** 💡
The optimization should push down partial aggregate though union:
```
...
| logical_plan | Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]] |
| | Union |
| | TableScan: my_table projection=[c1] |
| | Projection: my_table2.c1 AS my_table.c1 |
| | TableScan: my_table2 projection=[c1]
...
| physical_plan | AggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))] |
| | CoalescePartitionsExec | |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=2 |
| | UnionExec
| | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))] |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple.csv]]}, has_header=true, limit=None, projection=[c1] |
| | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))]
| | ProjectionExec: expr=[c1@0 as my_table.c1] |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple2.csv]]}, has_header=true, limit=None, projection=[c1] |
| | |
+------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```
Optimization in the logical plan gave unnecessary doubled final-partial aggregate pair.
@mingmwang What do you think?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mslapek commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1485376378
@mingmwang Thank you for your observation. Let's make an experiment...
## Experiment
Copied `arrow-datafusion/datafusion/core/tests/data/aggregate_simple.csv` to `aggregate_simple.csv`.
Script:
```rust
use datafusion::prelude::*;
#[tokio::main]
async fn main() -> datafusion::error::Result<()> {
let ctx = SessionContext::new();
ctx.register_csv(
"my_table",
"datafusion/core/tests/data/aggregate_simple.csv",
CsvReadOptions::default(),
).await?;
ctx.register_csv(
"my_table2",
"datafusion/core/tests/data/aggregate_simple2.csv",
CsvReadOptions::default(),
).await?;
let sql = "SELECT COUNT(*) FROM (SELECT * from my_table UNION ALL SELECT * from my_table2)";
let df = ctx.sql(sql).await?;
df.clone().explain(true, false)?.show().await?;
df.show().await?;
Ok(())
}
```
Optimized logical and physical plans are below.
## Without `push_down_aggregate`
*Explain after optimizations*
```
...
| logical_plan | Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]] |
| | Union |
| | TableScan: my_table projection=[c1] |
| | Projection: my_table2.c1 AS my_table.c1 |
| | TableScan: my_table2 projection=[c1]
...
| physical_plan | AggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=2 |
| | UnionExec |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple.csv]]}, has_header=true, limit=None, projection=[c1] |
| | ProjectionExec: expr=[c1@0 as my_table.c1] |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple2.csv]]}, has_header=true, limit=None, projection=[c1] |
| | |
+------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```
These CSV files will be naively mixed together with `RoundRobinBatch` - only to count the rows at the end! 😧
> actually the partial agg is already pushed down.
❗️ No, it isn't...
## With `push_down_aggregate`
*Explain after optimizations*
```
...
| logical_plan | Projection: SUM(COUNT(aggr_1)) AS COUNT(UInt8(1)) |
| | Aggregate: groupBy=[[]], aggr=[[SUM(COUNT(aggr_1))]] |
| | Union |
| | Aggregate: groupBy=[[]], aggr=[[COUNT(aggr_1)]] |
| | Projection: UInt8(1) AS aggr_1 |
| | TableScan: my_table projection=[c1] |
| | Aggregate: groupBy=[[]], aggr=[[COUNT(aggr_1)]] |
| | Projection: UInt8(1) AS aggr_1 |
| | TableScan: my_table2 projection=[c1]
...
| physical_plan | ProjectionExec: expr=[SUM(COUNT(aggr_1))@0 as COUNT(UInt8(1))] |
| | AggregateExec: mode=Final, gby=[], aggr=[SUM(COUNT(aggr_1))] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[SUM(COUNT(aggr_1))] |
| | UnionExec |
| | AggregateExec: mode=Final, gby=[], aggr=[COUNT(aggr_1)] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(aggr_1)] |
| | ProjectionExec: expr=[1 as aggr_1] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1 |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple.csv]]}, has_header=true, limit=None, projection=[c1] |
| | AggregateExec: mode=Final, gby=[], aggr=[COUNT(aggr_1)] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[COUNT(aggr_1)] |
| | ProjectionExec: expr=[1 as aggr_1] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1 |
| | CsvExec: files={1 group: [[/home/hello/repo/arrow-datafusion/datafusion/core/tests/data/aggregate_simple2.csv]]}, has_header=true, limit=None, projection=[c1] |
|
```
There is no mixing of the file contents.
To `AggregateExec: mode=Partial, gby=[], aggr=[SUM(COUNT(aggr_1))]` are going only two rows.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mingmwang commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1488280207
> My intuitions says, that 2 partial aggregates in **PARALLEL** branches would be more susceptible to parallelization - than one big partial aggregate. 🧐
>
> Could you clarify, why that's not the case?
This is related to how UnionExec is actually executed:
For example, if we have a plan fragment like below, if each CsvExec's parallelization is 1, then the AggregateExec's parallelization is 2
```
AggregateExec(2)
UnionExec(2)
CsvExec(1)
CsvExec(1)
```
If we push down the AggregateExec through UnionExec like below, the total parallelization of AggregateExec is still 2.
This is what I mean the pushdown is useless.
```
UnionExec(2)
AggregateExec(1)
CsvExec(1)
AggregateExec(1)
CsvExec(1)
```
Now we take the RepartitionExec into consideration, if the RepartitionExec change the parallelization to 4, the parallelization of AggregateExec is 4
```
AggregateExec(4)
RepartitionExec(4)
UnionExec(2)
CsvExec(1)
CsvExec(1)
```
If we push down the AggregateExec through RepartitionExec + UnionExec , it will reduce the parallelization of AggregateExec to 2 and will not get performance benefits
```
RepartitionExec(4)
UnionExec(2)
AggregateExec(1)
CsvExec(1)
AggregateExec(1)
CsvExec(1)
```
Hope this explains.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mingmwang commented on pull request #5745: Add PushDownAggregate optimization
Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on PR #5745:
URL: https://github.com/apache/arrow-datafusion/pull/5745#issuecomment-1493680430
I think you can close this PR.
But you can file another ticket to track the `RepartitionExec` enhancement.
It is better if you can give some real SQL examples with the physical plan fragments generated by the DataFusion optimizer which can prove the `RepartitionExec` is unnecessary.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb closed pull request #5745: Add PushDownAggregate optimization
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed pull request #5745: Add PushDownAggregate optimization
URL: https://github.com/apache/arrow-datafusion/pull/5745
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org