You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org> on 2025/12/17 14:19:01 UTC

[PR] feat: Allow log with non-integer base on decimals [datafusion]

Yuvraj-cyborg opened a new pull request, #19372:
URL: https://github.com/apache/datafusion/pull/19372

   ## Which issue does this PR close?
   Closes #19347 
   
   ## Rationale for this change
   #17023 introduced handling of decimals for log natively; previously we'd cast the input to float before performing the log. It introduced a condition against non-integer bases, as the implementation using ilog can't handle them. This is technically a regression as previously we could handle them via casting to float before doing the log.
   
   ## What changes are included in this PR?
   - Modified log_decimal128 to fallback to f64 computation for non-integer bases
   - For integer bases >= 2, continues to use the efficient ilog algorithm
   - For non-integer bases, converts the decimal value to f64 and uses standard log(base)
   - Updated error message from "non-integer base" to "non-finite base" since non-integer is now supported
   - Added sqllogictest for non-integer base case
   
   ## Are these changes tested?
   Yes:
   
   - Existing unit tests for log_decimal128 continue to pass
   - Added SQL logic test in decimal.slt for non-integer base: log(2.5, 100::decimal(38,0))
   
   ## Are there any user-facing changes?
   Yes. The following queries now work instead of returning errors:
   ```sql
   -- Non-integer base
   SELECT log(2.5, 100::decimal(38,0));  -- Returns 5.025883189464
   
   -- Fractional bases work
   SELECT log(1.5, 10::decimal(38,0));   -- Returns 5.678935662314


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#discussion_r2658742469


##########
datafusion/functions/src/math/log.rs:
##########
@@ -104,91 +102,70 @@ impl LogFunc {
     }
 }
 
-/// Binary function to calculate logarithm of Decimal32 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal32(value: i32, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
-    }
-
-    let unscaled_value = decimal32_to_i32(value, scale)?;
-    if unscaled_value > 0 {
-        let log_value: u32 = unscaled_value.ilog(base as i32);
-        Ok(log_value as f64)
-    } else {
-        // Reflect f64::log behaviour
-        Ok(f64::NAN)
-    }
+/// Checks if the base is valid for the efficient integer logarithm algorithm.
+#[inline]
+fn is_valid_integer_base(base: f64) -> bool {
+    base.trunc() == base && base >= 2.0 && base <= u32::MAX as f64
 }
 
-/// Binary function to calculate logarithm of Decimal64 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal64(value: i64, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
+/// Generic function to calculate logarithm of a decimal value using the given base.
+///
+/// For integer bases >= 2 with non-negative scale, uses the efficient integer `ilog` algorithm.
+/// For all other cases (non-integer bases, negative bases, non-finite bases),
+/// falls back to f64 computation which naturally returns NaN for invalid inputs,
+/// matching the behavior of `f64::log`.
+fn log_decimal<T>(value: T, scale: i8, base: f64) -> Result<f64, ArrowError>
+where
+    T: ToPrimitive + Copy,
+{
+    // For integer bases >= 2 and non-negative scale, try the efficient integer algorithm
+    if is_valid_integer_base(base)
+        && scale >= 0
+        && let Some(unscaled) = unscale_decimal_value(&value, scale)
+    {
+        return if unscaled > 0 {
+            Ok(unscaled.ilog(base as u128) as f64)
+        } else {
+            Ok(f64::NAN)
+        };
     }
 
-    let unscaled_value = decimal64_to_i64(value, scale)?;
-    if unscaled_value > 0 {
-        let log_value: u32 = unscaled_value.ilog(base as i64);
-        Ok(log_value as f64)
-    } else {
-        // Reflect f64::log behaviour
-        Ok(f64::NAN)
-    }
+    // Fallback to f64 computation for non-integer bases, negative scale, etc.
+    // This naturally returns NaN for invalid inputs (base <= 1, non-finite, value <= 0)
+    decimal_to_f64(&value, scale).map(|v| v.log(base))
 }
 
-/// Binary function to calculate an integer logarithm of Decimal128 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal128(value: i128, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
-    }
-
-    if value <= 0 {
-        // Reflect f64::log behaviour
-        return Ok(f64::NAN);
-    }
+/// Unscale a decimal value by dividing by 10^scale, returning the result as u128.
+/// Returns None if the value is negative or the conversion fails.
+#[inline]
+fn unscale_decimal_value<T: ToPrimitive>(value: &T, scale: i8) -> Option<u128> {
+    let value_u128 = value.to_u128()?;

Review Comment:
   I'm not sure about this choice to universally convert to u128, even for decimal32 & decimal64



##########
datafusion/functions/src/math/log.rs:
##########
@@ -1151,7 +1128,8 @@ mod tests {
     }
 
     #[test]
-    fn test_log_decimal128_wrong_base() {
+    fn test_log_decimal128_invalid_base() {

Review Comment:
   Could we move these tests to SLTs



-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3707831058

   > I must admit I am very lost now; it seems this PR has removed all the logic for computing log natively on decimal and now we manually convert to float?
   
   Apologies ! I misunderstood your feedback about u128 and overcorrected by removing the native decimal logic entirely.
   I'm fixing it rn


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3665582759

   cc: @Jefffrey @martin-g 


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3665949142

   cc: @alamb 


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3707378403

   cc: @Jefffrey 


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on code in PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#discussion_r2637185314


##########
datafusion/functions/src/math/log.rs:
##########
@@ -102,32 +102,39 @@ impl LogFunc {
     }
 }
 
-/// Binary function to calculate an integer logarithm of Decimal128 `value` using `base` base
-/// Returns error if base is invalid
 fn log_decimal128(value: i128, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
+    if !base.is_finite() {

Review Comment:
   check the recent changes 
   Using a single trait and unified the previous functions !
   
   cc: @Jefffrey 



-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Yuvraj-cyborg (via GitHub)" <gi...@apache.org>.
Yuvraj-cyborg commented on code in PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#discussion_r2659034704


##########
datafusion/functions/src/math/log.rs:
##########
@@ -104,91 +102,70 @@ impl LogFunc {
     }
 }
 
-/// Binary function to calculate logarithm of Decimal32 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal32(value: i32, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
-    }
-
-    let unscaled_value = decimal32_to_i32(value, scale)?;
-    if unscaled_value > 0 {
-        let log_value: u32 = unscaled_value.ilog(base as i32);
-        Ok(log_value as f64)
-    } else {
-        // Reflect f64::log behaviour
-        Ok(f64::NAN)
-    }
+/// Checks if the base is valid for the efficient integer logarithm algorithm.
+#[inline]
+fn is_valid_integer_base(base: f64) -> bool {
+    base.trunc() == base && base >= 2.0 && base <= u32::MAX as f64
 }
 
-/// Binary function to calculate logarithm of Decimal64 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal64(value: i64, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
+/// Generic function to calculate logarithm of a decimal value using the given base.
+///
+/// For integer bases >= 2 with non-negative scale, uses the efficient integer `ilog` algorithm.
+/// For all other cases (non-integer bases, negative bases, non-finite bases),
+/// falls back to f64 computation which naturally returns NaN for invalid inputs,
+/// matching the behavior of `f64::log`.
+fn log_decimal<T>(value: T, scale: i8, base: f64) -> Result<f64, ArrowError>
+where
+    T: ToPrimitive + Copy,
+{
+    // For integer bases >= 2 and non-negative scale, try the efficient integer algorithm
+    if is_valid_integer_base(base)
+        && scale >= 0
+        && let Some(unscaled) = unscale_decimal_value(&value, scale)
+    {
+        return if unscaled > 0 {
+            Ok(unscaled.ilog(base as u128) as f64)
+        } else {
+            Ok(f64::NAN)
+        };
     }
 
-    let unscaled_value = decimal64_to_i64(value, scale)?;
-    if unscaled_value > 0 {
-        let log_value: u32 = unscaled_value.ilog(base as i64);
-        Ok(log_value as f64)
-    } else {
-        // Reflect f64::log behaviour
-        Ok(f64::NAN)
-    }
+    // Fallback to f64 computation for non-integer bases, negative scale, etc.
+    // This naturally returns NaN for invalid inputs (base <= 1, non-finite, value <= 0)
+    decimal_to_f64(&value, scale).map(|v| v.log(base))
 }
 
-/// Binary function to calculate an integer logarithm of Decimal128 `value` using `base` base
-/// Returns error if base is invalid
-fn log_decimal128(value: i128, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
-        return Err(ArrowError::ComputeError(format!(
-            "Log cannot use non-integer base: {base}"
-        )));
-    }
-    if (base as u32) < 2 {
-        return Err(ArrowError::ComputeError(format!(
-            "Log base must be greater than 1: {base}"
-        )));
-    }
-
-    if value <= 0 {
-        // Reflect f64::log behaviour
-        return Ok(f64::NAN);
-    }
+/// Unscale a decimal value by dividing by 10^scale, returning the result as u128.
+/// Returns None if the value is negative or the conversion fails.
+#[inline]
+fn unscale_decimal_value<T: ToPrimitive>(value: &T, scale: i8) -> Option<u128> {
+    let value_u128 = value.to_u128()?;

Review Comment:
   Valid ! It's overkill



-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#discussion_r2667260391


##########
datafusion/sqllogictest/test_files/decimal.slt:
##########
@@ -896,14 +898,20 @@ select log(2, 100000000000000000000000000000000000::decimal(38,0));
 ----
 116
 
-# log(10^35) for decimal128 with another base
+# log(10^35) for decimal128 with another base (float base)
 # TODO: this should be 116.267483321058, error with native decimal log impl
 #       https://github.com/apache/datafusion/issues/18524
 query R
 select log(2.0, 100000000000000000000000000000000000::decimal(38,0));
 ----
 116
 
+# log with non-integer base now works (fallback to f64)

Review Comment:
   ```suggestion
   # log with non-integer base (fallback to f64)
   ```



##########
datafusion/sqllogictest/test_files/decimal.slt:
##########
@@ -896,14 +898,20 @@ select log(2, 100000000000000000000000000000000000::decimal(38,0));
 ----
 116
 
-# log(10^35) for decimal128 with another base
+# log(10^35) for decimal128 with another base (float base)

Review Comment:
   ```suggestion
   # log(10^35) for decimal128 with another base
   ```



-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #19372:
URL: https://github.com/apache/datafusion/pull/19372


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#discussion_r2636873495


##########
datafusion/functions/src/math/log.rs:
##########
@@ -102,32 +102,39 @@ impl LogFunc {
     }
 }
 
-/// Binary function to calculate an integer logarithm of Decimal128 `value` using `base` base
-/// Returns error if base is invalid
 fn log_decimal128(value: i128, scale: i8, base: f64) -> Result<f64, ArrowError> {
-    if !base.is_finite() || base.trunc() != base {
+    if !base.is_finite() {

Review Comment:
   I think we need to check some of these other error conditions such as base being non-finite or base being less than 1.0, as I don't think they error on float version



-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3707827969

   I must admit I am very lost now; it seems this PR has removed all the logic for computing log natively on decimal and now we manually convert to float?


-- 
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] feat: Allow log with non-integer base on decimals [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #19372:
URL: https://github.com/apache/datafusion/pull/19372#issuecomment-3724036869

   Thanks @Yuvraj-cyborg 


-- 
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