Skip to content

Conversation

@shifluxxc
Copy link
Contributor

Which issue does this PR close?

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:

-- This would fail
SELECT log(1e4);  -- Error: Negative scale is not supported

After: The log function now correctly handles decimal values with negative scales:

-- This now works
SELECT log(1e4);  -- Returns 4.0 (log10(10000))
SELECT log(2.0, 8e1);  -- Returns ~6.32 (log2(80))

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Dec 14, 2025
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.

Allow log/pow on negative scale decimals

1 participant