Skip to content

Feat: Allow pow with negative & non-integer exponent on decimals (#19…#90

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit intoDataDog:branch-52from
benbellick:benbellick/add-negative-exponent
Mar 4, 2026
Merged

Feat: Allow pow with negative & non-integer exponent on decimals (#19…#90
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit intoDataDog:branch-52from
benbellick:benbellick/add-negative-exponent

Conversation

@benbellick
Copy link

Add a commit from upstream to allow negative exponents

…che#19369)

## Which issue does this PR close?
Closes apache#19348

## Rationale for this change
Previously, pow() on decimal types would error for negative exponents
and non-integer exponents with messages like:

- Arrow error: Arithmetic overflow: Unsupported exp value: -5
- Compute error: Cannot use non-integer exp
- This was a regression from when decimals were cast to float before
pow(). The efficient integer-based algorithm for computing power on
scaled integers cannot handle these cases.

## What changes are included in this PR?
- Modified pow_decimal_int to fallback to pow_decimal_float for negative
exponents
- Modified pow_decimal_float to use an efficient integer path for
non-negative integer exponents, otherwise fallback to f64 computation

Added pow_decimal_float_fallback function that:
- Converts the decimal to f64
- Computes powf(exp)
- Converts back to the original decimal type with proper scaling
- Added decimal_from_i128 helper to convert i128 results back to generic
decimal types (needed for Decimal256 support)
- Updated sqllogictests to expect success for negative/non-integer
exponents

## Are these changes tested?
Yes:

Unit tests for pow_decimal_float_fallback covering negative exponents,
fractional exponents, cube roots
Updated SQL logic tests in decimal.slt

## Are there any user-facing changes?
Yes. The following queries now work instead of returning errors:

```sql
-- Negative exponent
SELECT power(4::decimal(38, 5), -1);  -- Returns 0.25

-- Non-integer exponent
SELECT power(2.5, 4.2);  -- Returns 46.9

-- Square root via power
SELECT power(4::decimal, 0.5);  -- Returns 2

(cherry picked from commit 1d5d63c)
@LiaCastaneda
Copy link

LiaCastaneda commented Mar 4, 2026

( feel free to squash and merge)
Note for the upgrade: This commit will be included in the v53 release

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d bot merged commit c6731ee into DataDog:branch-52 Mar 4, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants