Skip to content
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

feat: Add implicit casting to TypeSignature::String #13404

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 19 additions & 9 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,25 @@ fn get_valid_types(
let mut new_types = Vec::with_capacity(current_types.len());
for data_type in current_types.iter() {
let logical_data_type: NativeType = data_type.into();
if logical_data_type == NativeType::String {
new_types.push(data_type.to_owned());
} else if logical_data_type == NativeType::Null {
// TODO: Switch to Utf8View if all the string functions supports Utf8View
new_types.push(DataType::Utf8);
} else {
return plan_err!(
"The signature expected NativeType::String but received {logical_data_type}"
);
match logical_data_type {
NativeType::String => {
new_types.push(data_type.to_owned());
}
// Allow implicit casting
NativeType::Null
| NativeType::Int32
| NativeType::Int64
| NativeType::Float32
| NativeType::Float64
| NativeType::Boolean => {
// TODO: Switch to Utf8View if all the string functions supports Utf8View
new_types.push(DataType::Utf8);
}
_ => {
return plan_err!(
"The signature expected NativeType::String but received {logical_data_type}"
);
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,42 @@ SELECT ltrim(NULL, 'xyz')
----
NULL

# implicit casting with TypeSignature test
query T
SELECT ltrim(NULL, NULL)
----
NULL

query T
SELECT ltrim(12345, '1')
Copy link
Contributor

@jayzhan211 jayzhan211 Nov 14, 2024

Choose a reason for hiding this comment

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

DuckDB doesn't allow this query 🤔
Numeric string casting is quite confusing to me

D SELECT ltrim(12345, '1');
Binder Error: No function matches the given name and argument types 'ltrim(INTEGER_LITERAL, STRING_LITERAL)'. You might need to add explicit type casts.
	Candidate functions:
	ltrim(VARCHAR) -> VARCHAR
	ltrim(VARCHAR, VARCHAR) -> VARCHAR

LINE 1: SELECT ltrim(12345, '1');

Same with postgres

postgres=# SELECT ltrim(12345, '1');
ERROR:  function ltrim(integer, unknown) does not exist
LINE 1: SELECT ltrim(12345, '1');
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

----
2345

query T
SELECT ltrim(10020, '0')
----
10020

query T
SELECT ltrim(12.345, '12')
----
.345

query T
SELECT ltrim(0.00123, '0.')
----
123

query T
SELECT ltrim(true, 't')
----
rue

query T
SELECT ltrim(false, 'f')
----
alse

query I
SELECT octet_length('')
----
Expand Down
3 changes: 0 additions & 3 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1940,9 +1940,6 @@ select position('' in '')
----
1

query error DataFusion error: Error during planning: Error during planning: The signature expected NativeType::String but received NativeType::Int64
select position(1 in 1)

query I
select strpos('abc', 'c');
----
Expand Down