Decimal division truncates the quotient instead of rounding it
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/arrow-rs のほかの issue
-
parquet: `ArrowWriter::try_new` panics on a schema with a Union field instead of returning an errorオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
`RunArray::try_new` rejects run ends whose null buffer has no nulls対応中かも @Surajsuthar が 2 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
apache/arrow-rs#12427 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[Variant] `variant_get` string and binary cast errors show Rust builder type names再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 61/100
メンテナーはふだん 1 日以内に返信
apache/arrow-rs の issue をすべて見る
似ている issue
-
area:ui effort:medium enhancement P7 source:agent
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
marketing
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
machine rename accepts a label already used by another machine, making --machine <label> ambiguousオープンmaintainer-needed p2 remote triaged
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
bug help wanted ui
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
userFRM/egui-charts#4605 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信