Skip to content

fix: preserve operand width in DecimalValue checked arithmetic#7380

Open
abnobdoss wants to merge 1 commit intovortex-data:developfrom
abnobdoss:fix/decimal-checked-add-no-upcast
Open

fix: preserve operand width in DecimalValue checked arithmetic#7380
abnobdoss wants to merge 1 commit intovortex-data:developfrom
abnobdoss:fix/decimal-checked-add-no-upcast

Conversation

@abnobdoss
Copy link
Copy Markdown

Summary

Closes: #7022

DecimalValue::checked_add/sub/mul/div unconditionally upcast both operands to i256 and returned DecimalValue::I256, producing unnecessarily wide scalars even when both inputs were narrow (e.g. I32 + I32 → I256).

Operate at max(self, other) width instead, matching the pattern in aggregate_fn/fns/sum/decimal.rs. The old checked_binary_op helper was replaced with a local macro so each op dispatches with its own trait.

AI disclosure: Analysis, implementation, and tests were done with Claude Code under my direction and review.

API Changes

No public API signature changes (verified via ./scripts/public-api.sh).

Overflow is now caught at the target width rather than silently widening (e.g. I8(i8::MAX) + I8(1) now returns None instead of I256(128)). This felt like the most faithful reading of the issue, but I'd appreciate a sanity check that returning None on target-width overflow is the desired semantics rather than, say, promoting to the next wider variant. No in-tree caller depends on the old behavior: sum/mod.rs pre-widens the accumulator by +10 precision, decimal/scalar.rs::checked_binary_numeric requires both operands to share the same width, and sum/constant.rs uses I128 as the multiplier.

Testing

  • Updated test_decimal_value_checked_* to assert the correct variant.
  • Added tests for variant preservation, mixed-width promotion, and overflow at the target width (including i8::MIN / -1).
  • All existing tests pass (212 decimal, 14 sum).

…thmetic (vortex-data#7022)

Signed-off-by: Abanoub Doss <abanoub.doss@gmail.com>
@joseph-isaacs
Copy link
Copy Markdown
Contributor

I256 is not the maximum of the inputs. I would accept a PR doing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DecimalValue checked_add should not upcast to i256 unconditionally

2 participants