You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/02/19 14:32:27 UTC
[PR] Fix decimal log precision for non-power values [datafusion]
kumarUjjawal opened a new pull request, #20433:
URL: https://github.com/apache/datafusion/pull/20433
## 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 #123` indicates that this PR will close issue #123.
-->
- Closes #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.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#discussion_r2907911166
##########
datafusion/functions/src/math/log.rs:
##########
Review Comment:
```suggestion
```
remove some debug leftover
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#issuecomment-4068097001
I am going to merge this PR tomorrow unless someone has objections!
Any improvements could be done in a follow-up!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#issuecomment-3938120832
@Jefffrey when you do get the time, can you take a look.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g merged PR #20433:
URL: https://github.com/apache/datafusion/pull/20433
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#discussion_r2922631158
##########
datafusion/functions/src/math/log.rs:
##########
@@ -109,80 +109,59 @@ fn is_valid_integer_base(base: f64) -> bool {
}
/// Calculate logarithm for Decimal32 values.
-/// For integer bases >= 2 with non-negative scale, uses the efficient u32 ilog algorithm.
-/// Otherwise falls back to f64 computation.
+/// For integer bases >= 2 with zero scale, return an exact integer log when the
+/// value is a perfect power of the base. Otherwise falls back to f64 computation.
fn log_decimal32(value: i32, scale: i8, base: f64) -> Result<f64, ArrowError> {
- if is_valid_integer_base(base)
- && scale >= 0
- && let Some(unscaled) = unscale_to_u32(value, scale)
+ if scale == 0
+ && is_valid_integer_base(base)
+ && let Ok(unscaled) = u32::try_from(value)
+ && unscaled > 0
Review Comment:
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "theirix (via GitHub)" <gi...@apache.org>.
theirix commented on code in PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#discussion_r2922857045
##########
datafusion/functions/src/math/log.rs:
##########
@@ -109,80 +109,59 @@ fn is_valid_integer_base(base: f64) -> bool {
}
/// Calculate logarithm for Decimal32 values.
-/// For integer bases >= 2 with non-negative scale, uses the efficient u32 ilog algorithm.
-/// Otherwise falls back to f64 computation.
+/// For integer bases >= 2 with zero scale, return an exact integer log when the
+/// value is a perfect power of the base. Otherwise falls back to f64 computation.
fn log_decimal32(value: i32, scale: i8, base: f64) -> Result<f64, ArrowError> {
- if is_valid_integer_base(base)
- && scale >= 0
- && let Some(unscaled) = unscale_to_u32(value, scale)
+ if scale == 0
+ && is_valid_integer_base(base)
+ && let Ok(unscaled) = u32::try_from(value)
+ && unscaled > 0
Review Comment:
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 https://github.com/apache/datafusion/pull/19874
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix decimal log precision for non-power values [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #20433:
URL: https://github.com/apache/datafusion/pull/20433#issuecomment-4072625963
Thank you, @kumarUjjawal, @Jefffrey and @theirix !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org