Skip to content

Conversation

@2010YOUY01
Copy link
Contributor

Which issue does this PR close?

#6396

Rationale for this change

Currently, the error message for the built-in scalar function signature mismatch is not straightforward.
The error message before and after this change is shown below:
(There are 5 different function signature types(variadic, exact...) from internal implementation, 1 function is chosen from each type)

[BEFORE]DataFusion CLI v25.0.0
❯ select concat();
Internal error: Builtin scalar function concat does not support empty arguments. This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker
❯ SELECT nullif(1);
Error during planning: Coercion from [Int64] to the signature Uniform(2, [Boolean, UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, Float64, Utf8, LargeUtf8]) failed.
❯ SELECT pi(3.14);
Error during planning: Coercion from [Float64] to the signature Exact([]) failed.
❯ SELECT arrow_typeof(1, 1);
Error during planning: The function expected 1 arguments but received 2
❯ SELECT power('1', '2');
Error during planning: Coercion from [Utf8, Utf8] to the signature OneOf([Exact([Int64, Int64]), Exact([Float64, Float64])]) failed.
[AFTER]DataFusion CLI v25.0.0
❯ select concat();
Error during planning: No function matches the given name and argument types 'concat()'. You might need to add explicit type casts.
        Candidate functions:
        concat(Utf8, ..)
❯ SELECT nullif(1);
Error during planning: No function matches the given name and argument types 'nullif(Int64)'. You might need to add explicit type casts.
        Candidate functions:
        nullif(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 pi(3.14);
Error during planning: No function matches the given name and argument types 'pi(Float64)'. You might need to add explicit type casts.
        Candidate functions:
        pi()
❯ SELECT arrow_typeof(1, 1);
Error during planning: No function matches the given name and argument types 'arrowtypeof(Int64, Int64)'. You might need to add explicit type casts.
        Candidate functions:
        arrowtypeof(Any)
❯ SELECT power('1', '2');
Error during planning: No function matches the given name and argument types 'power(Utf8, Utf8)'. You might need to add explicit type casts.
        Candidate functions:
        power(Int64, Int64)
        power(Float64, Float64)

What changes are included in this PR?

  1. Add a function_err.rs to generate error messages by providing candidate function signatures if input function arguments are not valid.
  2. Before this change, if functions have wrong number of args or input args can't be coerced into valid signatures, it will return different error messages, now they'll all report the error in this way.

Are these changes tested?

There are 5 function signature types from internal implementation , remaining 2 of them are not used when defining built-in scalar functions.
1 of each signature type has an end-to-end sqllogictest for the error message.

Are there any user-facing changes?

No.

@github-actions github-actions bot added core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) labels May 22, 2023
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

This looks really nice @2010YOUY01 -- thank you so much. I have also noticed the hard to understand error messages and this looks like a great improvement ❤️

fn join_types<T: std::fmt::Debug>(types: &[T], delimiter: &str) -> String {
types
.iter()
.map(|t| format!("{:?}", t))
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest we use the Display impl here to better follow the Rust convention of Display for users and Debug for developers.

Suggested change
.map(|t| format!("{:?}", t))
.map(|t| t.to_string())

However, under the covers that simply calls the Debug impl 🤦 so it won't make any practical difference

https://docs.rs/arrow-schema/40.0.0/src/arrow_schema/datatype.rs.html#307

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Updated.

@alamb
Copy link
Contributor

alamb commented May 22, 2023

I'll plan to merge this tomorrow unless there are more comments or anyone wants more time to review

Copy link
Member

@jackwener jackwener left a comment

Choose a reason for hiding this comment

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

Make sense to me, thank you @2010YOUY01

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.

👍

@jackwener jackwener merged commit 84d0692 into apache:main May 24, 2023
@alamb
Copy link
Contributor

alamb commented May 24, 2023

Thanks @jackwener

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants