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
2 changes: 1 addition & 1 deletion datafusion/core/tests/sqllogictests/test_files/errors.slt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ WITH HEADER ROW
LOCATION '../../testing/data/csv/aggregate_test_100.csv'

# csv_query_error
statement error Error during planning: Coercion from \[Utf8\] to the signature Uniform\(1, \[Float64, Float32\]\) failed\.
statement error DataFusion error: Error during planning: No function matches the given name and argument types 'sin\(Utf8\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tsin\(Float64/Float32\)
SELECT sin(c1) FROM aggregate_test_100

# cast_expressions_error
Expand Down
20 changes: 20 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,23 @@ NULL NULL NULL NULL 8 15.625

statement ok
drop table test

# error message for wrong function signature (Variadic: arbitrary number of args all from some common types)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

statement error Error during planning: No function matches the given name and argument types 'concat\(\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tconcat\(Utf8, ..\)
SELECT concat();

# error message for wrong function signature (Uniform: t args all from some common types)
statement error Error during planning: No function matches the given name and argument types 'nullif\(Int64\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tnullif\(Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8, Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8\)
SELECT nullif(1);

# error message for wrong function signature (Exact: exact number of args of an exact type)
statement error Error during planning: No function matches the given name and argument types 'pi\(Float64\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tpi\(\)
SELECT pi(3.14);

# error message for wrong function signature (Any: fixed number of args of arbitrary types)
statement error Error during planning: No function matches the given name and argument types 'arrowtypeof\(Int64, Int64\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tarrowtypeof\(Any\)
SELECT arrow_typeof(1, 1);

# error message for wrong function signature (OneOf: fixed number of args of arbitrary types)
statement error Error during planning: No function matches the given name and argument types 'power\(Int64, Int64, Int64\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tpower\(Int64, Int64\)\n\tpower\(Float64, Float64\)
SELECT power(1, 2, 3);
10 changes: 7 additions & 3 deletions datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

//! Function module contains typing and signature for built-in and user defined functions.

use crate::function_err::generate_signature_error_msg;
use crate::nullif::SUPPORTED_NULLIF_TYPES;
use crate::type_coercion::functions::data_types;
use crate::ColumnarValue;
Expand Down Expand Up @@ -100,13 +101,16 @@ pub fn return_type(
// or the execution panics.

if input_expr_types.is_empty() && !fun.supports_zero_argument() {
return Err(DataFusionError::Internal(format!(
"Builtin scalar function {fun} does not support empty arguments"
return Err(DataFusionError::Plan(generate_signature_error_msg(
fun,
input_expr_types,
)));
}

// verify that this is a valid set of data types for this function
data_types(input_expr_types, &signature(fun))?;
data_types(input_expr_types, &signature(fun)).map_err(|_| {
DataFusionError::Plan(generate_signature_error_msg(fun, input_expr_types))
})?;

// the return type of the built in function.
// Some built-in functions' return type depends on the incoming type.
Expand Down
91 changes: 91 additions & 0 deletions datafusion/expr/src/function_err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.

//! Function_err module enhances frontend error messages for unresolved functions due to incorrect parameters,
//! by providing the correct function signatures.
//!
//! For example, a query like `select round(3.14, 1.1);` would yield:
//! ```text
//! Error during planning: No function matches 'round(Float64, Float64)'. You might need to add explicit type casts.
//! Candidate functions:
//! round(Float64, Int64)
//! round(Float32, Int64)
//! round(Float64)
//! round(Float32)
//! ```

use crate::function::signature;
use crate::{BuiltinScalarFunction, TypeSignature};
use arrow::datatypes::DataType;

impl TypeSignature {
fn to_string_repr(&self) -> Vec<String> {
match self {
TypeSignature::Variadic(types) => {
vec![format!("{}, ..", join_types(types, "/"))]
}
TypeSignature::Uniform(arg_count, valid_types) => {
vec![std::iter::repeat(join_types(valid_types, "/"))
.take(*arg_count)
.collect::<Vec<String>>()
.join(", ")]
}
TypeSignature::Exact(types) => {
vec![join_types(types, ", ")]
}
TypeSignature::Any(arg_count) => {
vec![std::iter::repeat("Any")
.take(*arg_count)
.collect::<Vec<&str>>()
.join(", ")]
}
TypeSignature::VariadicEqual => vec!["T, .., T".to_string()],
TypeSignature::VariadicAny => vec!["Any, .., Any".to_string()],
TypeSignature::OneOf(sigs) => {
sigs.iter().flat_map(|s| s.to_string_repr()).collect()
}
}
}
}

/// Helper function to join types with specified delimiter.
fn join_types<T: std::fmt::Display>(types: &[T], delimiter: &str) -> String {
types
.iter()
.map(|t| t.to_string())
.collect::<Vec<String>>()
.join(delimiter)
}

/// Creates a detailed error message for a function with wrong signature.
pub fn generate_signature_error_msg(
fun: &BuiltinScalarFunction,
input_expr_types: &[DataType],
) -> String {
let candidate_signatures = signature(fun)
.type_signature
.to_string_repr()
.iter()
.map(|args_str| format!("\t{}({})", fun, args_str))
.collect::<Vec<String>>()
.join("\n");

format!(
"No function matches the given name and argument types '{}({})'. You might need to add explicit type casts.\n\tCandidate functions:\n{}",
fun, join_types(input_expr_types, ", "), candidate_signatures
)
}
1 change: 1 addition & 0 deletions datafusion/expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod expr_rewriter;
pub mod expr_schema;
pub mod field_util;
pub mod function;
mod function_err;
mod literal;
pub mod logical_plan;
mod nullif;
Expand Down
9 changes: 4 additions & 5 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2750,11 +2750,10 @@ mod tests {
"Builtin scalar function {fun} does not support empty arguments"
)));
}
Err(DataFusionError::Internal(err)) => {
if err
!= format!(
"Builtin scalar function {fun} does not support empty arguments"
) {
Err(DataFusionError::Plan(err)) => {
if !err
.contains("No function matches the given name and argument types")
{
return Err(DataFusionError::Internal(format!(
"Builtin scalar function {fun} didn't got the right error message with empty arguments")));
}
Expand Down