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 Mar 4, 2026
Conversation
…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
approved these changes
Mar 4, 2026
|
( feel free to squash and merge) |
c6731ee
into
DataDog:branch-52
30 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a commit from upstream to allow negative exponents