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

Allow creation of String arrays from &Option<&str> iterators #680

Merged
merged 2 commits into from
Aug 12, 2021
Merged
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
32 changes: 27 additions & 5 deletions arrow/src/array/array_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,26 @@ impl<OffsetSize: StringOffsetSizeTrait> GenericStringArray<OffsetSize> {
}
}

impl<'a, Ptr, OffsetSize: StringOffsetSizeTrait> FromIterator<&'a Option<Ptr>>
for GenericStringArray<OffsetSize>
where
Ptr: AsRef<str> + 'a,
{
/// Creates a [`GenericStringArray`] based on an iterator of `Option` references.
fn from_iter<I: IntoIterator<Item = &'a Option<Ptr>>>(iter: I) -> Self {
// Convert each owned Ptr into &str and wrap in an owned `Option`
let iter = iter.into_iter().map(|o| o.as_ref().map(|p| p.as_ref()));
// Build a `GenericStringArray` with the resulting iterator
iter.collect::<GenericStringArray<OffsetSize>>()
}
}

impl<'a, Ptr, OffsetSize: StringOffsetSizeTrait> FromIterator<Option<Ptr>>
for GenericStringArray<OffsetSize>
where
Ptr: AsRef<str>,
{
/// Creates a [`GenericStringArray`] based on an iterator of `Option`s
fn from_iter<I: IntoIterator<Item = Option<Ptr>>>(iter: I) -> Self {
let iter = iter.into_iter();
let (_, data_len) = iter.size_hint();
Expand Down Expand Up @@ -358,6 +373,7 @@ impl<T: StringOffsetSizeTrait> From<GenericListArray<T>> for GenericStringArray<

#[cfg(test)]
mod tests {

use crate::array::{ListBuilder, StringBuilder};

use super::*;
Expand Down Expand Up @@ -484,17 +500,23 @@ mod tests {

#[test]
fn test_string_array_from_iter() {
let data = vec![Some("hello"), None, Some("arrow")];
let data = [Some("hello"), None, Some("arrow")];
let data_vec = data.to_vec();
// from Vec<Option<&str>>
let array1 = StringArray::from(data.clone());
let array1 = StringArray::from(data_vec.clone());
// from Iterator<Option<&str>>
let array2: StringArray = data.clone().into_iter().collect();
let array2: StringArray = data_vec.clone().into_iter().collect();
// from Iterator<Option<String>>
let array3: StringArray =
data.into_iter().map(|x| x.map(|s| s.to_string())).collect();
let array3: StringArray = data_vec
.into_iter()
.map(|x| x.map(|s| s.to_string()))
.collect();
// from Iterator<&Option<&str>>
let array4: StringArray = data.iter().collect::<StringArray>();

assert_eq!(array1, array2);
assert_eq!(array2, array3);
assert_eq!(array3, array4);
}

#[test]
Expand Down