From 2d88c76af876c0686c97221f76dcbcc7f26f04c1 Mon Sep 17 00:00:00 2001 From: telleroutlook Date: Thu, 28 May 2026 07:28:58 +0800 Subject: [PATCH] fix: ln() raises error for non-positive input instead of returning NaN PostgreSQL raises "cannot take logarithm of a negative number" for ln(-1). DataFusion currently returns NaN. Add input validation to match PostgreSQL behavior, using the same pattern as sqrt(). Closes #22271 --- datafusion/functions/src/math/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/datafusion/functions/src/math/mod.rs b/datafusion/functions/src/math/mod.rs index 1754ccb43488a..d458f6fd1fd31 100644 --- a/datafusion/functions/src/math/mod.rs +++ b/datafusion/functions/src/math/mod.rs @@ -51,6 +51,14 @@ fn validate_sqrt_input(value: f64) -> Result<()> { } } +fn validate_ln_input(value: f64) -> Result<()> { + if value <= 0.0 { + exec_err!("cannot take logarithm of a negative number") + } else { + Ok(()) + } +} + // Create UDFs make_udf_function!(abs::AbsFunc, abs); make_math_unary_udf!( @@ -163,7 +171,8 @@ make_math_unary_udf!( ln, super::ln_order, super::bounds::unbounded_bounds, - super::get_ln_doc + super::get_ln_doc, + Some(super::validate_ln_input) ); make_math_unary_udf!( Log2Func,