Skip to content

Fix decimal log precision for non-power values#20433

Merged
martin-g merged 3 commits intoapache:mainfrom
kumarUjjawal:fix/decimal_log
Mar 17, 2026
Merged

Fix decimal log precision for non-power values#20433
martin-g merged 3 commits intoapache:mainfrom
kumarUjjawal:fix/decimal_log

Conversation

@kumarUjjawal
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The decimal log implementation used the integer ilog path whenever the base was an integer, which floors results for non‑power values. That produced incorrect outputs such as log(2, 10^35) returning 116 instead of 116.267...

What changes are included in this PR?

  • Fix decimal log to use the integer fast path only for exact powers, preserving fractional results for non-power inputs.
  • Update unit tests and decimal sqllogictest expectations to match the corrected behav

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Feb 19, 2026
@kumarUjjawal
Copy link
Copy Markdown
Contributor Author

@Jefffrey when you do get the time, can you take a look.

@kumarUjjawal
Copy link
Copy Markdown
Contributor Author

@martin-g if I could borrow your time here.

Comment on lines +115 to +118
if scale == 0
&& is_valid_integer_base(base)
&& let Ok(unscaled) = u32::try_from(value)
&& unscaled > 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel we'll need to reconsider this integer log path now that it's reduced to needing all these requirements:

  • 0 scale
  • integer base
  • can unscale

Just for a more accurate computation 🤔

cc @theirix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's true. I would start with more test cases to express our expectations on int/float semantics, like stated in the PR description.

For unscaling, I believe it could be improved in general, there are some edge cases. One attempt is here #19874

@martin-g
Copy link
Copy Markdown
Member

I am going to merge this PR tomorrow unless someone has objections!
Any improvements could be done in a follow-up!

@martin-g martin-g added this pull request to the merge queue Mar 17, 2026
Merged via the queue into apache:main with commit 4c96125 Mar 17, 2026
30 checks passed
@martin-g
Copy link
Copy Markdown
Member

Thank you, @kumarUjjawal, @Jefffrey and @theirix !

de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18524

## Rationale for this change

The decimal log implementation used the integer ilog path whenever the
base was an integer, which floors results for non‑power values. That
produced incorrect outputs such as log(2, 10^35) returning 116 instead
of 116.267...


<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

- Fix decimal log to use the integer fast path only for exact powers,
preserving fractional results for non-power inputs.
- Update unit tests and decimal sqllogictest expectations to match the
corrected behav

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

Yes

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

No

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decimal128 implementation of log loses precision

4 participants