Repository navigation
fix(array): scale decimal-to-float scalar casts across the whole scale domain - #9883
Conversation
|
This PR has been marked as stale because it has been open for 14 days with no activity. Please comment or remove the stale label if you wish to keep it active, otherwise it will be closed in 7 days |
b2fc5e5 to
355ebbc
Compare
Merging this PR will degrade performance by 13.63%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Simulation | decode_primitives[i64, (1000, 32)] |
25 µs | 38.3 µs | -34.85% |
| ⚡ | WallTime | bitpack_blocked_compress_avx2 |
7.6 µs | 6.7 µs | +14.5% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing jackylee-ch:fix/decimal-scalar-scale-factor (70439e7) with develop (9c7b782)
Footnotes
-
184 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
…e domain `DecimalScalar::cast` computed the scale factor as `10_i128.pow(self.decimal_type.scale() as u32)`. `DecimalDType` admits any `i8` scale up to `MAX_SCALE` (76), so that expression is wrong at both ends: - a scale above 38 overflows `i128`, which in release wraps to a garbage factor — `decimal(76,39)` storing 1 cast to `-4.8e-38` instead of `1e-39`; - a negative scale reaches `pow` as a wrapped `u32` exponent, because `-5i8 as u32` is 4294967291 — `decimal(5,-5)` storing 1 cast to `inf` instead of `100000.0`. A negative scale should scale the value up, not divide it. Take the factor in `f64` and branch on the sign, the way the array kernel `arrays::decimal::compute::cast::cast_to_f64` already does. The widest factor the domain needs is `10^128`, well inside `f64` range. Division is kept for non-negative scale rather than multiplying by the inverse, so every scale up to 22 — where `10^scale` is exactly representable — returns the same bits as before. Signed-off-by: jackylee-ch <qcsd2011@gmail.com>
355ebbc to
11cbcfb
Compare
The scalar cast divided by `10^scale`, while the array kernel multiplied by `10^-scale`. The two results could differ by one ulp, for example `12345 / 100` and `12345 * 0.01`. Move the conversion into `DecimalToFloatCast`, next to `DecimalToIntegerCast`, and use it in both paths. It divides for a non-negative scale, so the result is correctly rounded while `10^scale` is exact. `i256` did not implement `ToPrimitive::to_f64`. The default goes through `to_i64`, so the array kernel gave NaN for every `i256` value outside the `i64` range, and the scalar cast failed above the `i128` range. Delegate to the arrow `i256` conversion instead. Signed-off-by: Robert Kruszewski <robert@spiraldb.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes decimal-to-float casts for the full scale range and makes the scalar and array paths return the same bits.
Bugs
decimal(76,39)storing1→f64(scalar)-4.8e-381e-39decimal(5,-5)storing1→f64(scalar)inf100000.0i256value outsidei64→f64(array)NaNi256value outsidei128→f64(scalar)decimal(_,2)storing12345→f64Changes
DecimalToFloatCast(next toDecimalToIntegerCast) is the single decimal-to-f64policy. The scalar cast and the array kernelcast_to_f64both use it. It divides by10^scalefor a non-negative scale and multiplies by10^-scalefor a negative scale. Up to scale 22 the factor is exact, so the result is correctly rounded.i256implementsToPrimitive::to_f64through the arrowi256conversion. The default went throughto_i64.