Skip to content

Commit

Permalink
check signature for udf-based function impl
Browse files Browse the repository at this point in the history
  • Loading branch information
2010YOUY01 committed Feb 29, 2024
1 parent ca37ce3 commit a9e5942
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
12 changes: 12 additions & 0 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ impl ExprSchemable for Expr {
fun.return_type(&arg_data_types)
}
ScalarFunctionDefinition::UDF(fun) => {
// verify that input data types is consistent with function's `TypeSignature`
data_types(&arg_data_types, fun.signature()).map_err(|_| {
plan_datafusion_err!(
"{}",
utils::generate_signature_error_msg(
fun.name(),
fun.signature().clone(),
&arg_data_types,
)
)
})?;

Ok(fun.return_type_from_exprs(args, schema)?)
}
ScalarFunctionDefinition::Name(_) => {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ pub struct Values {

/// Evaluates an arbitrary list of expressions (essentially a
/// SELECT with an expression list) on its input.
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
// mark non_exhaustive to encourage use of try_new/new()
#[non_exhaustive]
pub struct Projection {
Expand Down
14 changes: 6 additions & 8 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,14 +873,12 @@ mod test {
fn scalar_udf_invalid_input() -> Result<()> {
let empty = empty();
let udf = ScalarUDF::from(TestScalarUDF {}).call(vec![lit("Apple")]);
let plan = LogicalPlan::Projection(Projection::try_new(vec![udf], empty)?);
let err = assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, "")
.err()
.unwrap();
assert_eq!(
"type_coercion\ncaused by\nError during planning: Coercion from [Utf8] to the signature Uniform(1, [Float32]) failed.",
err.strip_backtrace()
);
let plan_err = Projection::try_new(vec![udf], empty)
.expect_err("Expected an error due to incorrect function input");

let expected_error = "Error during planning: No function matches the given name and argument types 'TestScalarUDF(Utf8)'. You might need to add explicit type casts.";

assert!(plan_err.to_string().starts_with(expected_error));
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/errors.slt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ statement error Error during planning: No function matches the given name and ar
SELECT concat();

# error message for wrong function signature (Uniform: t args all from some common types)
statement error DataFusion error: Failed to coerce arguments for NULLIF
statement error DataFusion error: Error during planning: No function matches the given name and argument types 'nullif\(Int64\)'. You might need to add explicit type casts.
SELECT nullif(1);

# error message for wrong function signature (Exact: exact number of args of an exact type)
Expand Down
9 changes: 8 additions & 1 deletion datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,13 @@ false false true true
true true false false
NULL NULL NULL NULL

# invalid inputs
statement error DataFusion error: Error during planning: No function matches the given name and argument types 'isnan\(\)'. You might need to add explicit type casts.
select isnan();

statement error DataFusion error: Error during planning: No function matches the given name and argument types 'isnan\(Int64, Int64, Int64\)'. You might need to add explicit type casts.
select isnan(1,2,3);

## iszero

# iszero scalar function
Expand Down Expand Up @@ -1858,7 +1865,7 @@ statement error Error during planning: No function matches the given name and ar
SELECT concat();

# error message for wrong function signature (Uniform: t args all from some common types)
statement error DataFusion error: Failed to coerce arguments for NULLIF
statement error DataFusion error: Error during planning: No function matches the given name and argument types 'nullif\(Int64\)'. You might need to add explicit type casts.
SELECT nullif(1);


Expand Down

0 comments on commit a9e5942

Please sign in to comment.