`avg` of decimals truncates the result instead of rounding it

Đang mở Phù hợp với người mới
#27,688 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, sql
Lĩnh vực
data-engineering

Hướng nghiên cứu

Việc cắt cụt xảy ra trong DecimalAverager::avg tại datafusion/functions-aggregate-common/src/utils.rs, nơi tổng đã scale được chia cho số lượng bằng div_wrapping. Tái hiện bằng câu SQL trong issue để xác nhận 1.666666 so với kết quả mong đợi 1.666667, sau đó đổi phép chia để làm tròn nửa ra xa số 0. Hoàn thành khi avg(1, 2, 2) trên decimal(10,2) trả về 1.666667 và các test aggregate decimal hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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:

https://github.com/apache/datafusion/blob/dbb98dedc329a296a5153019b5ccfdf260088c3f/datafusion/functions-aggregate-common/src/utils.rs#L144-L146

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.

Ngôn ngữ chính
Rust
Star
9.6k
Fork
2.7k
Merge trung bình
3 ngày 17 giờ
Pull request đã merge (30 ngày)
318

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/datafusion

Tất cả issue của apache/datafusion

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.