Decimal division truncates the quotient instead of rounding it

Aperta
#12,426 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
64/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Rust
Stelle
3.7k
Fork
1.5k
Merge medio
2g 16h
PR unite (30g)
183

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/arrow-rs

Tutte le issue di apache/arrow-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.