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

Add FixedSizeList::from_iter_primitive #2887

Merged
merged 1 commit into from
Oct 18, 2022
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
52 changes: 51 additions & 1 deletion arrow-array/src/array/fixed_size_list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// specific language governing permissions and limitations
// under the License.

use crate::{make_array, print_long_array, Array, ArrayAccessor, ArrayRef};
use crate::builder::{FixedSizeListBuilder, PrimitiveBuilder};
use crate::{
make_array, print_long_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType,
};
use arrow_data::ArrayData;
use arrow_schema::DataType;
use std::any::Any;
Expand Down Expand Up @@ -100,6 +103,53 @@ impl FixedSizeListArray {
const fn value_offset_at(&self, i: usize) -> i32 {
i as i32 * self.length
}

/// Creates a [`FixedSizeListArray`] from an iterator of primitive values
/// # Example
/// ```
/// # use arrow_array::FixedSizeListArray;
/// # use arrow_array::types::Int32Type;
///
/// let data = vec![
/// Some(vec![Some(0), Some(1), Some(2)]),
/// None,
/// Some(vec![Some(3), None, Some(5)]),
/// Some(vec![Some(6), Some(7), Some(45)]),
/// ];
/// let list_array = FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(data, 3);
/// println!("{:?}", list_array);
/// ```
pub fn from_iter_primitive<T, P, I>(iter: I, length: i32) -> Self
where
T: ArrowPrimitiveType,
P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
I: IntoIterator<Item = Option<P>>,
{
let l = length as usize;
let iter = iter.into_iter();
let size_hint = iter.size_hint().0;
let mut builder = FixedSizeListBuilder::with_capacity(
PrimitiveBuilder::<T>::with_capacity(size_hint * l),
length,
size_hint,
);

for i in iter {
match i {
Some(p) => {
for t in p {
builder.values().append_option(t);
}
builder.append(true);
}
None => {
builder.values().append_nulls(l);
builder.append(false)
}
}
}
builder.finish()
}
}

impl From<ArrayData> for FixedSizeListArray {
Expand Down