You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "haohuaijin (via GitHub)" <gi...@apache.org> on 2023/11/10 14:41:16 UTC
[PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
haohuaijin opened a new pull request, #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124
## 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 #8123
## 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.
-->
In this pr
```
❯ SELECT "RegionID", SUM("AdvEngineID"), COUNT(DISTINCT "UserID") FROM '../benchmarks/data/hits.parquet' GROUP BY "RegionID" order by "RegionID" limit 10;
+----------+--------------------------------------------------+--------------------------------------------------------+
| RegionID | SUM(../benchmarks/data/hits.parquet.AdvEngineID) | COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID) |
+----------+--------------------------------------------------+--------------------------------------------------------+
| 0 | 0 | 8 |
| 1 | 147946 | 239380 |
| 2 | 441662 | 1081016 |
| 3 | 39724 | 131195 |
| 4 | 34557 | 79500 |
| 5 | 13502 | 40914 |
| 6 | 24338 | 55768 |
| 7 | 28417 | 64989 |
| 8 | 34483 | 65472 |
| 9 | 38047 | 91576 |
+----------+--------------------------------------------------+--------------------------------------------------------+
10 rows in set. Query took 0.945 seconds.
❯ explain SELECT "RegionID", SUM("AdvEngineID"), COUNT(DISTINCT "UserID") FROM '../benchmarks/data/hits.parquet' GROUP BY "RegionID" order by "RegionID" limit 10;
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type | plan |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| logical_plan | Limit: skip=0, fetch=10 |
| | Sort: ../benchmarks/data/hits.parquet.RegionID ASC NULLS LAST, fetch=10 |
| | Aggregate: groupBy=[[../benchmarks/data/hits.parquet.RegionID]], aggr=[[SUM(alias2) AS SUM(../benchmarks/data/hits.parquet.AdvEngineID), COUNT(alias1) AS COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)]] |
| | Aggregate: groupBy=[[../benchmarks/data/hits.parquet.RegionID, ../benchmarks/data/hits.parquet.UserID AS alias1]], aggr=[[SUM(CAST(../benchmarks/data/hits.parquet.AdvEngineID AS Int64)) AS alias2]] |
| | TableScan: ../benchmarks/data/hits.parquet projection=[RegionID, UserID, AdvEngineID] |
| physical_plan | GlobalLimitExec: skip=0, fetch=10 |
| | SortPreservingMergeExec: [RegionID@0 ASC NULLS LAST], fetch=10 |
| | SortExec: TopK(fetch=10), expr=[RegionID@0 ASC NULLS LAST] |
| | AggregateExec: mode=FinalPartitioned, gby=[RegionID@0 as RegionID], aggr=[SUM(../benchmarks/data/hits.parquet.AdvEngineID), COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)] |
| | CoalesceBatchesExec: target_batch_size=8192 |
| | RepartitionExec: partitioning=Hash([RegionID@0], 24), input_partitions=24 |
| | AggregateExec: mode=Partial, gby=[RegionID@0 as RegionID], aggr=[SUM(../benchmarks/data/hits.parquet.AdvEngineID), COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)] |
| | AggregateExec: mode=FinalPartitioned, gby=[RegionID@0 as RegionID, alias1@1 as alias1], aggr=[alias2] |
| | CoalesceBatchesExec: target_batch_size=8192 |
| | RepartitionExec: partitioning=Hash([RegionID@0, alias1@1], 24), input_partitions=24 |
| | AggregateExec: mode=Partial, gby=[RegionID@0 as RegionID, UserID@1 as alias1], aggr=[alias2] |
| | ParquetExec: file_groups={24 groups: [[home/hhj/datafusion/benchmarks/data/hits.parquet:0..615832352], [home/hhj/datafusion/benchmarks/data/hits.parquet:615832352..1231664704], [home/hhj/datafusion/benchmarks/data/hits.parquet:1231664704..1847497056], [home/hhj/datafusion/benchmarks/data/hits.parquet:1847497056..2463329408], [home/hhj/datafusion/benchmarks/data/hits.parquet:2463329408..3079161760], ...]}, projection=[RegionID, UserID, AdvEngineID] |
| | |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set. Query took 0.043 seconds.
```
in main 43cc870a951611e9081a462d5a8a1686e87fce9a
```
❯ SELECT "RegionID", SUM("AdvEngineID"), COUNT(DISTINCT "UserID") FROM '../benchmarks/data/hits.parquet' GROUP BY "RegionID" order by "RegionID" limit 10;
+----------+--------------------------------------------------+--------------------------------------------------------+
| RegionID | SUM(../benchmarks/data/hits.parquet.AdvEngineID) | COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID) |
+----------+--------------------------------------------------+--------------------------------------------------------+
| 0 | 0 | 8 |
| 1 | 147946 | 239380 |
| 2 | 441662 | 1081016 |
| 3 | 39724 | 131195 |
| 4 | 34557 | 79500 |
| 5 | 13502 | 40914 |
| 6 | 24338 | 55768 |
| 7 | 28417 | 64989 |
| 8 | 34483 | 65472 |
| 9 | 38047 | 91576 |
+----------+--------------------------------------------------+--------------------------------------------------------+
10 rows in set. Query took 1.343 seconds.
❯ explain SELECT "RegionID", SUM("AdvEngineID"), COUNT(DISTINCT "UserID") FROM '../benchmarks/data/hits.parquet' GROUP BY "RegionID" order by "RegionID" limit 10;
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type | plan |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| logical_plan | Limit: skip=0, fetch=10 |
| | Sort: ../benchmarks/data/hits.parquet.RegionID ASC NULLS LAST, fetch=10 |
| | Aggregate: groupBy=[[../benchmarks/data/hits.parquet.RegionID]], aggr=[[SUM(CAST(../benchmarks/data/hits.parquet.AdvEngineID AS Int64)), COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)]] |
| | TableScan: ../benchmarks/data/hits.parquet projection=[RegionID, UserID, AdvEngineID] |
| physical_plan | GlobalLimitExec: skip=0, fetch=10 |
| | SortPreservingMergeExec: [RegionID@0 ASC NULLS LAST], fetch=10 |
| | SortExec: TopK(fetch=10), expr=[RegionID@0 ASC NULLS LAST] |
| | AggregateExec: mode=FinalPartitioned, gby=[RegionID@0 as RegionID], aggr=[SUM(../benchmarks/data/hits.parquet.AdvEngineID), COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)] |
| | CoalesceBatchesExec: target_batch_size=8192 |
| | RepartitionExec: partitioning=Hash([RegionID@0], 24), input_partitions=24 |
| | AggregateExec: mode=Partial, gby=[RegionID@0 as RegionID], aggr=[SUM(../benchmarks/data/hits.parquet.AdvEngineID), COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID)] |
| | ParquetExec: file_groups={24 groups: [[home/hhj/datafusion/benchmarks/data/hits.parquet:0..615832352], [home/hhj/datafusion/benchmarks/data/hits.parquet:615832352..1231664704], [home/hhj/datafusion/benchmarks/data/hits.parquet:1231664704..1847497056], [home/hhj/datafusion/benchmarks/data/hits.parquet:1847497056..2463329408], [home/hhj/datafusion/benchmarks/data/hits.parquet:2463329408..3079161760], ...]}, projection=[RegionID, UserID, AdvEngineID] |
| | |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set. Query took 0.054 seconds.
```
## What changes are included in this PR?
add no-distinct count/sum/min/max aggregate support in `single_distinct_to_group_by` rule
<!--
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.
-->
## Are these changes tested?
yes, add soem tests
<!--
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.
-->
--
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
Re: [PR] Support no distinct aggregate sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin closed pull request #8124: Support no distinct aggregate sum/min/max in `single_distinct_to_group_by` rule
URL: https://github.com/apache/arrow-datafusion/pull/8124
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#issuecomment-1808310754
I hope to review this PR later today
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#issuecomment-1806950843
The behaviors of `sum` and `count` are different when dealing with empty inputs.
Re-writing `count` as `count`+`sum` could lead to incorrect results.
I test on this branch:
```sh
DataFusion CLI v33.0.0
❯ create table t(a int, b int);
0 rows in set. Query took 0.016 seconds.
❯ select count(distinct a), count(b) from t;
+---------------------+------------+
| COUNT(DISTINCT t.a) | COUNT(t.b) |
+---------------------+------------+
| 0 | |
+---------------------+------------+
1 row in set. Query took 0.029 seconds.
```
`COUNT(t.b)` should be 0 and not NULL.
Maybe we need a special `sum` function like `SqlSumEmptyIsZero` in calcite, or just skip the optimization for `count`.
--
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
Re: [PR] Support no distinct aggregate sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#issuecomment-1811520131
I am sorry -- I did not have a chance to get to this one. Note that this PR seems to have non trivial conflicts now (perhaps due to https://github.com/apache/arrow-datafusion/pull/8176
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#discussion_r1389490760
##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -89,6 +105,24 @@ fn contains_grouping_set(expr: &[Expr]) -> bool {
matches!(expr.first(), Some(Expr::GroupingSet(_)))
}
+/// Rewrite the aggregate function to two aggregate functions.
+fn aggregate_function_rewrite(
+ fun: &aggregate_function::AggregateFunction,
+) -> (
+ aggregate_function::AggregateFunction,
+ aggregate_function::AggregateFunction,
+) {
+ match fun {
+ Count => (Sum, Count),
Review Comment:
like two phase aggregate, for `Count` aggregate, in first phase we `Count`, and in second phase we `Sum`.
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#discussion_r1390185790
##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -58,27 +64,37 @@ impl SingleDistinctToGroupBy {
}
}
-/// Check whether all aggregate exprs are distinct on a single field.
+/// Check whether all distinct aggregate exprs are distinct on a single field.
fn is_single_distinct_agg(plan: &LogicalPlan) -> Result<bool> {
match plan {
LogicalPlan::Aggregate(Aggregate { aggr_expr, .. }) => {
let mut fields_set = HashSet::new();
- let mut distinct_count = 0;
+ let mut aggregate_count = 0;
for expr in aggr_expr {
if let Expr::AggregateFunction(AggregateFunction {
- distinct, args, ..
+ fun,
+ distinct,
+ args,
+ filter,
+ ..
}) = expr
{
- if *distinct {
- distinct_count += 1;
- }
- for e in args {
- fields_set.insert(e.canonical_name());
+ match filter {
+ Some(_) => return Ok(false),
Review Comment:
Before this pr, we also don't support filter in `single_distinct_to_group_by` rule. But we dn't check it.
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#discussion_r1390185790
##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -58,27 +64,37 @@ impl SingleDistinctToGroupBy {
}
}
-/// Check whether all aggregate exprs are distinct on a single field.
+/// Check whether all distinct aggregate exprs are distinct on a single field.
fn is_single_distinct_agg(plan: &LogicalPlan) -> Result<bool> {
match plan {
LogicalPlan::Aggregate(Aggregate { aggr_expr, .. }) => {
let mut fields_set = HashSet::new();
- let mut distinct_count = 0;
+ let mut aggregate_count = 0;
for expr in aggr_expr {
if let Expr::AggregateFunction(AggregateFunction {
- distinct, args, ..
+ fun,
+ distinct,
+ args,
+ filter,
+ ..
}) = expr
{
- if *distinct {
- distinct_count += 1;
- }
- for e in args {
- fields_set.insert(e.canonical_name());
+ match filter {
+ Some(_) => return Ok(false),
Review Comment:
Before this pr, we also don't support filter in `single_distinct_to_group_by` rule. But we forget check it.
--
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
Re: [PR] Support no distinct aggregate count/sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#issuecomment-1806987015
@jonahgao thanks for your provider this case. I didn't read the source code of calcite carefully.
I think implement `SqlSumEmptyIsZero` in datafusion is easy, we can add flag to [`Sum`](https://github.com/apache/arrow-datafusion/blob/e642cc2a94f38518d765d25c8113523aedc29198/datafusion/physical-expr/src/aggregate/sum.rs#L42-L47) and init [`SumAccumulator`](https://github.com/apache/arrow-datafusion/blob/e642cc2a94f38518d765d25c8113523aedc29198/datafusion/physical-expr/src/aggregate/sum.rs#L180-L187) to 0, and add a `AggregateFunction` about `SqlSumEmptyIsZero`. Here is a link about [`Sum0`](https://javadoc.io/static/org.apache.calcite/calcite-core/1.21.0/org/apache/calcite/sql/fun/SqlSumEmptyIsZeroAggFunction.html) in calcite.
I have no preference between the two methods, what do you think @alamb ?
--
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
Re: [PR] Support no distinct aggregate sum/min/max in `single_distinct_to_group_by` rule [arrow-datafusion]
Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on PR #8124:
URL: https://github.com/apache/arrow-datafusion/pull/8124#issuecomment-1811845757
Due to #8176, I close this pr, and then reorganize the code and submit a new pr.
--
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