-
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.
refactor: move acos() to function crate
- Loading branch information
Showing
16 changed files
with
239 additions
and
177 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,110 @@ | ||
// 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::array::{ArrayRef, Float32Array, Float64Array}; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::{internal_err, plan_datafusion_err, DataFusionError, Result}; | ||
use datafusion_expr::ColumnarValue; | ||
use datafusion_expr::{ | ||
utils::generate_signature_error_msg, ScalarUDFImpl, Signature, Volatility, | ||
}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug)] | ||
pub 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> { | ||
if arg_types.len() != 1 { | ||
return Err(plan_datafusion_err!( | ||
"{}", | ||
generate_signature_error_msg( | ||
self.name(), | ||
self.signature().clone(), | ||
arg_types, | ||
) | ||
)); | ||
} | ||
|
||
let arg_type = &arg_types[0]; | ||
|
||
match arg_type { | ||
DataType::Float64 => Ok(DataType::Float64), | ||
DataType::Float32 => Ok(DataType::Float32), | ||
|
||
// For other types (possible values null/int), use Float 64 | ||
_ => Ok(DataType::Float64), | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.