-
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
Move ceil
, exp
, factorial
to datafusion-functions
crate
#10083
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aafa676
Issue-9939 - Move ceil, exp, factorial to datafusion-functions crate
erenavsarogullari a3b98f2
Issue-9939 - Fix build failures
erenavsarogullari d25ac87
Merge remote-tracking branch 'apache/main' into Issue-9939
alamb f1a44e9
Update Volatility
alamb 3673665
Merge branch 'Issue-9939' of github.com:erenavsarogullari/arrow-dataf…
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 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. | ||
|
||
use arrow::array::{ArrayRef, Int64Array}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use arrow::datatypes::DataType; | ||
use arrow::datatypes::DataType::Int64; | ||
|
||
use crate::utils::make_scalar_function; | ||
use datafusion_common::{exec_err, DataFusionError, Result}; | ||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||
|
||
#[derive(Debug)] | ||
pub struct FactorialFunc { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for FactorialFunc { | ||
fn default() -> Self { | ||
FactorialFunc::new() | ||
} | ||
} | ||
|
||
impl FactorialFunc { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::uniform(1, vec![Int64], Volatility::Volatile), | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for FactorialFunc { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"factorial" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(Int64) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
make_scalar_function(factorial, vec![])(args) | ||
} | ||
} | ||
|
||
macro_rules! make_function_scalar_inputs { | ||
($ARG: expr, $NAME:expr, $ARRAY_TYPE:ident, $FUNC: block) => {{ | ||
let arg = downcast_arg!($ARG, $NAME, $ARRAY_TYPE); | ||
|
||
arg.iter() | ||
.map(|a| match a { | ||
Some(a) => Some($FUNC(a)), | ||
_ => None, | ||
}) | ||
.collect::<$ARRAY_TYPE>() | ||
}}; | ||
} | ||
|
||
/// Factorial SQL function | ||
fn factorial(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
match args[0].data_type() { | ||
DataType::Int64 => Ok(Arc::new(make_function_scalar_inputs!( | ||
&args[0], | ||
"value", | ||
Int64Array, | ||
{ |value: i64| { (1..=value).product() } } | ||
)) as ArrayRef), | ||
other => exec_err!("Unsupported data type {other:?} for function factorial."), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
|
||
use datafusion_common::cast::as_int64_array; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_factorial_i64() { | ||
let args: Vec<ArrayRef> = vec![ | ||
Arc::new(Int64Array::from(vec![0, 1, 2, 4])), // input | ||
]; | ||
|
||
let result = factorial(&args).expect("failed to initialize function factorial"); | ||
let ints = | ||
as_int64_array(&result).expect("failed to initialize function factorial"); | ||
|
||
let expected = Int64Array::from(vec![1, 1, 2, 24]); | ||
|
||
assert_eq!(ints, &expected); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
wow --
BuiltInScalarFunction
is getting pretty sparse (I think @Omega359 plans the final part)