`avg` of decimals truncates the result instead of rounding it
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- data-engineering
Research direction
The truncation happens in DecimalAverager::avg in datafusion/functions-aggregate-common/src/utils.rs, where the scaled sum is divided by the count with div_wrapping. Reproduce with the SQL in the issue to confirm 1.666666 against the expected 1.666667, then change the division to round half away from zero. Done when avg(1, 2, 2) on decimal(10,2) returns 1.666667 and existing decimal aggregate tests still pass.
Written by the indexing model from the issue text.
Description
Describe the bug
avg over a decimal column returns Decimal(p + 4, s + 4) (MySQL's convention), but it computes the value by truncating, not rounding. DecimalAverager::avg scales the sum and then divides by the count with div_wrapping, which is integer division and truncates toward zero:
So the last digit of an average is often one unit too small in magnitude.
To Reproduce
datafusion-cli 55.3.0:
select avg(x), arrow_typeof(avg(x))
from (values (cast(1 as decimal(10,2))), (cast(2 as decimal(10,2))), (cast(2 as decimal(10,2)))) as v(x);
-- 1.666666, Decimal128(14, 6)
The same average elsewhere:
| avg(1, 2, 2) | |
|---|---|
| DataFusion 55.3.0 | 1.666666 |
| MySQL 8.4.13 (same result scale, 6) | 1.666667 |
| PostgreSQL 16 | 1.6666666666666667 |
Expected behavior
The average is rounded to the result scale, half away from zero, as MySQL (whose s + 4 scale this follows) and PostgreSQL do: 1.666667 here.
Additional context
Decimal / has the same issue in the arrow-rs division kernel, reported at https://github.com/apache/arrow-rs/issues/12426. This one is separate: the average is computed here, not through that kernel.
- Dominant language
- Rust
- Stars
- 9.6k
- Forks
- 2.7k
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 318
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/datafusion
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/datafusion#27687 ·
Maintainers usually reply within 1 day
-
`verify-release-candidate.sh` version check doesn't catch `-dev` versions and is skipped on macOSPossibly taken @vinycalves claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/datafusion#27563 · 2 comments ·
Maintainers usually reply within 1 day
-
`MoveTowardsLeafNodes` placement implies speculative evaluation, but the docs don't say soPossibly taken @Inf1mum claimed this 6 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
apache/datafusion#27487 · 3 comments ·
Maintainers usually reply within 1 day
-
`lpad` and `rpad` with a literal length above `i32::MAX` error on a NULL string or fillPossibly taken @TechWizard9999 claimed this 6 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
apache/datafusion#27482 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Window constructors panic when the expression list is emptyPossibly taken @abp002 claimed this 9 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/datafusion#27315 · 3 comments · 1 assignee ·
Maintainers usually reply within 1 day
All issues in apache/datafusion
Similar issues
-
ai_p1 comp-sql-syntax
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
ClickHouse/ClickHouse#133888 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
horizon-rust/horizon#20542 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
LadybirdBrowser/ladybird#15521 ·
Maintainers usually reply within 1 day
-
macOS: the Save Diagnostics panel goes behind the editor after an app switch and can't be reachedOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
getopenscreen/openscreen#1667 ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
erkamyaman/rust-anything#371 ·
Maintainers usually reply within 1 day