andygrove commented on code in PR #334:
URL: https://github.com/apache/datafusion-spark/pull/334#discussion_r4603022578


##########
LICENSE.txt:
##########
@@ -200,3 +200,15 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+--------------------------------------------------------------------------------
+
+This product includes code translated from fdlibm 5.3 (Freely Distributable
+LIBM), in crates/spark/src/function/math/fdlibm.rs, under the following terms:
+
+  Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
+
+  Developed at SunSoft, a Sun Microsystems, Inc. business.
+  Permission to use, copy, modify, and distribute this
+  software is freely granted, provided that this notice
+  is preserved.

Review Comment:
   Thanks for digging into this. I'll ask on legal-discuss@ and link the thread 
here once there's an answer.



##########
crates/spark/src/function/math/log.rs:
##########
@@ -0,0 +1,317 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible logarithm functions: `ln`, `log10`, `log2`, `log1p` and 
`log`.
+//!
+//! Spark follows Hive here: an input at or below the function's asymptote
+//! (0 for `ln`, `log10`, `log2` and both arguments of `log`, -1 for `log1p`)
+//! returns NULL instead of the NaN or -Infinity that IEEE 754 gives. NaN is
+//! not below the asymptote, so a NaN input returns NaN.
+//!
+//! DataFusion's built-ins return -Infinity or NaN for those inputs, and its
+//! one-argument `log` is base 10 where Spark's is the natural logarithm.
+//!
+//! Spark computes the logarithms with `java.lang.StrictMath`, so these use the
+//! fdlibm translation in [`super::fdlibm`] to return the same bits. Rust's
+//! `f64::ln` differs in the last digit for some inputs.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, exec_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    TypeSignature, Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+use datafusion_macros::user_doc;
+
+use super::fdlibm;
+
+/// Applies `f` to every value of a Float64 array, returning NULL where the
+/// value is at or below `asymptote`, as Spark's `UnaryLogExpression` does.
+fn unary_log(
+    name: &str,
+    args: &[ArrayRef],
+    asymptote: f64,
+    f: impl Fn(f64) -> f64,
+) -> Result<ArrayRef> {
+    let [arg] = take_function_args(name, args)?;
+    let result: Float64Array = arg
+        .as_primitive::<Float64Type>()

Review Comment:
   Done in 3cbe805. Both `unary_log` and `spark_log` now return 
`unsupported_data_type_exec_err` for a non-Float64 array, and 
`non_float64_arguments_error` covers each path.



##########
crates/spark/src/function/math/log.rs:
##########
@@ -0,0 +1,317 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible logarithm functions: `ln`, `log10`, `log2`, `log1p` and 
`log`.
+//!
+//! Spark follows Hive here: an input at or below the function's asymptote
+//! (0 for `ln`, `log10`, `log2` and both arguments of `log`, -1 for `log1p`)
+//! returns NULL instead of the NaN or -Infinity that IEEE 754 gives. NaN is
+//! not below the asymptote, so a NaN input returns NaN.
+//!
+//! DataFusion's built-ins return -Infinity or NaN for those inputs, and its
+//! one-argument `log` is base 10 where Spark's is the natural logarithm.
+//!
+//! Spark computes the logarithms with `java.lang.StrictMath`, so these use the
+//! fdlibm translation in [`super::fdlibm`] to return the same bits. Rust's
+//! `f64::ln` differs in the last digit for some inputs.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, exec_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    TypeSignature, Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+use datafusion_macros::user_doc;
+
+use super::fdlibm;
+
+/// Applies `f` to every value of a Float64 array, returning NULL where the
+/// value is at or below `asymptote`, as Spark's `UnaryLogExpression` does.
+fn unary_log(
+    name: &str,
+    args: &[ArrayRef],
+    asymptote: f64,
+    f: impl Fn(f64) -> f64,
+) -> Result<ArrayRef> {
+    let [arg] = take_function_args(name, args)?;
+    let result: Float64Array = arg
+        .as_primitive::<Float64Type>()
+        .unary_opt(|x| if x <= asymptote { None } else { Some(f(x)) });
+    Ok(Arc::new(result))
+}
+
+/// Defines a one-argument Spark logarithm UDF over DOUBLE.
+macro_rules! unary_log_udf {
+    ($struct:ident, $name:literal, $asymptote:expr, $f:expr) => {
+        impl Default for $struct {
+            fn default() -> Self {
+                Self::new()
+            }
+        }
+
+        impl $struct {
+            pub fn new() -> Self {
+                Self {
+                    signature: Signature::exact(
+                        vec![DataType::Float64],
+                        Volatility::Immutable,
+                    ),

Review Comment:
   Done in 3cbe805. All five functions now use `Coercion::new_implicit` with 
`logical_string()` among the allowed sources. `ln.slt` and `log.slt` cover 
string arguments and the ANSI error for a malformed string. The ANSI-off NULL 
case is commented out, citing #88.



##########
crates/spark/src/function/math/log.rs:
##########
@@ -0,0 +1,317 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible logarithm functions: `ln`, `log10`, `log2`, `log1p` and 
`log`.
+//!
+//! Spark follows Hive here: an input at or below the function's asymptote
+//! (0 for `ln`, `log10`, `log2` and both arguments of `log`, -1 for `log1p`)
+//! returns NULL instead of the NaN or -Infinity that IEEE 754 gives. NaN is
+//! not below the asymptote, so a NaN input returns NaN.
+//!
+//! DataFusion's built-ins return -Infinity or NaN for those inputs, and its
+//! one-argument `log` is base 10 where Spark's is the natural logarithm.
+//!
+//! Spark computes the logarithms with `java.lang.StrictMath`, so these use the
+//! fdlibm translation in [`super::fdlibm`] to return the same bits. Rust's
+//! `f64::ln` differs in the last digit for some inputs.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, exec_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    TypeSignature, Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+use datafusion_macros::user_doc;
+
+use super::fdlibm;
+
+/// Applies `f` to every value of a Float64 array, returning NULL where the
+/// value is at or below `asymptote`, as Spark's `UnaryLogExpression` does.
+fn unary_log(
+    name: &str,
+    args: &[ArrayRef],
+    asymptote: f64,
+    f: impl Fn(f64) -> f64,
+) -> Result<ArrayRef> {
+    let [arg] = take_function_args(name, args)?;
+    let result: Float64Array = arg
+        .as_primitive::<Float64Type>()
+        .unary_opt(|x| if x <= asymptote { None } else { Some(f(x)) });
+    Ok(Arc::new(result))
+}
+
+/// Defines a one-argument Spark logarithm UDF over DOUBLE.
+macro_rules! unary_log_udf {
+    ($struct:ident, $name:literal, $asymptote:expr, $f:expr) => {
+        impl Default for $struct {
+            fn default() -> Self {
+                Self::new()
+            }
+        }
+
+        impl $struct {
+            pub fn new() -> Self {
+                Self {
+                    signature: Signature::exact(
+                        vec![DataType::Float64],
+                        Volatility::Immutable,
+                    ),
+                }
+            }
+        }
+
+        impl ScalarUDFImpl for $struct {
+            fn name(&self) -> &str {
+                $name
+            }
+
+            fn signature(&self) -> &Signature {
+                &self.signature
+            }
+
+            fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> 
{
+                Ok(DataType::Float64)
+            }
+
+            fn documentation(&self) -> Option<&Documentation> {
+                self.doc()
+            }
+
+            fn invoke_with_args(
+                &self,
+                args: ScalarFunctionArgs,
+            ) -> Result<ColumnarValue> {
+                make_scalar_function(
+                    |args: &[ArrayRef]| unary_log($name, args, $asymptote, $f),
+                    vec![],
+                )(&args.args)
+            }
+        }
+    };
+}
+
+/// Spark-compatible `ln` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#ln>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the natural logarithm (base e) of `expr` as a 
DOUBLE. The argument is \
+        cast to DOUBLE. Unlike DataFusion's built-in `ln`, which returns 
-Infinity for 0 and NaN for \
+        a negative number, an input less than or equal to 0 returns NULL. 
Returns NULL if the input \
+        is NULL, and NaN if it is NaN.",
+    syntax_example = "ln(expr)",
+    sql_example = r#"```sql
+> SELECT ln(1);
+0
+> SELECT ln(0);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLn {
+    signature: Signature,
+}
+
+unary_log_udf!(SparkLn, "ln", 0.0, fdlibm::log);
+
+/// Spark-compatible `log10` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log10>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the base 10 logarithm of `expr` as a DOUBLE. The 
argument is cast to \
+        DOUBLE. Unlike DataFusion's built-in `log10`, which returns -Infinity 
for 0 and NaN for a \
+        negative number, an input less than or equal to 0 returns NULL. 
Returns NULL if the input \
+        is NULL, and NaN if it is NaN.",
+    syntax_example = "log10(expr)",
+    sql_example = r#"```sql
+> SELECT log10(10);
+1
+> SELECT log10(-1);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog10 {
+    signature: Signature,
+}
+
+unary_log_udf!(SparkLog10, "log10", 0.0, fdlibm::log10);
+
+/// Spark-compatible `log2` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log2>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the base 2 logarithm of `expr` as a DOUBLE. The 
argument is cast to \
+        DOUBLE. Like Spark, it is computed as `ln(expr) / ln(2)`, so it can 
differ from an exact \
+        base 2 logarithm in the last digit. Unlike DataFusion's built-in 
`log2`, which returns \
+        -Infinity for 0 and NaN for a negative number, an input less than or 
equal to 0 returns \
+        NULL. Returns NULL if the input is NULL, and NaN if it is NaN.",
+    syntax_example = "log2(expr)",
+    sql_example = r#"```sql
+> SELECT log2(2);
+1
+> SELECT log2(0);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog2 {
+    signature: Signature,
+}
+
+// Spark computes `StrictMath.log(x) / StrictMath.log(2)` rather than a true
+// base 2 logarithm, which differs in the last bit for some inputs.
+unary_log_udf!(SparkLog2, "log2", 0.0, |x: f64| {
+    fdlibm::log(x) / fdlibm::log(2.0)
+});

Review Comment:
   Done in 3cbe805, with `log_of_two_is_ln_2` in `fdlibm::tests`.



##########
crates/spark/src/function/math/log.rs:
##########
@@ -0,0 +1,317 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible logarithm functions: `ln`, `log10`, `log2`, `log1p` and 
`log`.
+//!
+//! Spark follows Hive here: an input at or below the function's asymptote
+//! (0 for `ln`, `log10`, `log2` and both arguments of `log`, -1 for `log1p`)
+//! returns NULL instead of the NaN or -Infinity that IEEE 754 gives. NaN is
+//! not below the asymptote, so a NaN input returns NaN.
+//!
+//! DataFusion's built-ins return -Infinity or NaN for those inputs, and its
+//! one-argument `log` is base 10 where Spark's is the natural logarithm.
+//!
+//! Spark computes the logarithms with `java.lang.StrictMath`, so these use the
+//! fdlibm translation in [`super::fdlibm`] to return the same bits. Rust's
+//! `f64::ln` differs in the last digit for some inputs.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, exec_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    TypeSignature, Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+use datafusion_macros::user_doc;
+
+use super::fdlibm;
+
+/// Applies `f` to every value of a Float64 array, returning NULL where the
+/// value is at or below `asymptote`, as Spark's `UnaryLogExpression` does.
+fn unary_log(
+    name: &str,
+    args: &[ArrayRef],
+    asymptote: f64,
+    f: impl Fn(f64) -> f64,
+) -> Result<ArrayRef> {
+    let [arg] = take_function_args(name, args)?;
+    let result: Float64Array = arg
+        .as_primitive::<Float64Type>()
+        .unary_opt(|x| if x <= asymptote { None } else { Some(f(x)) });
+    Ok(Arc::new(result))
+}
+
+/// Defines a one-argument Spark logarithm UDF over DOUBLE.
+macro_rules! unary_log_udf {
+    ($struct:ident, $name:literal, $asymptote:expr, $f:expr) => {
+        impl Default for $struct {
+            fn default() -> Self {
+                Self::new()
+            }
+        }
+
+        impl $struct {
+            pub fn new() -> Self {
+                Self {
+                    signature: Signature::exact(
+                        vec![DataType::Float64],
+                        Volatility::Immutable,
+                    ),
+                }
+            }
+        }
+
+        impl ScalarUDFImpl for $struct {
+            fn name(&self) -> &str {
+                $name
+            }
+
+            fn signature(&self) -> &Signature {
+                &self.signature
+            }
+
+            fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> 
{
+                Ok(DataType::Float64)
+            }
+
+            fn documentation(&self) -> Option<&Documentation> {
+                self.doc()
+            }
+
+            fn invoke_with_args(
+                &self,
+                args: ScalarFunctionArgs,
+            ) -> Result<ColumnarValue> {
+                make_scalar_function(
+                    |args: &[ArrayRef]| unary_log($name, args, $asymptote, $f),
+                    vec![],
+                )(&args.args)
+            }
+        }
+    };
+}
+
+/// Spark-compatible `ln` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#ln>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the natural logarithm (base e) of `expr` as a 
DOUBLE. The argument is \
+        cast to DOUBLE. Unlike DataFusion's built-in `ln`, which returns 
-Infinity for 0 and NaN for \
+        a negative number, an input less than or equal to 0 returns NULL. 
Returns NULL if the input \
+        is NULL, and NaN if it is NaN.",
+    syntax_example = "ln(expr)",
+    sql_example = r#"```sql
+> SELECT ln(1);
+0
+> SELECT ln(0);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLn {
+    signature: Signature,
+}
+
+unary_log_udf!(SparkLn, "ln", 0.0, fdlibm::log);
+
+/// Spark-compatible `log10` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log10>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the base 10 logarithm of `expr` as a DOUBLE. The 
argument is cast to \
+        DOUBLE. Unlike DataFusion's built-in `log10`, which returns -Infinity 
for 0 and NaN for a \
+        negative number, an input less than or equal to 0 returns NULL. 
Returns NULL if the input \
+        is NULL, and NaN if it is NaN.",
+    syntax_example = "log10(expr)",
+    sql_example = r#"```sql
+> SELECT log10(10);
+1
+> SELECT log10(-1);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog10 {
+    signature: Signature,
+}
+
+unary_log_udf!(SparkLog10, "log10", 0.0, fdlibm::log10);
+
+/// Spark-compatible `log2` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log2>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the base 2 logarithm of `expr` as a DOUBLE. The 
argument is cast to \
+        DOUBLE. Like Spark, it is computed as `ln(expr) / ln(2)`, so it can 
differ from an exact \
+        base 2 logarithm in the last digit. Unlike DataFusion's built-in 
`log2`, which returns \
+        -Infinity for 0 and NaN for a negative number, an input less than or 
equal to 0 returns \
+        NULL. Returns NULL if the input is NULL, and NaN if it is NaN.",
+    syntax_example = "log2(expr)",
+    sql_example = r#"```sql
+> SELECT log2(2);
+1
+> SELECT log2(0);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog2 {
+    signature: Signature,
+}
+
+// Spark computes `StrictMath.log(x) / StrictMath.log(2)` rather than a true
+// base 2 logarithm, which differs in the last bit for some inputs.
+unary_log_udf!(SparkLog2, "log2", 0.0, |x: f64| {
+    fdlibm::log(x) / fdlibm::log(2.0)
+});
+
+/// Spark-compatible `log1p` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log1p>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns `ln(1 + expr)` as a DOUBLE, computed without the 
precision loss of \
+        adding 1 to `expr` when `expr` is close to 0. DataFusion has no 
built-in `log1p`. The \
+        argument is cast to DOUBLE. An input less than or equal to -1 returns 
NULL. Returns NULL \
+        if the input is NULL, and NaN if it is NaN.",
+    syntax_example = "log1p(expr)",
+    sql_example = r#"```sql
+> SELECT log1p(0);
+0
+> SELECT log1p(-1);
+NULL
+```"#,
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL or a number 
less than or equal to -1 returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog1p {
+    signature: Signature,
+}
+
+unary_log_udf!(SparkLog1p, "log1p", -1.0, fdlibm::log1p);
+
+/// Spark-compatible `log` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#log>
+#[user_doc(
+    doc_section(label = "Math Functions"),
+    description = "Returns the logarithm of `expr` in base `base` as a DOUBLE, 
computed as \
+        `ln(expr) / ln(base)`. With one argument it returns the natural 
logarithm of `expr`, \
+        where DataFusion's built-in `log` returns the base 10 logarithm. Both 
arguments are cast \
+        to DOUBLE. Returns NULL if either argument is NULL or less than or 
equal to 0, otherwise \
+        NaN if either is NaN. A base of 1 divides by 0, so it returns 
Infinity, -Infinity, or NaN \
+        when `expr` is also 1.",
+    syntax_example = "log([base, ]expr)",
+    sql_example = r#"```sql
+> SELECT log(10, 100);
+2
+> SELECT log(1);
+0
+> SELECT log(0, 10);
+NULL
+```"#,
+    argument(
+        name = "base",
+        description = "Optional numeric base, cast to DOUBLE. Defaults to e. 
NULL, 0 or a negative number returns NULL."
+    ),
+    argument(
+        name = "expr",
+        description = "Numeric expression, cast to DOUBLE. NULL, 0 or a 
negative number returns NULL."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkLog {
+    signature: Signature,
+}
+
+impl Default for SparkLog {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkLog {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::one_of(
+                vec![
+                    TypeSignature::Exact(vec![DataType::Float64]),
+                    TypeSignature::Exact(vec![DataType::Float64, 
DataType::Float64]),
+                ],
+                Volatility::Immutable,
+            ),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkLog {
+    fn name(&self) -> &str {
+        "log"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        Ok(DataType::Float64)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        make_scalar_function(spark_log, vec![])(&args.args)
+    }
+}
+
+fn spark_log(args: &[ArrayRef]) -> Result<ArrayRef> {
+    match args {
+        // Spark's one-argument `log` is `Logarithm(EulerNumber(), expr)`,
+        // which its generated code evaluates as `StrictMath.log(expr)`.
+        [_] => unary_log("log", args, 0.0, fdlibm::log),
+        [base, value] => {
+            let base = base.as_primitive::<Float64Type>();
+            let value = value.as_primitive::<Float64Type>();
+            let result: Float64Array = base
+                .iter()
+                .zip(value.iter())
+                .map(|(base, value)| match (base, value) {
+                    // NaN is not <= 0, so a NaN argument yields NaN
+                    // unless the other argument is <= 0.
+                    (Some(base), Some(value)) if base <= 0.0 || value <= 0.0 
=> None,
+                    (Some(base), Some(value)) => {
+                        Some(fdlibm::log(value) / fdlibm::log(base))
+                    }
+                    _ => None,
+                })
+                .collect();
+            Ok(Arc::new(result))

Review Comment:
   Done in 3cbe805. A scalar base with an array `expr` now takes 
`fdlibm::log(base)` once and maps `value` with `unary_opt`. A NULL or 
non-positive base returns all NULLs. A new query covers NULL, 0, negative and 
NaN bases.



##########
crates/sqllogictest/test_files/math/log.slt:
##########
@@ -8,7 +8,102 @@
 # For more information, please see:
 #   https://github.com/apache/datafusion/issues/15914
 
+
 ## Original Query: SELECT log(10, 100);
 ## PySpark 3.5.5 Result: {'LOG(10, 100)': 2.0, 'typeof(LOG(10, 100))': 
'double', 'typeof(10)': 'int', 'typeof(100)': 'int'}
-#query
-#SELECT log(10::int, 100::int);
+query R
+SELECT log(10::int, 100::int);
+----
+2
+
+query T
+SELECT arrow_typeof(log(10::int, 100::int));
+----
+Float64
+
+query RR
+SELECT log(2::bigint, 1024::bigint), log(0.5::double, 8::double);
+----
+10 -3
+
+# Computed as ln(expr) / ln(base), so results can be off in the last digit.
+# Rust's f64::ln gives 4 for log(3, 81); StrictMath.log, which Spark uses,
+# gives 4.000000000000001.
+query RR
+SELECT log(10::int, 1000::int), log(3::int, 81::int);
+----
+2.9999999999999996 4.000000000000001
+
+query R
+SELECT log(10::decimal(5, 1), 100::decimal(5, 1));
+----
+2
+
+# One argument is the natural logarithm, not base 10 as in DataFusion
+query RRR
+SELECT log(1::int), log(2::double), log(10::decimal(5, 2));
+----
+0 0.6931471805599453 2.302585092994046
+
+query T
+SELECT arrow_typeof(log(1::int));
+----
+Float64
+
+# NULL if either argument is <= 0
+query RRRRRR
+SELECT log(0::int, 10::int), log(-2::int, 10::int), log(2::int, 0::int), 
log(2::int, -8::int), log(-1::double), log(0::double);
+----
+NULL NULL NULL NULL NULL NULL
+
+# ...even when the other argument is NaN
+query RR
+SELECT log(0::double, 'NaN'::double), log('NaN'::double, -1::double);
+----
+NULL NULL
+
+query RRRR
+SELECT log(NULL::double, 2::double), log(2::double, NULL::double), 
log(NULL::double), log(NULL::double, NULL::double);
+----
+NULL NULL NULL NULL
+
+query RRR
+SELECT log('NaN'::double, 2::double), log(2::double, 'NaN'::double), 
log('NaN'::double);
+----
+NaN NaN NaN
+
+# A base of 1 divides by ln(1) = 0
+query RRR
+SELECT log(1::int, 2::int), log(1::double, 0.5::double), log(1::int, 1::int);
+----
+Infinity -Infinity NaN
+
+query RRR
+SELECT log('Infinity'::double, 2::double), log(2::double, 'Infinity'::double), 
log('Infinity'::double, 'Infinity'::double);
+----
+0 Infinity NaN
+
+# Array path, including NULL and non-positive rows
+query R
+SELECT log(b, x) FROM (VALUES (10::double, 100::double), (2::double, 
0::double), (NULL::double, 3::double), (0::double, 3::double), (2::double, 
8::double)) AS t(b, x);
+----
+2
+NULL
+NULL
+NULL
+3
+
+# Scalar base with an array expr
+query R
+SELECT log(2::int, x) FROM (VALUES (8::double), (-1::double), (NULL::double)) 
AS t(x);
+----
+3
+NULL
+NULL

Review Comment:
   Added in 3cbe805.



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

Reply via email to