Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/spark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ datafusion-functions-nested = { workspace = true }
datafusion-macros = { workspace = true }
datafusion-physical-expr-common = { workspace = true }
datafusion-session = { workspace = true }
libm = "0.2.16"
log = { workspace = true }
num-traits = { workspace = true }
percent-encoding = "2.3.2"
Expand Down
12 changes: 10 additions & 2 deletions crates/spark/src/function/math/expm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl ScalarUDFImpl for SparkExpm1 {
let [arg] = take_function_args(self.name(), args.args)?;
match arg {
ColumnarValue::Scalar(ScalarValue::Float64(value)) => Ok(
ColumnarValue::Scalar(ScalarValue::Float64(value.map(|x| x.exp_m1()))),
ColumnarValue::Scalar(ScalarValue::Float64(value.map(spark_expm1))),
),
ColumnarValue::Array(array) => match array.data_type() {
DataType::Float64 => Ok(ColumnarValue::Array(Arc::new(
array
.as_primitive::<Float64Type>()
.unary::<_, Float64Type>(|x| x.exp_m1()),
.unary::<_, Float64Type>(spark_expm1),
)
as ArrayRef)),
other => Err(unsupported_data_type_exec_err(
Expand All @@ -97,3 +97,11 @@ impl ScalarUDFImpl for SparkExpm1 {
}
}
}

/// Spark's `Expm1` calls Java's `StrictMath.expm1`, which returns fdlibm's
/// result on every platform. `f64::exp_m1` calls the platform's C math library,
/// which can differ in the last bit, as on macOS. The `libm` crate is a port of
/// musl's math library, which comes from fdlibm, so it matches Spark.
fn spark_expm1(x: f64) -> f64 {
libm::expm1(x)
}
17 changes: 17 additions & 0 deletions crates/sqllogictest/test_files/math/expm1.slt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ query R
SELECT expm1(0.0::double);
----
0

# Spark returns fdlibm's result. For each of these, macOS's math library
# returns a result that differs in the last bit.
query R
SELECT expm1(a) FROM (VALUES (-2.5::DOUBLE), (0.9::DOUBLE), (1.7::DOUBLE)) AS t(a);
----
-0.9179150013761013
1.4596031111569499
4.473947391727199

query R
SELECT expm1(a) FROM (VALUES (-0.5::DOUBLE), (1e-10::DOUBLE), (710.0::DOUBLE), (-800.0::DOUBLE)) AS t(a);
----
-0.3934693402873666
0.000000000100000000005
Infinity
-1
Loading