Skip to content

fix: Remove power(decimal, int) code path#22651

Merged
Jefffrey merged 2 commits into
apache:mainfrom
neilconway:neilc/fix-power-decimal-negative-int
Jun 4, 2026
Merged

fix: Remove power(decimal, int) code path#22651
Jefffrey merged 2 commits into
apache:mainfrom
neilconway:neilc/fix-power-decimal-negative-int

Conversation

@neilconway
Copy link
Copy Markdown
Contributor

@neilconway neilconway commented May 30, 2026

Which issue does this PR close?

Rationale for this change

power(decimal, int) attempted to compute power without loss of precision. For negative exponents, the code did the computation in Float64 and then cast the result back to decimal. Unfortunately, the previous implementation got this wrong, because decimal might not have enough precision to accurately represent the result. It seems simpler to return Float64 for the negative exponent case.

We could potentially try to return decimal only for non-negative exponents and Float64 for negative exponents, but that is complicated, and also means that the code would produce different results for literal arguments vs. columnar arguments, which I think should be avoided.

On reflection, it seems simplest to just remove the power(decimal, int) code path entirely, and have power always return Float64. This also fixes another issue in the decimal code path (#22480)

What changes are included in this PR?

Are these changes tested?

Yes.

Are there any user-facing changes?

Yes, power(decimal, ...) will now return Float64.

@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 30, 2026
@neilconway
Copy link
Copy Markdown
Contributor Author

@theirix @Jefffrey curious what you think

@theirix
Copy link
Copy Markdown
Contributor

theirix commented May 30, 2026

Thanks for the PR, @neilconway . I had similar reflections on power recently, and this change would make sense.

Since power and log can change scale drastically, fixed-size decimal types are tricky to implement correctly without having an infinite intermediate representation like BigInt or Postgres-style infinite decimal. An ideal behaviour for me is determining whether it makes sense to return decimal values based on the arguments and scale, but it essentially shifts computation to the planning phase and doesn't work well with arrays (you can have small and large scales in the same array, but the return type has to stay the same).

Nevertheless, other scale-preserving functions like round, abs, gcd, etc still benefit from a decimal implementation since they carry the same type across operations.

@neilconway
Copy link
Copy Markdown
Contributor Author

@theirix Yep, makes sense to me!

Copy link
Copy Markdown
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense; it was a nice idea to have native decimal support but the implementation complexity & edge cases made it a bit of a headache in reality 😅

@Jefffrey Jefffrey added this pull request to the merge queue Jun 4, 2026
@Jefffrey
Copy link
Copy Markdown
Contributor

Jefffrey commented Jun 4, 2026

thanks @neilconway & @theirix

Merged via the queue into apache:main with commit 7ed9a0b Jun 4, 2026
37 checks passed
@neilconway neilconway deleted the neilc/fix-power-decimal-negative-int branch June 4, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

power(decimal, -integer) is incorrect power(decimal, integer) overflow for moderate exponents

3 participants