shifluxxc opened a new pull request, #19315:
URL: https://github.com/apache/datafusion/pull/19315
## Which issue does this PR close?
- Closes #19250
## Rationale for this change
Previously, the `log` function 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 value `100`
represents `10000`). This PR restores support for negative-scale decimals in
the `log` function by implementing the logarithmic property: `log_base(value *
10^(-scale)) = log_base(value) + (-scale) * log_base(10)`.
## What changes are included in this PR?
1. **Enhanced `log_decimal128` function**:
- Added support for negative scales using the logarithmic property
- For negative scales, computes `log_base(value) + (-scale) *
log_base(10)` instead of trying to convert to unscaled value
- Added detection for negative-scale decimals in both the number and base
arguments
- Skips simplification when negative scales are detected to avoid errors
with `ScalarValue` (which doesn't support negative scales yet)
2. **Added comprehensive tests**:
- Unit tests in `log.rs` for negative-scale decimals with various bases
(2, 3, 10)
- SQL logic tests in `decimal.slt` using scientific notation (e.g.,
`1e4`, `8e1`) to create decimals with negative scales
## Are these changes tested?
Yes, this PR includes comprehensive tests:
1. Unit tests:
- `test_log_decimal128_negative_scale`: Tests array inputs with negative
scales
- `test_log_decimal128_negative_scale_base2`: Tests with base 2 and
negative scales
- `test_log_decimal128_negative_scale_scalar`: Tests scalar inputs with
negative scales
2. SQL logic tests:
- Tests for unary log with negative scales (`log(1e4)`)
- Tests for binary log with explicit base 10 (`log(10, 1e4)`)
- Tests for binary log with base 2 (`log(2.0, 8e1)`, `log(2.0, 16e1)`)
- Tests for different negative scale values (`log(5e3)`)
- Tests for array operations with negative scales
- Tests for different bases (2, 3, 10) with negative-scale decimals
All tests pass successfully.
## Are there any user-facing changes?
Yes, this is a user-facing change:
**Before**: The `log` function would fail with an error when operating on
decimal values with negative scales:
```sql
-- This would fail
SELECT log(1e4); -- Error: Negative scale is not supported
```
**After**: The `log` function now correctly handles decimal values with
negative scales:
```sql
-- This now works
SELECT log(1e4); -- Returns 4.0 (log10(10000))
SELECT log(2.0, 8e1); -- Returns ~6.32 (log2(80))
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]