Skip to content

fix(array): scale decimal-to-float scalar casts across the whole scale domain - #9883

Merged
robert3005 merged 2 commits into
vortex-data:developfrom
jackylee-ch:fix/decimal-scalar-scale-factor
Oct 12, 2026
Merged

robert3005 merged 2 commits into
vortex-data:developfrom
jackylee-ch:fix/decimal-scalar-scale-factor

Conversation

@jackylee-ch

@jackylee-ch jackylee-ch commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes decimal-to-float casts for the full scale range and makes the scalar and array paths return the same bits.

Bugs

input before after
decimal(76,39) storing 1 → f64 (scalar) -4.8e-38 1e-39
decimal(5,-5) storing 1 → f64 (scalar) inf 100000.0
i256 value outside i64 → f64 (array) NaN value
i256 value outside i128 → f64 (scalar) error value
decimal(_,2) storing 12345 → f64 scalar and array differ by 1 ulp same bits

Changes

  • DecimalToFloatCast (next to DecimalToIntegerCast) is the single decimal-to-f64 policy. The scalar cast and the array kernel cast_to_f64 both use it. It divides by 10^scale for a non-negative scale and multiplies by 10^-scale for a negative scale. Up to scale 22 the factor is exact, so the result is correctly rounded.
  • i256 implements ToPrimitive::to_f64 through the arrow i256 conversion. The default went through to_i64.

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the stale This PR is stale and will be auto-closed soon label Sep 30, 2026
@jackylee-ch
jackylee-ch force-pushed the fix/decimal-scalar-scale-factor branch from b2fc5e5 to 355ebbc Compare September 30, 2026 11:39
@codspeed

codspeed Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 13.63%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ 1 benchmark measured no execution time

Nothing ran under measurement, usually because the compiler removed the code under test. This result is not comparable, so it counts as unchanged.

Preventing compiler optimizations

⚠️ 10 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 2181 untouched benchmarks
⏩ 184 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

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)

Open in CodSpeed

Footnotes

  1. 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. ↩

@github-actions github-actions Bot removed the stale This PR is stale and will be auto-closed soon label Oct 3, 2026
…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>
@robert3005
robert3005 force-pushed the fix/decimal-scalar-scale-factor branch from 355ebbc to 11cbcfb Compare October 12, 2026 10:49
@robert3005 robert3005 added the changelog/fix A bug fix label Oct 12, 2026
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>
@robert3005
robert3005 enabled auto-merge (squash) October 12, 2026 11:09
@robert3005
robert3005 merged commit 85e7acf into vortex-data:develop Oct 12, 2026
95 of 96 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants