Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions datafusion/spark/src/function/math/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ use arrow::array::{Array, Int64Array};
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::{Int32, Int64};
use datafusion_common::cast::as_int32_array;
use datafusion_common::types::logical_int32;
use datafusion_common::{
DataFusionError, Result, ScalarValue, exec_err, utils::take_function_args,
};
use datafusion_expr::Signature;
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Volatility};
use datafusion_expr::{
Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, TypeSignatureClass,
Volatility,
};

/// <https://spark.apache.org/docs/latest/api/sql/index.html#factorial>
#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -43,7 +47,13 @@ impl Default for SparkFactorial {
impl SparkFactorial {
pub fn new() -> Self {
Self {
signature: Signature::exact(vec![Int32], Volatility::Immutable),
signature: Signature::coercible(
vec![Coercion::new_implicit_native(
logical_int32(),
vec![TypeSignatureClass::Integer],
)],
Volatility::Immutable,
),
aliases: vec![],
}
}
Expand Down
19 changes: 17 additions & 2 deletions datafusion/sqllogictest/test_files/spark/math/factorial.slt
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,20 @@ NULL
NULL
NULL

query error Error during planning: Failed to coerce arguments to satisfy a call to 'factorial' function
SELECT factorial(5::BIGINT);
query IIIII
SELECT factorial(5) AS i64_literal,
factorial(5::TINYINT) AS i8,
factorial(5::SMALLINT) AS i16,
factorial(5::BIGINT) AS i64,
factorial(NULL) AS null_input;
----
120 120 120 120 NULL

query I
SELECT factorial(a) FROM VALUES (-1::BIGINT), (0::BIGINT), (20::BIGINT), (21::BIGINT), (NULL) AS t(a);
----
NULL
1
2432902008176640000
NULL
NULL