Skip to content

Commit

Permalink
refactor: move acos() to function crate
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Feb 21, 2024
1 parent 89ee9b0 commit da1da35
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 26 deletions.
10 changes: 2 additions & 8 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ pub enum BuiltinScalarFunction {
// math functions
/// abs
Abs,
/// acos
Acos,
/// asin
Asin,
/// atan
Expand Down Expand Up @@ -365,7 +363,6 @@ impl BuiltinScalarFunction {
match self {
// Immutable scalar builtins
BuiltinScalarFunction::Abs => Volatility::Immutable,
BuiltinScalarFunction::Acos => Volatility::Immutable,
BuiltinScalarFunction::Asin => Volatility::Immutable,
BuiltinScalarFunction::Atan => Volatility::Immutable,
BuiltinScalarFunction::Atan2 => Volatility::Immutable,
Expand Down Expand Up @@ -877,8 +874,7 @@ impl BuiltinScalarFunction {
utf8_to_int_type(&input_expr_types[0], "levenshtein")
}

BuiltinScalarFunction::Acos
| BuiltinScalarFunction::Asin
BuiltinScalarFunction::Asin
| BuiltinScalarFunction::Atan
| BuiltinScalarFunction::Acosh
| BuiltinScalarFunction::Asinh
Expand Down Expand Up @@ -1351,8 +1347,7 @@ impl BuiltinScalarFunction {
vec![Exact(vec![Utf8, Utf8]), Exact(vec![LargeUtf8, LargeUtf8])],
self.volatility(),
),
BuiltinScalarFunction::Acos
| BuiltinScalarFunction::Asin
BuiltinScalarFunction::Asin
| BuiltinScalarFunction::Atan
| BuiltinScalarFunction::Acosh
| BuiltinScalarFunction::Asinh
Expand Down Expand Up @@ -1444,7 +1439,6 @@ impl BuiltinScalarFunction {
pub fn aliases(&self) -> &'static [&'static str] {
match self {
BuiltinScalarFunction::Abs => &["abs"],
BuiltinScalarFunction::Acos => &["acos"],
BuiltinScalarFunction::Acosh => &["acosh"],
BuiltinScalarFunction::Asin => &["asin"],
BuiltinScalarFunction::Asinh => &["asinh"],
Expand Down
2 changes: 0 additions & 2 deletions datafusion/expr/src/expr_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ scalar_expr!(Sinh, sinh, num, "hyperbolic sine");
scalar_expr!(Cosh, cosh, num, "hyperbolic cosine");
scalar_expr!(Tanh, tanh, num, "hyperbolic tangent");
scalar_expr!(Asin, asin, num, "inverse sine");
scalar_expr!(Acos, acos, num, "inverse cosine");
scalar_expr!(Atan, atan, num, "inverse tangent");
scalar_expr!(Asinh, asinh, num, "inverse hyperbolic sine");
scalar_expr!(Acosh, acosh, num, "inverse hyperbolic cosine");
Expand Down Expand Up @@ -1340,7 +1339,6 @@ mod test {
test_unary_scalar_expr!(Cosh, cosh);
test_unary_scalar_expr!(Tanh, tanh);
test_unary_scalar_expr!(Asin, asin);
test_unary_scalar_expr!(Acos, acos);
test_unary_scalar_expr!(Atan, atan);
test_unary_scalar_expr!(Asinh, asinh);
test_unary_scalar_expr!(Acosh, acosh);
Expand Down
86 changes: 86 additions & 0 deletions datafusion/functions/src/math/acos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, 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.

//! Math function: `acos()`.
use arrow::datatypes::DataType;
use datafusion_common::{internal_err, Result, DataFusionError};
use datafusion_expr::ColumnarValue;

use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
use std::any::Any;
use std::sync::Arc;
use arrow::array::{ArrayRef, Float32Array, Float64Array};

#[derive(Debug)]
pub(super) struct AcosFunc {
signature: Signature,
}

impl AcosFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature:
Signature::uniform(1, vec![Float64, Float32], Volatility::Immutable)
}
}
}

impl ScalarUDFImpl for AcosFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"acos"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
Ok(arg_types[0].clone())
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
let args = ColumnarValue::values_to_arrays(args)?;

let arr: ArrayRef = match args[0].data_type() {
DataType::Float64 => {
Arc::new(make_function_scalar_inputs_return_type!(
&args[0],
self.name(),
Float64Array,
Float64Array,
{ f64::acos }
))
},
DataType::Float32 => {
Arc::new(make_function_scalar_inputs_return_type!(
&args[0],
"x",
Float32Array,
Float32Array,
{ f32::acos }
))
},
other => return internal_err!("Unsupported data type {other:?} for function {}", self.name()),
};
Ok(ColumnarValue::Array(arr))
}
}
7 changes: 5 additions & 2 deletions datafusion/functions/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
// specific language governing permissions and limitations
// under the License.

//! "core" DataFusion functions
//! "math" DataFusion functions
mod nans;
mod acos;

// create UDFs
make_udf_function!(nans::IsNanFunc, ISNAN, isnan);
make_udf_function!(acos::AcosFunc, ACOS, acos);

// Export the functions out of this package, both as expr_fn as well as a list of functions
export_functions!(
(isnan, num, "returns true if a given number is +NaN or -NaN otherwise returns false")
(isnan, num, "returns true if a given number is +NaN or -NaN otherwise returns false"),
(acos, num, "returns the arc cosine or inverse cosine of a number")
);

4 changes: 2 additions & 2 deletions datafusion/functions/src/math/nans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

//! Encoding expressions
//! Math function: `isnan()`.
use arrow::datatypes::DataType;
use datafusion_common::{internal_err, Result, DataFusionError};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl ScalarUDFImpl for IsNanFunc {
{ f32::is_nan }
))
},
other => return internal_err!("Unsupported data type {other:?} for function isnan"),
other => return internal_err!("Unsupported data type {other:?} for function {}", self.name()),
};
Ok(ColumnarValue::Array(arr))
}
Expand Down
1 change: 0 additions & 1 deletion datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ pub fn create_physical_fun(
BuiltinScalarFunction::Abs => Arc::new(|args| {
make_scalar_function_inner(math_expressions::abs_invoke)(args)
}),
BuiltinScalarFunction::Acos => Arc::new(math_expressions::acos),
BuiltinScalarFunction::Asin => Arc::new(math_expressions::asin),
BuiltinScalarFunction::Atan => Arc::new(math_expressions::atan),
BuiltinScalarFunction::Acosh => Arc::new(math_expressions::acosh),
Expand Down
2 changes: 1 addition & 1 deletion datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ message InListNode {

enum ScalarFunction {
Abs = 0;
Acos = 1;
// 1 was Acos
Asin = 2;
Atan = 3;
Ascii = 4;
Expand Down
3 changes: 0 additions & 3 deletions datafusion/proto/src/generated/pbjson.rs

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

4 changes: 1 addition & 3 deletions datafusion/proto/src/generated/prost.rs

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

4 changes: 1 addition & 3 deletions datafusion/proto/src/logical_plan/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use datafusion_common::{
use datafusion_expr::expr::Unnest;
use datafusion_expr::window_frame::{check_window_frame, regularize_window_order_by};
use datafusion_expr::{
abs, acos, acosh, array, array_append, array_concat, array_dims, array_distinct,
abs, acosh, array, array_append, array_concat, array_dims, array_distinct,
array_element, array_empty, array_except, array_has, array_has_all, array_has_any,
array_intersect, array_length, array_ndims, array_pop_back, array_pop_front,
array_position, array_positions, array_prepend, array_remove, array_remove_all,
Expand Down Expand Up @@ -449,7 +449,6 @@ impl From<&protobuf::ScalarFunction> for BuiltinScalarFunction {
ScalarFunction::Tan => Self::Tan,
ScalarFunction::Cot => Self::Cot,
ScalarFunction::Asin => Self::Asin,
ScalarFunction::Acos => Self::Acos,
ScalarFunction::Atan => Self::Atan,
ScalarFunction::Sinh => Self::Sinh,
ScalarFunction::Cosh => Self::Cosh,
Expand Down Expand Up @@ -1355,7 +1354,6 @@ pub fn parse_expr(

match scalar_function {
ScalarFunction::Asin => Ok(asin(parse_expr(&args[0], registry)?)),
ScalarFunction::Acos => Ok(acos(parse_expr(&args[0], registry)?)),
ScalarFunction::Asinh => Ok(asinh(parse_expr(&args[0], registry)?)),
ScalarFunction::Acosh => Ok(acosh(parse_expr(&args[0], registry)?)),
ScalarFunction::Array => Ok(array(
Expand Down
1 change: 0 additions & 1 deletion datafusion/proto/src/logical_plan/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,6 @@ impl TryFrom<&BuiltinScalarFunction> for protobuf::ScalarFunction {
BuiltinScalarFunction::Cosh => Self::Cosh,
BuiltinScalarFunction::Tanh => Self::Tanh,
BuiltinScalarFunction::Asin => Self::Asin,
BuiltinScalarFunction::Acos => Self::Acos,
BuiltinScalarFunction::Atan => Self::Atan,
BuiltinScalarFunction::Asinh => Self::Asinh,
BuiltinScalarFunction::Acosh => Self::Acosh,
Expand Down

0 comments on commit da1da35

Please sign in to comment.