-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: move acos() to function crate #9297
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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::{exec_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], | ||
self.name(), | ||
Float32Array, | ||
Float32Array, | ||
{ f32::acos } | ||
)), | ||
other => { | ||
return exec_err!( | ||
"Unsupported data type {other:?} for function {}", | ||
self.name() | ||
) | ||
} | ||
}; | ||
Ok(ColumnarValue::Array(arr)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! Encoding expressions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, seems to be a copy-paste error |
||
//! Math function: `isnan()`. | ||
|
||
use arrow::datatypes::DataType; | ||
use datafusion_common::{exec_err, DataFusionError, Result}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -889,14 +889,14 @@ mod test { | |
// test that automatic argument type coercion for scalar functions work | ||
let empty = empty(); | ||
let lit_expr = lit(10i64); | ||
let fun: BuiltinScalarFunction = BuiltinScalarFunction::Acos; | ||
let fun: BuiltinScalarFunction = BuiltinScalarFunction::Floor; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
let scalar_function_expr = | ||
Expr::ScalarFunction(ScalarFunction::new(fun, vec![lit_expr])); | ||
let plan = LogicalPlan::Projection(Projection::try_new( | ||
vec![scalar_function_expr], | ||
empty, | ||
)?); | ||
let expected = "Projection: acos(CAST(Int64(10) AS Float64))\n EmptyRelation"; | ||
let expected = "Projection: floor(CAST(Int64(10) AS Float64))\n EmptyRelation"; | ||
assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, expected) | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be a copy-paste error, so I changed it