alamb commented on code in PR #9973:
URL: https://github.com/apache/arrow-datafusion/pull/9973#discussion_r1554927300
##########
datafusion/functions/src/unicode/substrindex.rs:
##########
@@ -101,38 +101,154 @@ pub fn substr_index<T: OffsetSizeTrait>(args:
&[ArrayRef]) -> Result<ArrayRef> {
let delimiter_array = as_generic_string_array::<T>(&args[1])?;
let count_array = as_int64_array(&args[2])?;
- let result = string_array
+ let mut builder = StringBuilder::new();
+ string_array
.iter()
.zip(delimiter_array.iter())
.zip(count_array.iter())
- .map(|((string, delimiter), n)| match (string, delimiter, n) {
- (Some(string), Some(delimiter), Some(n)) => {
+ .for_each(|((string, delimiter), n)| {
+ if let (Some(string), Some(delimiter), Some(n)) = (string,
delimiter, n) {
// In MySQL, these cases will return an empty string.
if n == 0 || string.is_empty() || delimiter.is_empty() {
- return Some(String::new());
+ builder.append_value("");
+ return;
}
- let splitted: Box<dyn Iterator<Item = _>> = if n > 0 {
- Box::new(string.split(delimiter))
+ let occurrences =
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
+ let length = if n > 0 {
+ let splitted = string.split(delimiter);
+ splitted
+ .take(occurrences)
+ .map(|s| s.len() + delimiter.len())
+ .sum::<usize>()
+ - delimiter.len()
} else {
- Box::new(string.rsplit(delimiter))
+ let splitted = string.rsplit(delimiter);
+ splitted
+ .take(occurrences)
+ .map(|s| s.len() + delimiter.len())
+ .sum::<usize>()
+ - delimiter.len()
};
- let occurrences =
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
- // The length of the substring covered by substr_index.
- let length = splitted
- .take(occurrences) // at least 1 element, since n != 0
- .map(|s| s.len() + delimiter.len())
- .sum::<usize>()
- - delimiter.len();
if n > 0 {
- Some(string[..length].to_owned())
+ if let Some(substring) = string.get(..length) {
+ builder.append_value(substring);
+ }
} else {
- Some(string[string.len() - length..].to_owned())
+ #[allow(clippy::collapsible_else_if)]
+ if let Some(substring) =
+ string.get(string.len().saturating_sub(length)..)
+ {
+ builder.append_value(substring);
+ }
}
}
- _ => None,
- })
- .collect::<GenericStringArray<T>>();
+ });
+
+ if builder.is_empty() {
+ builder.append_null();
Review Comment:
I think append null needs to be called on each row (not just once at the
end) -- this will work fine for tests that only use a single row array (like
these unit tests) but likely won't work for arrays with multiple rows
It might be useful to add some tests here for NULL inputs :
https://github.com/apache/arrow-datafusion/blob/1a002bccd420ff91ec149ee1ba9c42061510f906/datafusion/sqllogictest/test_files/functions.slt#L939-L954
##########
datafusion/functions/src/unicode/substrindex.rs:
##########
@@ -101,38 +101,154 @@ pub fn substr_index<T: OffsetSizeTrait>(args:
&[ArrayRef]) -> Result<ArrayRef> {
let delimiter_array = as_generic_string_array::<T>(&args[1])?;
let count_array = as_int64_array(&args[2])?;
- let result = string_array
+ let mut builder = StringBuilder::new();
+ string_array
.iter()
.zip(delimiter_array.iter())
.zip(count_array.iter())
- .map(|((string, delimiter), n)| match (string, delimiter, n) {
- (Some(string), Some(delimiter), Some(n)) => {
+ .for_each(|((string, delimiter), n)| {
+ if let (Some(string), Some(delimiter), Some(n)) = (string,
delimiter, n) {
// In MySQL, these cases will return an empty string.
if n == 0 || string.is_empty() || delimiter.is_empty() {
- return Some(String::new());
+ builder.append_value("");
+ return;
}
- let splitted: Box<dyn Iterator<Item = _>> = if n > 0 {
- Box::new(string.split(delimiter))
+ let occurrences =
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
+ let length = if n > 0 {
+ let splitted = string.split(delimiter);
+ splitted
+ .take(occurrences)
+ .map(|s| s.len() + delimiter.len())
+ .sum::<usize>()
+ - delimiter.len()
} else {
- Box::new(string.rsplit(delimiter))
+ let splitted = string.rsplit(delimiter);
+ splitted
+ .take(occurrences)
+ .map(|s| s.len() + delimiter.len())
+ .sum::<usize>()
+ - delimiter.len()
};
- let occurrences =
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
- // The length of the substring covered by substr_index.
- let length = splitted
- .take(occurrences) // at least 1 element, since n != 0
- .map(|s| s.len() + delimiter.len())
- .sum::<usize>()
- - delimiter.len();
if n > 0 {
- Some(string[..length].to_owned())
+ if let Some(substring) = string.get(..length) {
Review Comment:
For each row here, I think you either need to call `append_value` or
`append_null` -- the code as I read it will not call `append_null` for non
matching rows. I'll suggest some more tests below
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]