Fix regression for negative-scale decimals in log #19315
+242
−6
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.
Which issue does this PR close?
Rationale for this change
Previously, the
logfunction would fail when operating on decimal values with negative scales.Negative scales in decimals represent values where the scale indicates padding zeros to the right (e.g.,
Decimal128(38, -2)with value100represents10000). This PR restores support for negative-scale decimals in thelogfunction by implementing the logarithmic property:log_base(value * 10^(-scale)) = log_base(value) + (-scale) * log_base(10).What changes are included in this PR?
Enhanced
log_decimal128function:log_base(value) + (-scale) * log_base(10)instead of trying to convert to unscaled valueScalarValue(which doesn't support negative scales yet)Added comprehensive tests:
log.rsfor negative-scale decimals with various bases (2, 3, 10)decimal.sltusing scientific notation (e.g.,1e4,8e1) to create decimals with negative scalesAre these changes tested?
Yes, this PR includes comprehensive tests:
Unit tests:
test_log_decimal128_negative_scale: Tests array inputs with negative scalestest_log_decimal128_negative_scale_base2: Tests with base 2 and negative scalestest_log_decimal128_negative_scale_scalar: Tests scalar inputs with negative scalesSQL logic tests:
log(1e4))log(10, 1e4))log(2.0, 8e1),log(2.0, 16e1))log(5e3))All tests pass successfully.
Are there any user-facing changes?
Yes, this is a user-facing change:
Before: The
logfunction would fail with an error when operating on decimal values with negative scales:After: The
logfunction now correctly handles decimal values with negative scales: