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

[fix]: Coerce integer values for substr #13154

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions datafusion/functions/src/unicode/substr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

DataType::UInt64, DataType::UInt32 as well?
What about any type that can be coerced to Utf8?

Copy link
Contributor

Choose a reason for hiding this comment

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

I also forget why this function needs custom coercion logic. I feel like we should be able to do this with a Signature as the same logic could/should apply to any function that takes a string as argument 🤔

DataType::Dictionary(key_type, value_type) => {
if key_type.is_integer() {
match value_type.as_ref() {
Expand Down
6 changes: 0 additions & 6 deletions datafusion/sqllogictest/test_files/functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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')
----
Expand Down
11 changes: 5 additions & 6 deletions datafusion/sqllogictest/test_files/string/string_literal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down