diff --git a/datafusion/functions/src/unicode/substr.rs b/datafusion/functions/src/unicode/substr.rs index 5a8c2500900b..1df33542e1da 100644 --- a/datafusion/functions/src/unicode/substr.rs +++ b/datafusion/functions/src/unicode/substr.rs @@ -94,6 +94,7 @@ impl ScalarUDFImpl for SubstrFunc { let first_data_type = match &arg_types[0] { DataType::Null => Ok(DataType::Utf8), DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8 => Ok(arg_types[0].clone()), + DataType::Int64 | DataType::Int32 => Ok(DataType::Utf8), // Allows for first arg to be int DataType::Dictionary(key_type, value_type) => { if key_type.is_integer() { match value_type.as_ref() { diff --git a/datafusion/sqllogictest/test_files/functions.slt b/datafusion/sqllogictest/test_files/functions.slt index 5b6017b08a00..e8577a95633f 100644 --- a/datafusion/sqllogictest/test_files/functions.slt +++ b/datafusion/sqllogictest/test_files/functions.slt @@ -193,12 +193,6 @@ SELECT substr('alphabet', 3, CAST(NULL AS int)) ---- NULL -statement error The first argument of the substr function can only be a string, but got Int64 -SELECT substr(1, 3) - -statement error The first argument of the substr function can only be a string, but got Int64 -SELECT substr(1, 3, 4) - query T SELECT translate('12345', '143', 'ax') ---- diff --git a/datafusion/sqllogictest/test_files/string/string_literal.slt b/datafusion/sqllogictest/test_files/string/string_literal.slt index 5d847747693d..2c5944d61487 100644 --- a/datafusion/sqllogictest/test_files/string/string_literal.slt +++ b/datafusion/sqllogictest/test_files/string/string_literal.slt @@ -123,18 +123,17 @@ SELECT substr('Hello🌏世界', 5, 3) ---- o🌏世 -statement error The first argument of the substr function can only be a string, but got Int64 -SELECT substr(1, 3) - -statement error The first argument of the substr function can only be a string, but got Int64 -SELECT substr(1, 3, 4) - statement error Execution error: negative substring length not allowed select substr(arrow_cast('foo', 'Utf8View'), 1, -1); statement error Execution error: negative substring length not allowed select substr('', 1, -1); +query T +select substr(123456, 3); +---- +3456 + # StringView scalar to StringView scalar query BBBB