diff --git a/datafusion/expr/src/type_coercion/functions.rs b/datafusion/expr/src/type_coercion/functions.rs index 6836713d8016..c81d59267c7f 100644 --- a/datafusion/expr/src/type_coercion/functions.rs +++ b/datafusion/expr/src/type_coercion/functions.rs @@ -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}" + ); + } } } diff --git a/datafusion/sqllogictest/test_files/expr.slt b/datafusion/sqllogictest/test_files/expr.slt index c653113fd438..b2df1cdb40ba 100644 --- a/datafusion/sqllogictest/test_files/expr.slt +++ b/datafusion/sqllogictest/test_files/expr.slt @@ -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') +---- +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('') ---- diff --git a/datafusion/sqllogictest/test_files/scalar.slt b/datafusion/sqllogictest/test_files/scalar.slt index fe7d1a90c5bd..1ab5444db1a3 100644 --- a/datafusion/sqllogictest/test_files/scalar.slt +++ b/datafusion/sqllogictest/test_files/scalar.slt @@ -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'); ----