From 5781997ef49b7e456ecb94ea47aca66adb9aa79c Mon Sep 17 00:00:00 2001 From: Jonathan Chen Date: Tue, 29 Oct 2024 00:49:18 -0400 Subject: [PATCH 1/2] coerce substr --- datafusion/functions/src/unicode/substr.rs | 1 + datafusion/sqllogictest/test_files/functions.slt | 9 ++++----- .../sqllogictest/test_files/string/string_literal.slt | 6 ------ 3 files changed, 5 insertions(+), 11 deletions(-) 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..12b8593c6caf 100644 --- a/datafusion/sqllogictest/test_files/functions.slt +++ b/datafusion/sqllogictest/test_files/functions.slt @@ -193,11 +193,10 @@ 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 substr(123456, 3); +---- +3456 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..2bbff964bdc9 100644 --- a/datafusion/sqllogictest/test_files/string/string_literal.slt +++ b/datafusion/sqllogictest/test_files/string/string_literal.slt @@ -123,12 +123,6 @@ 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); From 7e4b6afc7a967044ca6aa256e4fc36dcde991aa9 Mon Sep 17 00:00:00 2001 From: Jonathan Chen Date: Tue, 29 Oct 2024 02:55:16 -0400 Subject: [PATCH 2/2] change test file --- datafusion/sqllogictest/test_files/functions.slt | 5 ----- datafusion/sqllogictest/test_files/string/string_literal.slt | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/datafusion/sqllogictest/test_files/functions.slt b/datafusion/sqllogictest/test_files/functions.slt index 12b8593c6caf..e8577a95633f 100644 --- a/datafusion/sqllogictest/test_files/functions.slt +++ b/datafusion/sqllogictest/test_files/functions.slt @@ -193,11 +193,6 @@ SELECT substr('alphabet', 3, CAST(NULL AS int)) ---- NULL -query T -select substr(123456, 3); ----- -3456 - 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 2bbff964bdc9..2c5944d61487 100644 --- a/datafusion/sqllogictest/test_files/string/string_literal.slt +++ b/datafusion/sqllogictest/test_files/string/string_literal.slt @@ -129,6 +129,11 @@ 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