Skip to content

Commit 37dbf9e

Browse files
authored
Refactor Spark expm1 signature (#18655)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> Part of #12725 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Prefer to avoid user_defined for consistency in function definitions. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Refactor signature of expm1 away from user_defined. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Existing tests ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> No <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 377c0fc commit 37dbf9e

File tree

2 files changed

+10
-58
lines changed

2 files changed

+10
-58
lines changed

datafusion/spark/src/function/math/expm1.rs

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::function::error_utils::{
19-
invalid_arg_count_exec_err, unsupported_data_type_exec_err,
20-
};
18+
use crate::function::error_utils::unsupported_data_type_exec_err;
2119
use arrow::array::{ArrayRef, AsArray};
2220
use arrow::datatypes::{DataType, Float64Type};
21+
use datafusion_common::utils::take_function_args;
2322
use datafusion_common::{Result, ScalarValue};
2423
use datafusion_expr::{
2524
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
@@ -31,7 +30,6 @@ use std::sync::Arc;
3130
#[derive(Debug, PartialEq, Eq, Hash)]
3231
pub struct SparkExpm1 {
3332
signature: Signature,
34-
aliases: Vec<String>,
3533
}
3634

3735
impl Default for SparkExpm1 {
@@ -43,8 +41,7 @@ impl Default for SparkExpm1 {
4341
impl SparkExpm1 {
4442
pub fn new() -> Self {
4543
Self {
46-
signature: Signature::user_defined(Volatility::Immutable),
47-
aliases: vec![],
44+
signature: Signature::exact(vec![DataType::Float64], Volatility::Immutable),
4845
}
4946
}
5047
}
@@ -67,10 +64,8 @@ impl ScalarUDFImpl for SparkExpm1 {
6764
}
6865

6966
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
70-
if args.args.len() != 1 {
71-
return Err(invalid_arg_count_exec_err("expm1", (1, 1), args.args.len()));
72-
}
73-
match &args.args[0] {
67+
let [arg] = take_function_args(self.name(), args.args)?;
68+
match arg {
7469
ColumnarValue::Scalar(ScalarValue::Float64(value)) => Ok(
7570
ColumnarValue::Scalar(ScalarValue::Float64(value.map(|x| x.exp_m1()))),
7671
),
@@ -94,52 +89,4 @@ impl ScalarUDFImpl for SparkExpm1 {
9489
)),
9590
}
9691
}
97-
98-
fn aliases(&self) -> &[String] {
99-
&self.aliases
100-
}
101-
102-
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
103-
if arg_types.len() != 1 {
104-
return Err(invalid_arg_count_exec_err("expm1", (1, 1), arg_types.len()));
105-
}
106-
if arg_types[0].is_numeric() {
107-
Ok(vec![DataType::Float64])
108-
} else {
109-
Err(unsupported_data_type_exec_err(
110-
"expm1",
111-
"Numeric Type",
112-
&arg_types[0],
113-
))
114-
}
115-
}
116-
}
117-
118-
#[cfg(test)]
119-
mod tests {
120-
use crate::function::math::expm1::SparkExpm1;
121-
use crate::function::utils::test::test_scalar_function;
122-
use arrow::array::{Array, Float64Array};
123-
use arrow::datatypes::DataType::Float64;
124-
use datafusion_common::{Result, ScalarValue};
125-
use datafusion_expr::{ColumnarValue, ScalarUDFImpl};
126-
127-
macro_rules! test_expm1_float64_invoke {
128-
($INPUT:expr, $EXPECTED:expr) => {
129-
test_scalar_function!(
130-
SparkExpm1::new(),
131-
vec![ColumnarValue::Scalar(ScalarValue::Float64($INPUT))],
132-
$EXPECTED,
133-
f64,
134-
Float64,
135-
Float64Array
136-
);
137-
};
138-
}
139-
140-
#[test]
141-
fn test_expm1_invoke() -> Result<()> {
142-
test_expm1_float64_invoke!(Some(0f64), Ok(Some(0.0f64)));
143-
Ok(())
144-
}
14592
}

datafusion/sqllogictest/test_files/spark/math/expm1.slt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ SELECT expm1(a) FROM (VALUES (0::INT), (1::INT)) AS t(a);
3030
----
3131
0
3232
1.718281828459045
33+
34+
query R
35+
SELECT expm1(0.0::double);
36+
----
37+
0

0 commit comments

Comments
 (0)