Dandandan commented on a change in pull request #9654: URL: https://github.com/apache/arrow/pull/9654#discussion_r589762510
########## File path: rust/datafusion/src/physical_plan/string_expressions.rs ########## @@ -794,85 +525,57 @@ pub fn rtrim<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { } } -/// Extracts the substring of string starting at the start'th character, and extending for count characters if that is specified. (Same as substring(string from start for count).) -/// substr('alphabet', 3) = 'phabet' -/// substr('alphabet', 3, 2) = 'ph' -pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { - match args.len() { - 2 => { - let string_array = downcast_string_arg!(args[0], "string", T); - let start_array = downcast_arg!(args[1], "start", Int64Array); +/// Splits string at occurrences of delimiter and returns the n'th field (counting from one). +/// split_part('abc~@~def~@~ghi', '~@~', 2) = 'def' +pub fn split_part<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { + let string_array = downcast_string_arg!(args[0], "string", T); + let delimiter_array = downcast_string_arg!(args[1], "delimiter", T); + let n_array = downcast_arg!(args[2], "n", Int64Array); - let result = string_array - .iter() - .zip(start_array.iter()) - .map(|(string, start)| match (string, start) { - (None, _) => None, - (_, None) => None, - (Some(string), Some(start)) => { - if start <= 0 { - Some(string.to_string()) - } else { - let graphemes = string.graphemes(true).collect::<Vec<&str>>(); - let start_pos = start as usize - 1; - if graphemes.len() < start_pos { - Some("".to_string()) - } else { - Some(graphemes[start_pos..].concat()) - } - } + let result = string_array + .iter() + .zip(delimiter_array.iter()) + .zip(n_array.iter()) + .map(|((string, delimiter), n)| match (string, delimiter, n) { + (None, _, _) => Ok(None), Review comment: Here the same, could be `_ -> Ok(None)` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org