-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move
Atan2
, Atan
, Acosh
, Asinh
, Atanh
to `datafusion-functi…
…on` (#9872) * Refactor math functions in datafusion code * fic ci * fix: avoid regression * refactor: move atan2 function * chore: move atan2 test
- Loading branch information
Showing
13 changed files
with
201 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// 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: `atan2()`. | ||
use arrow::array::{ArrayRef, Float32Array, Float64Array}; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::DataFusionError; | ||
use datafusion_common::{exec_err, Result}; | ||
use datafusion_expr::ColumnarValue; | ||
use datafusion_expr::TypeSignature::*; | ||
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use crate::make_function_inputs2; | ||
use crate::utils::make_scalar_function; | ||
|
||
#[derive(Debug)] | ||
pub(super) struct Atan2 { | ||
signature: Signature, | ||
} | ||
|
||
impl Atan2 { | ||
pub fn new() -> Self { | ||
use DataType::*; | ||
Self { | ||
signature: Signature::one_of( | ||
vec![Exact(vec![Float32, Float32]), Exact(vec![Float64, Float64])], | ||
Volatility::Immutable, | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for Atan2 { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
fn name(&self) -> &str { | ||
"atan2" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
use self::DataType::*; | ||
match &arg_types[0] { | ||
Float32 => Ok(Float32), | ||
_ => Ok(Float64), | ||
} | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
make_scalar_function(atan2, vec![])(args) | ||
} | ||
} | ||
|
||
/// Atan2 SQL function | ||
pub fn atan2(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
match args[0].data_type() { | ||
DataType::Float64 => Ok(Arc::new(make_function_inputs2!( | ||
&args[0], | ||
&args[1], | ||
"y", | ||
"x", | ||
Float64Array, | ||
{ f64::atan2 } | ||
)) as ArrayRef), | ||
|
||
DataType::Float32 => Ok(Arc::new(make_function_inputs2!( | ||
&args[0], | ||
&args[1], | ||
"y", | ||
"x", | ||
Float32Array, | ||
{ f32::atan2 } | ||
)) as ArrayRef), | ||
|
||
other => exec_err!("Unsupported data type {other:?} for function atan2"), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use datafusion_common::cast::{as_float32_array, as_float64_array}; | ||
|
||
#[test] | ||
fn test_atan2_f64() { | ||
let args: Vec<ArrayRef> = vec![ | ||
Arc::new(Float64Array::from(vec![2.0, -3.0, 4.0, -5.0])), // y | ||
Arc::new(Float64Array::from(vec![1.0, 2.0, -3.0, -4.0])), // x | ||
]; | ||
|
||
let result = atan2(&args).expect("failed to initialize function atan2"); | ||
let floats = | ||
as_float64_array(&result).expect("failed to initialize function atan2"); | ||
|
||
assert_eq!(floats.len(), 4); | ||
assert_eq!(floats.value(0), (2.0_f64).atan2(1.0)); | ||
assert_eq!(floats.value(1), (-3.0_f64).atan2(2.0)); | ||
assert_eq!(floats.value(2), (4.0_f64).atan2(-3.0)); | ||
assert_eq!(floats.value(3), (-5.0_f64).atan2(-4.0)); | ||
} | ||
|
||
#[test] | ||
fn test_atan2_f32() { | ||
let args: Vec<ArrayRef> = vec![ | ||
Arc::new(Float32Array::from(vec![2.0, -3.0, 4.0, -5.0])), // y | ||
Arc::new(Float32Array::from(vec![1.0, 2.0, -3.0, -4.0])), // x | ||
]; | ||
|
||
let result = atan2(&args).expect("failed to initialize function atan2"); | ||
let floats = | ||
as_float32_array(&result).expect("failed to initialize function atan2"); | ||
|
||
assert_eq!(floats.len(), 4); | ||
assert_eq!(floats.value(0), (2.0_f32).atan2(1.0)); | ||
assert_eq!(floats.value(1), (-3.0_f32).atan2(2.0)); | ||
assert_eq!(floats.value(2), (4.0_f32).atan2(-3.0)); | ||
assert_eq!(floats.value(3), (-5.0_f32).atan2(-4.0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.