Decimal division truncates the quotient instead of rounding it

オープン
#12,426 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
64/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
data

調査の方向性

Start in arrow-arith/src/numeric.rs: the div kernel routes decimals through decimal_op, whose comment documents the +4 scale increment, and the digit-at-a-time helper near line 1432 is where the quotient is truncated. Read that helper and its callers to see where a round-half-away-from-zero step (plus carry into the result digits) belongs, then run cargo test -p arrow-arith numeric and add cases for 2/3 and -2/3 expecting 0.666667 and -0.666667 at scale 6. Done means existing decimal tests still pass and the truncation cases now round.

索引モデルが issue の本文から書いたものです。

説明

Describe the bug

arrow_arith::numeric::div on decimals truncates the quotient toward zero at the result scale. The result scale follows the MySQL/PostgreSQL convention of adding 4 to the dividend's scale (the comment in decimal_op says "Follow postgres and MySQL adding a fixed scale increment of 4"). The change that introduced these semantics was described as making decimal division "match that of postgres, mysql and Hive" (apache/datafusion#6832 review comment).

All three of those systems round the quotient. None of them truncate it. So the last digit of a quotient is often one unit too small in magnitude. For example, 2 / 3 is 0.666666 where MySQL, at the same scale 6, gives 0.666667.

The truncation is in the digit-at-a-time division helper, whose doc comment says it "truncates toward zero":
https://github.com/apache/arrow-rs/blob/6a3f719f1de43137f0ecc2e7de12d4a7a0b13bd5/arrow-arith/src/numeric.rs#L1432

To Reproduce

Through DataFusion (datafusion-cli 55.3.0), whose / on decimals calls this kernel:

select cast(2 as decimal(10,2)) / cast(3 as decimal(10,2));    -- 0.666666  (Decimal128(16, 6))
select cast(-2 as decimal(10,2)) / cast(3 as decimal(10,2));   -- -0.666666
select cast(2 as decimal(38,2)) / cast(3 as decimal(38,2));    -- 0.666666  (Decimal128(38, 6))

The same expressions elsewhere:

2 / 3 -2 / 3
arrow-rs (via DataFusion 55.3.0) 0.666666 -0.666666
MySQL 8.4.13 (same result scale, 6) 0.666667 -0.666667
PostgreSQL 16 0.66666666666666666667 -0.66666666666666666667
Expected behavior

The quotient is rounded to the result scale, half away from zero, as MySQL and PostgreSQL do. For example, 0.666667 and -0.666667 above.

Additional context

The truncation compounds in expressions that divide more than once, and in ratios of sums. For example, TPC-DS Q58's x / ((x + y + z) / 3) * 100 loses the last digit before the multiplication scales the error up.

The decimal AVG in DataFusion has the same issue independently (it divides the sum by the count with integer division). I'll report it there.

主要言語
Rust
スター
3.7k
フォーク
1.5k
平均マージ
2日 16時間
マージ済み PR(30日)
183

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/arrow-rs のほかの issue

apache/arrow-rs の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。