You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "wudidapaopao (via GitHub)" <gi...@apache.org> on 2026/09/20 09:45:47 UTC
[PR] Optimize shared Float64 AVG aggregates [datafusion]
wudidapaopao opened a new pull request, #25536:
URL: https://github.com/apache/datafusion/pull/25536
## Which issue does this PR close?
- Closes #19637.
## Rationale for this change
`AVG` maintains both sum and count state. When the same aggregate node already computes a matching `SUM` or `COUNT`, those states are redundant.
## What changes are included in this PR?
- Add an aggregate decomposition hook and apply candidates only when deduplication does not increase the number of distinct aggregates.
- Decompose eligible Float64 `AVG` expressions into shared `SUM` / `COUNT` components.
- Keep the combined `AVG` accumulator when no component can be shared.
- Reuse `NameTracker` for internal aggregate name conflicts.
Decimal AVG is not included because its widened sum state and result semantics differ from regular Decimal SUM and require a separate design.
## What is the testing strategy for this PR?
- Added `avg_to_sum_count.slt` covering shared SUM/COUNT, no-share cases, NULL/empty input, grouping sets, unsupported AVG forms, Decimal, and naming conflicts.
- Added unit tests for decomposition profitability and UDAF forwarding.
- Ran `cargo fmt --all`, full workspace Clippy with `-D warnings`, and the extended workspace test suite.
Release benchmark using TPC-H SF1 with the four Q1 Decimal inputs materialized as Float64 Parquet:
- baseline median: 29.137 ms
- this PR median: 25.978 ms
- improvement: 10.84%
The no-sharing ClickBench Q32 case remained unchanged (48.931 ms vs 48.705 ms).
## Are there any user-facing changes?
Eligible Float64 AVG aggregates may use shared SUM/COUNT states. Query results and output schemas are unchanged.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4065792340
##########
datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs:
##########
@@ -187,7 +191,10 @@ fn rewrite_aggregate_non_aggregate_aggr_expr(
return Ok(Transformed::no(plan));
};
- let rewrote_aggs = rewrite_multiple_linear_aggregates(&mut aggr_expr)?;
+ let rewrote_linear = rewrite_multiple_linear_aggregates(&mut aggr_expr)?;
Review Comment:
Yes, the existing CSE already deduplicates repeated SUM or COUNT expressions. `rewrite_shared_aggregate_components` does not replace that deduplication. It determines whether decomposing AVG enables SUM or COUNT sharing without increasing the total number of distinct aggregate expressions, and only then applies the decomposition. The resulting duplicate aggregates are still deduplicated by the existing CSE path.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5877248125
marking draft per https://github.com/apache/datafusion/pull/25536#discussion_r4066052552
Then we will reviit
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5762996820
> In a local release benchmark, I materialized the four Q1 Decimal input columns as Float64 Parquet. Q1 improved from a 29.137 ms median to 25.978 ms, or `10.84%`.
Could you please provide some instructions on how to reproduce these numbers?
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4126200024
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
I filed https://github.com/apache/datafusion/issues/25847 to track the follow-up optimization for avoiding aggregate argument-array materialization in `COUNT(*)` / provably non-null `COUNT` calls.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4066052552
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
Thanks for pointing this out. I found that `COUNT(*)` materializes a full `Int64Array` for each batch. I will optimize this in a separate PR, then continue this 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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4065514376
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
Thanks, I considered using `simplify`. I think we should retain the AVG accumulator and only decompose `AVG` when its generated `SUM` or `COUNT` can be shared. If implemented in `simplify`, every AVG would be unconditionally rewritten into `SUM/COUNT`.
Benchmark: 20 million random non-null `Int64` rows.
| Scenario | SQL | Before decomposition | After decomposition | Change |
|---|---|---:|---:|---:|
| No sharing | `SELECT AVG(x) FROM t` | 9.06 ms | 10.72 ms | 18.29% slower |
| One reusable SUM | `SELECT SUM(CAST(x AS DOUBLE)), AVG(x) FROM t` | 11.72 ms | 10.79 ms | 7.95% faster |
| Three reusable SUMs | `SELECT SUM(CAST(x AS DOUBLE)), AVG(x), SUM(CAST(y AS DOUBLE)), AVG(y), SUM(CAST(z AS DOUBLE)), AVG(z) FROM t` | 32.91 ms | 27.85 ms | 15.38% faster |
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749477539
## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/25536?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `92.67139%` with `31 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 82.40%. Comparing base ([`a9579bd`](https://app.codecov.io/gh/apache/datafusion/commit/a9579bdfd5446ce53dfde8167ba80b85f90c2f2f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`749751c`](https://app.codecov.io/gh/apache/datafusion/commit/749751cc1fef47045fde55d3b7bbd97dfd84ac51?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 2 commits behind head on main.
| [Files with missing lines](https://app.codecov.io/gh/apache/datafusion/pull/25536?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [datafusion/expr/src/utils.rs](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&filepath=datafusion%2Fexpr%2Fsrc%2Futils.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9leHByL3NyYy91dGlscy5ycw==) | 88.97% | [0 Missing and 14 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...timizer/src/simplify\_expressions/simplify\_exprs.rs](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&filepath=datafusion%2Foptimizer%2Fsrc%2Fsimplify_expressions%2Fsimplify_exprs.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9vcHRpbWl6ZXIvc3JjL3NpbXBsaWZ5X2V4cHJlc3Npb25zL3NpbXBsaWZ5X2V4cHJzLnJz) | 84.78% | [0 Missing and 7 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [datafusion/expr/src/udaf.rs](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&filepath=datafusion%2Fexpr%2Fsrc%2Fudaf.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9leHByL3NyYy91ZGFmLnJz) | 91.11% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [datafusion/functions-aggregate/src/average.rs](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&filepath=datafusion%2Ffunctions-aggregate%2Fsrc%2Faverage.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9mdW5jdGlvbnMtYWdncmVnYXRlL3NyYy9hdmVyYWdlLnJz) | 91.17% | [1 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...rc/simplify\_expressions/aggregate\_decomposition.rs](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&filepath=datafusion%2Foptimizer%2Fsrc%2Fsimplify_expressions%2Faggregate_decomposition.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9vcHRpbWl6ZXIvc3JjL3NpbXBsaWZ5X2V4cHJlc3Npb25zL2FnZ3JlZ2F0ZV9kZWNvbXBvc2l0aW9uLnJz) | 98.24% | [0 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25536?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## main #25536 +/- ##
========================================
Coverage 82.39% 82.40%
========================================
Files 1138 1139 +1
Lines 434623 434914 +291
Branches 434623 434914 +291
========================================
+ Hits 358100 358371 +271
- Misses 54856 54862 +6
- Partials 21667 21681 +14
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/25536?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749175701
run benchmark tpch
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4065767785
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
> If implemented in simplify, every AVG would be unconditionally rewritten into SUM/COUNT.
Given the internal avg implementation basically has a sum and count accumulator, I am surprised at these numbers. Can you profile them and find out why there is a performance difference?
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4063654259
##########
datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs:
##########
@@ -187,7 +191,10 @@ fn rewrite_aggregate_non_aggregate_aggr_expr(
return Ok(Transformed::no(plan));
};
- let rewrote_aggs = rewrite_multiple_linear_aggregates(&mut aggr_expr)?;
+ let rewrote_linear = rewrite_multiple_linear_aggregates(&mut aggr_expr)?;
Review Comment:
I think our CSE pass already does something like this π€
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
I wonder if you considered using the existing `simplify` method:
https://docs.rs/datafusion/latest/datafusion/logical_expr/trait.AggregateUDFImpl.html#method.simplify
If you changed the avg udf to simplify to `sum/count` the existing common subexpr eliminate path probably will already avoid the recomputation.
Also it woudl allow us to delete the actual AVG accumulators (rather than having a special case like this) π€
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "adriangbot (via GitHub)" <gi...@apache.org>.
adriangbot commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749189906
π€ Benchmark running (GKE) | [trigger](https://github.com/apache/datafusion/pull/25536#issuecomment-5749175701)
**Instance:** `c4a-highmem-16` (12 vCPU / 65 GiB) | `Linux bench-c5749175701-2523-w9qgt 6.12.94+ #1 SMP Tue Aug 4 08:44:15 UTC 2026 aarch64 GNU/Linux`
<details><summary>CPU Details (lscpu)</summary>
```
Architecture: aarch64
CPU op-mode(s): 64-bit
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: ARM
Model name: Neoverse-V2
Model: 1
Thread(s) per core: 1
Core(s) per cluster: 16
Socket(s): -
Cluster(s): 1
Stepping: r0p1
BogoMIPS: 2000.00
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache: 1 MiB (16 instances)
L1i cache: 1 MiB (16 instances)
L2 cache: 32 MiB (16 instances)
L3 cache: 80 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-15
Vulnerability Gather data sampling: Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; __user pointer sanitization
Vulnerability Spectre v2: Mitigation; CSV2, BHB
Vulnerability Srbds: Not affected
Vulnerability Tsa: Not affected
Vulnerability Tsx async abort: Not affected
Vulnerability Vmscape: Not affected
```
</details>
Comparing avg-simplify-sum-count (9acbb37f837774dbada0a6e91947ecf3a4c98224) to c149764 (merge-base) [diff](https://github.com/apache/datafusion/compare/c14976481ea53d7e1806eb9dbc1cff803c6eba31..9acbb37f837774dbada0a6e91947ecf3a4c98224)
<details><summary>Run configuration</summary>
```yaml
run benchmark tpch
```
</details>
Results will be posted here when complete
---
[File an issue](https://github.com/adriangb/datafusion-benchmarking/issues) against this benchmark runner
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5766357354
> Could you please provide some instructions on how to reproduce these numbers?
I converted the four non-nullable `Decimal128(15,2)` columns used by `Q1` to `Float64` starting from the existing `TPC-H SF1` Parquet data. And then I ran the unchanged `Q1` locally on a 14-core Apple Silicon Mac.
```Python
import pyarrow as pa
import pyarrow.compute as pc
import pyarrow.parquet as pq
source = "..."
destination = "..."
table = pq.read_table(source)
for name in [
"l_quantity",
"l_extendedprice",
"l_discount",
"l_tax",
]:
index = table.schema.get_field_index(name)
table = table.set_column(
index,
pa.field(name, pa.float64(), nullable=False),
pc.cast(table.column(index), pa.float64()),
)
pq.write_table(
table,
destination,
compression="zstd",
compression_level=1,
row_group_size=131072,
)
```
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4066052552
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
Thanks for pointing this out. I found that `COUNT(*)` materializes a full `Int64Array` for each batch. I will optimize this in a separate PR, then revisit this 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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "adriangbot (via GitHub)" <gi...@apache.org>.
adriangbot commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749230504
π€ Benchmark completed (GKE) | [trigger](https://github.com/apache/datafusion/pull/25536#issuecomment-5749175701)
**Instance:** `c4a-highmem-16` (12 vCPU / 65 GiB)
Comparing avg-simplify-sum-count (9acbb37f837774dbada0a6e91947ecf3a4c98224) to c149764 (merge-base) [diff](https://github.com/apache/datafusion/compare/c14976481ea53d7e1806eb9dbc1cff803c6eba31..9acbb37f837774dbada0a6e91947ecf3a4c98224)
<details><summary>Run configuration</summary>
```yaml
run benchmark tpch
```
</details>
<details><summary>CPU Details (lscpu)</summary>
```
Architecture: aarch64
CPU op-mode(s): 64-bit
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: ARM
Model name: Neoverse-V2
Model: 1
Thread(s) per core: 1
Core(s) per cluster: 16
Socket(s): -
Cluster(s): 1
Stepping: r0p1
BogoMIPS: 2000.00
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache: 1 MiB (16 instances)
L1i cache: 1 MiB (16 instances)
L2 cache: 32 MiB (16 instances)
L3 cache: 80 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-15
Vulnerability Gather data sampling: Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; __user pointer sanitization
Vulnerability Spectre v2: Mitigation; CSV2, BHB
Vulnerability Srbds: Not affected
Vulnerability Tsa: Not affected
Vulnerability Tsx async abort: Not affected
Vulnerability Vmscape: Not affected
```
</details>
<details><summary>Details</summary>
<p>
```
Comparing HEAD and avg-simplify-sum-count
--------------------
Benchmark tpch_sf1.json
--------------------
βββββββββββββ³βββββββββββ³βββββββββββββββββββββββββ³ββββββββββββ
β Query β HEAD β avg-simplify-sum-count β Change β
β‘ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β QQuery 1 β 40.49 ms β 40.39 ms β no change β
β QQuery 2 β 18.79 ms β 18.90 ms β no change β
β QQuery 3 β 28.11 ms β 27.97 ms β no change β
β QQuery 4 β 17.37 ms β 17.14 ms β no change β
β QQuery 5 β 34.84 ms β 35.03 ms β no change β
β QQuery 6 β 16.42 ms β 16.42 ms β no change β
β QQuery 7 β 41.03 ms β 41.32 ms β no change β
β QQuery 8 β 40.90 ms β 40.81 ms β no change β
β QQuery 9 β 49.53 ms β 49.68 ms β no change β
β QQuery 10 β 41.94 ms β 41.92 ms β no change β
β QQuery 11 β 13.23 ms β 13.22 ms β no change β
β QQuery 12 β 24.65 ms β 24.36 ms β no change β
β QQuery 13 β 39.26 ms β 38.97 ms β no change β
β QQuery 14 β 24.40 ms β 24.59 ms β no change β
β QQuery 15 β 31.02 ms β 30.91 ms β no change β
β QQuery 16 β 13.67 ms β 13.74 ms β no change β
β QQuery 17 β 72.45 ms β 72.71 ms β no change β
β QQuery 18 β 60.37 ms β 59.99 ms β no change β
β QQuery 19 β 33.34 ms β 33.28 ms β no change β
β QQuery 20 β 32.20 ms β 31.76 ms β no change β
β QQuery 21 β 56.26 ms β 55.09 ms β no change β
β QQuery 22 β 13.88 ms β 13.55 ms β no change β
βββββββββββββ΄βββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββ
βββββββββββββββββββββββββββββββββββββββββ³βββββββββββ
β Benchmark Summary β β
β‘βββββββββββββββββββββββββββββββββββββββββββββββββββ©
β Total Time (HEAD) β 744.16ms β
β Total Time (avg-simplify-sum-count) β 741.76ms β
β Average Time (HEAD) β 33.83ms β
β Average Time (avg-simplify-sum-count) β 33.72ms β
β Queries Faster β 0 β
β Queries Slower β 0 β
β Queries with No Change β 22 β
β Queries with Failure β 0 β
βββββββββββββββββββββββββββββββββββββββββ΄βββββββββββ
Distribution per query (min / mean Β±stddev / max):
Comparing HEAD and avg-simplify-sum-count
--------------------
Benchmark tpch_sf1.json
--------------------
βββββββββββββ³βββββββββββββββββββββββββββββββββ³βββββββββββββββββββββββββββββββββ³ββββββββββββ
β Query β HEAD β avg-simplify-sum-count β Change β
β‘ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β QQuery 1 β 40.49 / 41.53 Β±1.21 / 43.85 ms β 40.39 / 41.68 Β±1.42 / 43.57 ms β no change β
β QQuery 2 β 18.79 / 19.05 Β±0.20 / 19.34 ms β 18.90 / 19.27 Β±0.37 / 19.89 ms β no change β
β QQuery 3 β 28.11 / 28.61 Β±0.31 / 28.97 ms β 27.97 / 28.57 Β±0.37 / 29.09 ms β no change β
β QQuery 4 β 17.37 / 18.00 Β±0.64 / 18.92 ms β 17.14 / 17.37 Β±0.14 / 17.51 ms β no change β
β QQuery 5 β 34.84 / 35.02 Β±0.12 / 35.21 ms β 35.03 / 35.69 Β±1.00 / 37.67 ms β no change β
β QQuery 6 β 16.42 / 16.61 Β±0.24 / 17.08 ms β 16.42 / 16.48 Β±0.05 / 16.54 ms β no change β
β QQuery 7 β 41.03 / 41.61 Β±0.37 / 42.16 ms β 41.32 / 42.62 Β±1.13 / 44.17 ms β no change β
β QQuery 8 β 40.90 / 42.19 Β±1.53 / 45.14 ms β 40.81 / 42.16 Β±1.07 / 43.85 ms β no change β
β QQuery 9 β 49.53 / 50.43 Β±0.73 / 51.53 ms β 49.68 / 50.15 Β±0.35 / 50.64 ms β no change β
β QQuery 10 β 41.94 / 42.37 Β±0.37 / 42.92 ms β 41.92 / 42.55 Β±0.96 / 44.45 ms β no change β
β QQuery 11 β 13.23 / 14.02 Β±0.57 / 14.97 ms β 13.22 / 13.53 Β±0.20 / 13.84 ms β no change β
β QQuery 12 β 24.65 / 24.97 Β±0.32 / 25.57 ms β 24.36 / 24.56 Β±0.23 / 24.99 ms β no change β
β QQuery 13 β 39.26 / 41.72 Β±2.47 / 46.17 ms β 38.97 / 40.58 Β±2.28 / 45.12 ms β no change β
β QQuery 14 β 24.40 / 24.79 Β±0.32 / 25.26 ms β 24.59 / 24.89 Β±0.29 / 25.35 ms β no change β
β QQuery 15 β 31.02 / 31.19 Β±0.13 / 31.37 ms β 30.91 / 31.32 Β±0.31 / 31.82 ms β no change β
β QQuery 16 β 13.67 / 13.82 Β±0.11 / 14.01 ms β 13.74 / 14.05 Β±0.36 / 14.74 ms β no change β
β QQuery 17 β 72.45 / 74.00 Β±1.55 / 76.30 ms β 72.71 / 73.72 Β±1.33 / 76.30 ms β no change β
β QQuery 18 β 60.37 / 61.90 Β±1.52 / 64.73 ms β 59.99 / 61.31 Β±1.12 / 62.50 ms β no change β
β QQuery 19 β 33.34 / 34.00 Β±0.78 / 35.43 ms β 33.28 / 33.56 Β±0.21 / 33.92 ms β no change β
β QQuery 20 β 32.20 / 32.42 Β±0.20 / 32.72 ms β 31.76 / 32.39 Β±0.67 / 33.70 ms β no change β
β QQuery 21 β 56.26 / 56.98 Β±0.66 / 58.12 ms β 55.09 / 55.53 Β±0.34 / 56.02 ms β no change β
β QQuery 22 β 13.88 / 14.32 Β±0.51 / 15.29 ms β 13.55 / 13.80 Β±0.17 / 13.95 ms β no change β
βββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄ββββββββββββ
βββββββββββββββββββββββββββββββββββββββββ³βββββββββββ
β Benchmark Summary β β
β‘βββββββββββββββββββββββββββββββββββββββββββββββββββ©
β Total Time (HEAD) β 759.57ms β
β Total Time (avg-simplify-sum-count) β 755.78ms β
β Average Time (HEAD) β 34.53ms β
β Average Time (avg-simplify-sum-count) β 34.35ms β
β Queries Faster β 0 β
β Queries Slower β 0 β
β Queries with No Change β 22 β
β Queries with Failure β 0 β
βββββββββββββββββββββββββββββββββββββββββ΄βββββββββββ
```
</p>
</details>
<details><summary>Resource Usage</summary>
**tpch β base (merge-base)**
| Metric | Value |
|--------|-------|
| Wall time | 5.0s |
| Peak memory | 1.2 GiB |
| Avg memory | 496.0 MiB |
| CPU user | 21.2s |
| CPU sys | 1.7s |
| Peak spill | 0 B |
**tpch β branch**
| Metric | Value |
|--------|-------|
| Wall time | 5.0s |
| Peak memory | 1.3 GiB |
| Avg memory | 510.3 MiB |
| CPU user | 21.1s |
| CPU sys | 1.7s |
| Peak spill | 0 B |
</details>
---
[File an issue](https://github.com/adriangb/datafusion-benchmarking/issues) against this benchmark runner
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4065514376
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
Thanks, I considered using `simplify`. I think we should retain the AVG accumulator and only decompose `AVG` when its generated `SUM` or `COUNT` can be shared. If implemented in `simplify`, every AVG would be unconditionally rewritten into `SUM/COUNT`.
On 20 million random non-null Int64 rows:
```
SELECT AVG(x) FROM t;
```
versus:
```
SELECT SUM(CAST(x AS DOUBLE))
/ CAST(COUNT(*) AS DOUBLE)
FROM t;
```
took `9.06` ms and `10.72` ms respectively, so decomposition was `18.29%` slower.
With one reusable SUM:
```
SELECT SUM(CAST(x AS DOUBLE)), AVG(x) FROM t;
```
decomposition improved `11.72` ms to `10.79` ms (`7.95%`).
With three reusable SUMs:
```
SELECT
SUM(CAST(x AS DOUBLE)), AVG(x),
SUM(CAST(y AS DOUBLE)), AVG(y),
SUM(CAST(z AS DOUBLE)), AVG(z)
FROM t;
```
decomposition improved `32.91` ms to `27.85` ms (`15.38%`).
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4065514376
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
Thanks, I considered using `simplify`. I think we should retain the AVG accumulator and only decompose `AVG` when its generated `SUM` or `COUNT` can be shared. If implemented in `simplify`, every AVG would be unconditionally rewritten into `SUM/COUNT`.
Benchmark: 20 million random non-null `Int64` rows, single-threaded.
| Scenario | SQL | Before decomposition | After decomposition | Change |
|---|---|---:|---:|---:|
| No sharing | `SELECT AVG(x) FROM t` | 9.06 ms | 10.72 ms | 18.29% slower |
| One reusable SUM | `SELECT SUM(CAST(x AS DOUBLE)), AVG(x) FROM t` | 11.72 ms | 10.79 ms | 7.95% faster |
| Three reusable SUMs | `SELECT SUM(CAST(x AS DOUBLE)), AVG(x), SUM(CAST(y AS DOUBLE)), AVG(y), SUM(CAST(z AS DOUBLE)), AVG(z) FROM t` | 32.91 ms | 27.85 ms | 15.38% faster |
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Rewrite `AVG(expr)` --> `SUM(expr) / COUNT(expr)` when components can be shared [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on code in PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#discussion_r4126200024
##########
datafusion/expr/src/udaf.rs:
##########
@@ -311,6 +312,17 @@ impl AggregateUDF {
self.inner.simplify()
}
+ /// Returns this aggregate function's candidate decomposition, if any.
+ ///
+ /// See [`AggregateUDFImpl::decompose`] for more details.
+ pub fn decompose(
Review Comment:
I filed https://github.com/apache/datafusion/issues/25847 to track the follow-up optimization for avoiding aggregate argument-array materialization in `COUNT(*)` / provably non-null `COUNT` calls.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749373336
Thanks for running the benchmark. The result shows no improvement because the standard TPC-H schema uses Decimal columns, while this PR only decomposes Float64 AVG, so the new optimization is not triggered.
In a local release benchmark, I materialized the four Q1 Decimal input columns as Float64 Parquet. Q1 improved from a 29.137 ms median to 25.978 ms, or 10.84%.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Optimize shared Float64 AVG aggregates [datafusion]
Posted by "wudidapaopao (via GitHub)" <gi...@apache.org>.
wudidapaopao commented on PR #25536:
URL: https://github.com/apache/datafusion/pull/25536#issuecomment-5749337098
Thanks for running the benchmark. The all-no-change result is expected: the standard TPC-H schema uses Decimal columns, while this PR deliberately limits decomposition to Float64 AVG, so Q1 does not take the new path. This run is useful confirmation that the Decimal path is unchanged.
For the optimized path, I materialized the four Q1 Decimal inputs as Float64 Parquet and compared standard release builds on SF1 (14 threads, 58 post-warmup samples per version):
- baseline median: 29.137 ms
- this PR median: 25.978 ms
- improvement: 10.84%
I also checked the no-sharing ClickBench Q32 case; it remained effectively unchanged (48.931 ms vs 48.705 ms). Decimal decomposition is intentionally left for a separate design because AVG uses a wider sum state and different result semantics.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org