You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 46b0b2b
Browse filesBrowse the repository at this point in the historyBrowse files
Adds shared infrastructure for decimal arithmetic and native Add/Sub
kernels.
### Operand alignment
The execution kernel requires both operands to have the same precision
and scale (nullability may differ). Addition and subtraction can then
operate directly and exactly on the unscaled integers because both
values use the same power-of-ten factor.
Vortex's optional coercion pass can align operands with different
decimal dtypes before execution. Query-engine integrations may instead
insert their own planner casts. If neither layer aligns the operands,
the kernel rejects them rather than performing an implicit per-kernel
rescale.
### Result precision
Adding or subtracting two `p`-digit integers can require one additional
carry digit. This PR follows the Arrow/Hive result-type rule for already
aligned operands, capped at Vortex's maximum decimal precision:
```text
decimal(p, s) +/- decimal(p, s) -> decimal(min(p + 1, 76), s)
```
The scale remains unchanged because the operation is performed on
integers at the same scale. For example:
```text
decimal(2, 0): 60 + 60 = 120 -> decimal(3, 0)
decimal(5, 2): 123.45 + 0.55 = 124.00 -> decimal(6, 2)
```
Widening the logical precision can also widen the native storage used
for the result. The kernel executes at the smallest native width capable
of representing the result dtype, including the important `decimal(18,
s) -> decimal(19, s)` (`i64 -> i128`) and `decimal(38, s) -> decimal(39,
s)` (`i128 -> i256`) transitions. Inputs may already use a wider
physical representation; they are converted into the selected working
width before arithmetic.
At precision 76 the result dtype cannot widen to precision 77, so it
stays at precision 76 and the kernel explicitly checks the mathematical
result against the precision-76 bound.
### Nulls, constants, and errors
- Output nullability is the union of the input nullabilities, while a
lane is valid only when both input lanes are valid.
- Validity is combined and materialized once. Arithmetic runs only on
valid lanes, so arbitrary values underneath null lanes cannot trigger an
overflow error.
- Any native-width overflow or result-precision overflow on a valid lane
returns an error for the operation.
- Constant and all-null operands are handled directly instead of first
materializing full decimal buffers. Constant folding is preserved, and
empty Add/Sub inputs produce a correctly widened empty dtype without
evaluating a constant value.
- Decimal Mul/Div remain unsupported because they require separate
result-type and rescaling rules.
### Implementation
- Splits numeric execution into
`numeric/{mod,primitive,decimal,tests}.rs`, mirroring the compare
implementation.
- Canonicalizes encoded decimal operands through
`execute::<DecimalArray>`, while extracting constant and null operands
directly.
- Uses a checked lane loop at the result's working width, followed by an
explicit precision-bound check.
- Shares `widened_buffer` from `arrays/decimal` between comparison and
numeric execution.
### Comparative performance
Measured over arrays with ~60k elements, using same result width in
Vortex and Arrow.
| Case | Vortex Add | Arrow Add | Vortex Sub | Arrow Sub | Vortex
speedup |
| --- | ---: | ---: | ---: | ---: | ---: |
| `i32`, non-null | 59.87 us | 100.4 us | 59.74 us | 100.4 us | 1.68x |
| `i64`, non-null | 61.66 us | 101.1 us | 62.29 us | 101.2 us |
1.62-1.64x |
| `i128`, non-null | 68.91 us | 145.4 us | 69.54 us | 145.6 us |
2.09-2.11x |
| `i256`, nullable | 175.4 us | 564.7 us | 163.2 us | 561.5 us |
3.22-3.44x |
---------
Signed-off-by: Matt Katz <[email protected]>
0 commit comments