tlm365 commented on code in PR #14020: URL: https://github.com/apache/datafusion/pull/14020#discussion_r1904127737
########## datafusion/functions/src/unicode/find_in_set.rs: ########## @@ -138,31 +138,144 @@ fn find_in_set(args: &[ArrayRef]) -> Result<ArrayRef> { } } -pub fn find_in_set_general<'a, T: ArrowPrimitiveType, V: ArrayAccessor<Item = &'a str>>( +pub fn find_in_set_general<'a, T, V>( string_array: V, str_list_array: V, ) -> Result<ArrayRef> where + T: ArrowPrimitiveType, T::Native: OffsetSizeTrait, + V: ArrayAccessor<Item = &'a str>, { let string_iter = ArrayIter::new(string_array); let str_list_iter = ArrayIter::new(str_list_array); - let result = string_iter + + let mut builder = PrimitiveArray::<T>::builder(string_iter.len()); + + string_iter .zip(str_list_iter) - .map(|(string, str_list)| match (string, str_list) { - (Some(string), Some(str_list)) => { - let mut res = 0; - let str_set: Vec<&str> = str_list.split(',').collect(); - for (idx, str) in str_set.iter().enumerate() { - if str == &string { - res = idx + 1; - break; - } + .for_each( + |(string_opt, str_list_opt)| match (string_opt, str_list_opt) { + (Some(string), Some(str_list)) => { + let position = str_list + .split(',') + .position(|s| s == string) + .map_or(0, |idx| idx + 1); + builder.append_value(T::Native::from_usize(position).unwrap()); Review Comment: @jayzhan211 Thanks for reviewing, > Probably faster if create Vec first and use PrimitiveArray::from() I have tried implementing it this way, it seems to have similar performance to this PR. Here is the code I have tested: ```rust pub fn find_in_set_general<'a, T, V>( string_array: V, str_list_array: V, ) -> Result<ArrayRef> where T: ArrowPrimitiveType, T::Native: OffsetSizeTrait, V: StringArrayType<'a, Item = &'a str>, PrimitiveArray<T>: From<Vec<Option<<T as ArrowPrimitiveType>::Native>>>, { let string_iter = ArrayIter::new(string_array); let str_list_iter = ArrayIter::new(str_list_array); let mut result: Vec<Option<T::Native>> = Vec::with_capacity(string_iter.len()); string_iter .zip(str_list_iter) .for_each( |(string_opt, str_list_opt)| match (string_opt, str_list_opt) { (Some(string), Some(str_list)) => { let position = str_list .split(',') .position(|s| s == string) .map_or(0, |idx| idx + 1); let value = T::Native::from_usize(position).unwrap(); result.push(Some(value)); } _ => result.push(None), }, ); let array = PrimitiveArray::<T>::from(result); Ok(Arc::new(array) as ArrayRef) } ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org