Opt-in overflow tracking for decimal fixed-point arithmetic#22356
Opt-in overflow tracking for decimal fixed-point arithmetic#22356Avinash-Raj wants to merge 4 commits into
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
|
I would favor adding new operators/functions rather than a compile-time flag — we want to be able to ship one build of cuDF that works universally. |
|
@bdice I'd imagine this is not limited to scalar functions like binaryops and such. We'd also need to detect overflow in aggregations like groupby and reduce. And those would require overloading the existing +,* operators. What's your opinion on adding a new type maybe called safe_decimal. Spark should also have a concept of arithmetic exception for overflows. How do they support this with libcudf decimal? |
Inline with this thinking, we could extend cudf's and use |
|
This is related to ANSI support that I've recently filed: #21676 |
|
Ok, replaced the CMake |
|
You might want to put overflow tracking into the floating <--> decimal conversion code as well (include/cudf/fixed_point/detail/floating_conversion.hpp). E.g. there are the guarded_left_shift() and guarded_right_shift() functions, and there are potential overflows in convert_floating_to_integral_shifting(), shift_to_decimal_pospow(), and shift_to_decimal_negpow(). All potential overflow sites are mentioned explicitly in the code comments in these functions. |
|
Could this be done without modifying the fixed-point class? The operator overloads are not really required for this to work since the dispatcher logic creates specialized code paths automatically. And somehow the overflow-awareness is communicated through to the appropriate operation/function and would generally require a special code path either way. It seems the operators could be just free functions and not member functions or member operators. The return type would include an overflow flag along with the result. Similar to how the https://github.com/rapidsai/cudf/blob/main/cpp/include/cudf/fixed_point/detail/floating_conversion.hpp is a separate file handling specific operations for fixed-point. The new free functions would be called as part of the specialized overflow-aware aggregation types for reduce, groupby, etc. |
Summary
Introduces an opt-in path for detecting overflow in decimal fixed-point arithmetic, at two layers:
Value-type level – new
numeric::decimal32_safe/decimal64_safe/decimal128_safealiases backed by a newfixed_point<Rep, Rad, overflow_tracking::on>instantiation. These carry a sticky per-valueboolthat is set whenever a constructor, arithmetic operator,rescaled(), or float→decimal conversion would overflow the underlying integer representation. The flag propagates through+,-,*,%andrescaled(), so any expression built on the value-level operators (binaryop transforms, scans, reductionsexpressed through value ops) carries it for free.
Column / binaryop level – new public API
cudf::binary_operation_safe(..., rmm::device_scalar<uint32_t>& overflow_flag, ...)for the seven base-10 decimal arithmetic operators (ADD, SUB, MUL, DIV, MOD, PMOD, PYMOD). It updates a single device-side flag viaatomicMaxwhenever any active (non-null) row overflows during arithmetic or rescale to the output scale. Designed for callers (e.g. velox-cudf) that need a one-shot “did any row overflow?” signal without per-row storage.Motivation
Existing
decimal32/decimal64/decimal128silently wrap on overflow. Downstream engines that need stricter semantics today have to either implement their own checked arithmetic or rebuild libcudf. This PR keeps the default behavior unchanged but lets opt-in callers ask the same libcudf build for overflow-aware decimals.