Skip to content
Merged
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
106 changes: 30 additions & 76 deletions datafusion/functions/src/unicode/substr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ use arrow::array::{
use arrow::buffer::ScalarBuffer;
use arrow::datatypes::DataType;
use datafusion_common::cast::as_int64_array;
use datafusion_common::{exec_err, plan_err, Result};
use datafusion_common::types::{
logical_int32, logical_int64, logical_string, NativeType,
};
use datafusion_common::{exec_err, Result};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
Coercion, ColumnarValue, Documentation, ScalarUDFImpl, Signature, TypeSignature,
TypeSignatureClass, Volatility,
};
use datafusion_macros::user_doc;

Expand All @@ -44,7 +48,7 @@ use datafusion_macros::user_doc;
| substr(Utf8("datafusion"),Int64(5),Int64(3)) |
+----------------------------------------------+
| fus |
+----------------------------------------------+
+----------------------------------------------+
```"#,
standard_argument(name = "str", prefix = "String"),
argument(
Expand All @@ -70,14 +74,30 @@ impl Default for SubstrFunc {

impl SubstrFunc {
pub fn new() -> Self {
let string = Coercion::new_exact(TypeSignatureClass::Native(logical_string()));
let int64 = Coercion::new_implicit(
TypeSignatureClass::Native(logical_int64()),
vec![TypeSignatureClass::Native(logical_int32())],
NativeType::Int64,
);
Self {
signature: Signature::user_defined(Volatility::Immutable)
.with_parameter_names(vec![
"str".to_string(),
"start_pos".to_string(),
"length".to_string(),
])
.expect("valid parameter names"),
signature: Signature::one_of(
vec![
TypeSignature::Coercible(vec![string.clone(), int64.clone()]),
TypeSignature::Coercible(vec![
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

string.clone(),
int64.clone(),
int64.clone(),
]),
],
Volatility::Immutable,
)
.with_parameter_names(vec![
Copy link
Contributor

Choose a reason for hiding this comment

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

would be even nice to refactor such blocks in future so param names and types put together like

f.withNames("str", "str_pos").withTpyes("string", "i64")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Raised for tracking: #18685

"str".to_string(),
"start_pos".to_string(),
"length".to_string(),
])
.expect("valid parameter names"),
aliases: vec![String::from("substring")],
}
}
Expand Down Expand Up @@ -112,72 +132,6 @@ impl ScalarUDFImpl for SubstrFunc {
&self.aliases
}

fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
if arg_types.len() < 2 || arg_types.len() > 3 {
return plan_err!(
"The {} function requires 2 or 3 arguments, but got {}.",
self.name(),
arg_types.len()
);
}
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::Dictionary(key_type, value_type) => {
if key_type.is_integer() {
match value_type.as_ref() {
DataType::Null => Ok(DataType::Utf8),
DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8 => Ok(*value_type.clone()),
_ => plan_err!(
"The first argument of the {} function can only be a string, but got {:?}.",
self.name(),
arg_types[0]
),
}
} else {
plan_err!(
"The first argument of the {} function can only be a string, but got {:?}.",
self.name(),
arg_types[0]
)
}
}
_ => plan_err!(
"The first argument of the {} function can only be a string, but got {:?}.",
self.name(),
arg_types[0]
)
}?;

if ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[1]) {
return plan_err!(
"The second argument of the {} function can only be an integer, but got {:?}.",
self.name(),
arg_types[1]
);
}

if arg_types.len() == 3
&& ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[2])
{
return plan_err!(
"The third argument of the {} function can only be an integer, but got {:?}.",
self.name(),
arg_types[2]
);
}

if arg_types.len() == 2 {
Ok(vec![first_data_type.to_owned(), DataType::Int64])
} else {
Ok(vec![
first_data_type.to_owned(),
DataType::Int64,
DataType::Int64,
])
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down
19 changes: 17 additions & 2 deletions datafusion/sqllogictest/test_files/functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,25 @@ 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
query T
SELECT substr(NULL, 1, 2)
----
NULL

query T
SELECT substr('alphabet', 1, NULL)
----
NULL

query T
SELECT substr('alphabet', NULL, 2)
----
NULL

statement error Function 'substr' failed to match any signature
SELECT substr(1, 3)

statement error The first argument of the substr function can only be a string, but got Int64
statement error Function 'substr' failed to match any signature
SELECT substr(1, 3, 4)

query T
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/named_arguments.slt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ SELECT substr("STR" => 'hello world', "start_pos" => 7);

# Error: wrong number of arguments
# This query provides only 1 argument but substr requires 2 or 3
query error DataFusion error: Error during planning: Execution error: Function 'substr' user-defined coercion failed with "Error during planning: The substr function requires 2 or 3 arguments, but got 1."
query error Function 'substr' failed to match any signature
SELECT substr(str => 'hello world');

#############
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/string/string_literal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ SELECT substr('Hello🌏世界', 5, 3)
----
o🌏世

statement error The first argument of the substr function can only be a string, but got Int64
statement error Function 'substr' failed to match any signature
SELECT substr(1, 3)

statement error The first argument of the substr function can only be a string, but got Int64
statement error Function 'substr' failed to match any signature
SELECT substr(1, 3, 4)

statement error Execution error: negative substring length not allowed
Expand Down