Decimal division truncates the quotient instead of rounding it
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
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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di apache/arrow-rs
-
parquet: `ArrowWriter::try_new` panics on a schema with a Union field instead of returning an errorAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
`RunArray::try_new` rejects run ends whose null buffer has no nullsForse già presa @Surajsuthar l’ha presa 2 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
apache/arrow-rs#12427 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
[Variant] `variant_get` string and binary cast errors show Rust builder type namesForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 61/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di apache/arrow-rs
Issue simili
-
area:ui effort:medium enhancement P7 source:agent
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
marketing
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
machine rename accepts a label already used by another machine, making --machine <label> ambiguousApertamaintainer-needed p2 remote triaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
bug help wanted ui
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
userFRM/egui-charts#4605 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno