Path: blob/main/crates/polars-arrow/src/array/static_array_collect.rs
6939 views
use std::borrow::Cow;12use polars_utils::no_call_const;34use crate::array::static_array::{ParameterFreeDtypeStaticArray, StaticArray};5use crate::array::{6Array, BinaryArray, BinaryViewArray, BooleanArray, FixedSizeListArray, ListArray,7MutableBinaryArray, MutableBinaryValuesArray, MutableBinaryViewArray, PrimitiveArray,8StructArray, Utf8Array, Utf8ViewArray,9};10use crate::bitmap::BitmapBuilder;11use crate::datatypes::ArrowDataType;12#[cfg(feature = "dtype-array")]13use crate::legacy::prelude::fixed_size_list::AnonymousBuilder as AnonymousFixedSizeListArrayBuilder;14use crate::legacy::prelude::list::AnonymousBuilder as AnonymousListArrayBuilder;15use crate::legacy::trusted_len::TrustedLenPush;16use crate::trusted_len::TrustedLen;17use crate::types::NativeType;1819pub trait ArrayFromIterDtype<T>: Sized {20fn arr_from_iter_with_dtype<I: IntoIterator<Item = T>>(dtype: ArrowDataType, iter: I) -> Self;2122#[inline(always)]23fn arr_from_iter_trusted_with_dtype<I>(dtype: ArrowDataType, iter: I) -> Self24where25I: IntoIterator<Item = T>,26I::IntoIter: TrustedLen,27{28Self::arr_from_iter_with_dtype(dtype, iter)29}3031fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<T, E>>>(32dtype: ArrowDataType,33iter: I,34) -> Result<Self, E>;3536#[inline(always)]37fn try_arr_from_iter_trusted_with_dtype<E, I>(dtype: ArrowDataType, iter: I) -> Result<Self, E>38where39I: IntoIterator<Item = Result<T, E>>,40I::IntoIter: TrustedLen,41{42Self::try_arr_from_iter_with_dtype(dtype, iter)43}44}4546pub trait ArrayFromIter<T>: Sized {47fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self;4849#[inline(always)]50fn arr_from_iter_trusted<I>(iter: I) -> Self51where52I: IntoIterator<Item = T>,53I::IntoIter: TrustedLen,54{55Self::arr_from_iter(iter)56}5758fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E>;5960#[inline(always)]61fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>62where63I: IntoIterator<Item = Result<T, E>>,64I::IntoIter: TrustedLen,65{66Self::try_arr_from_iter(iter)67}68}6970impl<T, A: ParameterFreeDtypeStaticArray + ArrayFromIter<T>> ArrayFromIterDtype<T> for A {71#[inline(always)]72fn arr_from_iter_with_dtype<I: IntoIterator<Item = T>>(dtype: ArrowDataType, iter: I) -> Self {73// FIXME: currently some Object arrays have Unknown dtype, when this is fixed remove this bypass.74if dtype != ArrowDataType::Unknown {75debug_assert_eq!(76std::mem::discriminant(&dtype),77std::mem::discriminant(&A::get_dtype())78);79}80Self::arr_from_iter(iter)81}8283#[inline(always)]84fn arr_from_iter_trusted_with_dtype<I>(dtype: ArrowDataType, iter: I) -> Self85where86I: IntoIterator<Item = T>,87I::IntoIter: TrustedLen,88{89// FIXME: currently some Object arrays have Unknown dtype, when this is fixed remove this bypass.90if dtype != ArrowDataType::Unknown {91debug_assert_eq!(92std::mem::discriminant(&dtype),93std::mem::discriminant(&A::get_dtype())94);95}96Self::arr_from_iter_trusted(iter)97}9899#[inline(always)]100fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<T, E>>>(101dtype: ArrowDataType,102iter: I,103) -> Result<Self, E> {104// FIXME: currently some Object arrays have Unknown dtype, when this is fixed remove this bypass.105if dtype != ArrowDataType::Unknown {106debug_assert_eq!(107std::mem::discriminant(&dtype),108std::mem::discriminant(&A::get_dtype())109);110}111Self::try_arr_from_iter(iter)112}113114#[inline(always)]115fn try_arr_from_iter_trusted_with_dtype<E, I>(dtype: ArrowDataType, iter: I) -> Result<Self, E>116where117I: IntoIterator<Item = Result<T, E>>,118I::IntoIter: TrustedLen,119{120// FIXME: currently some Object arrays have Unknown dtype, when this is fixed remove this bypass.121if dtype != ArrowDataType::Unknown {122debug_assert_eq!(123std::mem::discriminant(&dtype),124std::mem::discriminant(&A::get_dtype())125);126}127Self::try_arr_from_iter_trusted(iter)128}129}130131pub trait ArrayCollectIterExt<A: StaticArray>: Iterator + Sized {132#[inline(always)]133fn collect_arr(self) -> A134where135A: ArrayFromIter<Self::Item>,136{137A::arr_from_iter(self)138}139140#[inline(always)]141fn collect_arr_trusted(self) -> A142where143A: ArrayFromIter<Self::Item>,144Self: TrustedLen,145{146A::arr_from_iter_trusted(self)147}148149#[inline(always)]150fn try_collect_arr<U, E>(self) -> Result<A, E>151where152A: ArrayFromIter<U>,153Self: Iterator<Item = Result<U, E>>,154{155A::try_arr_from_iter(self)156}157158#[inline(always)]159fn try_collect_arr_trusted<U, E>(self) -> Result<A, E>160where161A: ArrayFromIter<U>,162Self: Iterator<Item = Result<U, E>> + TrustedLen,163{164A::try_arr_from_iter_trusted(self)165}166167#[inline(always)]168fn collect_arr_with_dtype(self, dtype: ArrowDataType) -> A169where170A: ArrayFromIterDtype<Self::Item>,171{172A::arr_from_iter_with_dtype(dtype, self)173}174175#[inline(always)]176fn collect_arr_trusted_with_dtype(self, dtype: ArrowDataType) -> A177where178A: ArrayFromIterDtype<Self::Item>,179Self: TrustedLen,180{181A::arr_from_iter_trusted_with_dtype(dtype, self)182}183184#[inline(always)]185fn try_collect_arr_with_dtype<U, E>(self, dtype: ArrowDataType) -> Result<A, E>186where187A: ArrayFromIterDtype<U>,188Self: Iterator<Item = Result<U, E>>,189{190A::try_arr_from_iter_with_dtype(dtype, self)191}192193#[inline(always)]194fn try_collect_arr_trusted_with_dtype<U, E>(self, dtype: ArrowDataType) -> Result<A, E>195where196A: ArrayFromIterDtype<U>,197Self: Iterator<Item = Result<U, E>> + TrustedLen,198{199A::try_arr_from_iter_trusted_with_dtype(dtype, self)200}201}202203impl<A: StaticArray, I: Iterator> ArrayCollectIterExt<A> for I {}204205// ---------------206// Implementations207// ---------------208209impl<T: NativeType> ArrayFromIter<T> for PrimitiveArray<T> {210#[inline]211fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {212PrimitiveArray::from_vec(iter.into_iter().collect())213}214215#[inline]216fn arr_from_iter_trusted<I>(iter: I) -> Self217where218I: IntoIterator<Item = T>,219I::IntoIter: TrustedLen,220{221PrimitiveArray::from_vec(Vec::from_trusted_len_iter(iter))222}223224#[inline]225fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E> {226let v: Result<Vec<T>, E> = iter.into_iter().collect();227Ok(PrimitiveArray::from_vec(v?))228}229230#[inline]231fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>232where233I: IntoIterator<Item = Result<T, E>>,234I::IntoIter: TrustedLen,235{236let v = Vec::try_from_trusted_len_iter(iter);237Ok(PrimitiveArray::from_vec(v?))238}239}240241impl<T: NativeType> ArrayFromIter<Option<T>> for PrimitiveArray<T> {242fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {243let iter = iter.into_iter();244let n = iter.size_hint().0;245let mut buf = Vec::with_capacity(n);246let mut validity = BitmapBuilder::with_capacity(n);247unsafe {248for val in iter {249// Use one check for both capacities.250if buf.len() == buf.capacity() {251buf.reserve(1);252validity.reserve(buf.capacity() - buf.len());253}254buf.push_unchecked(val.unwrap_or_default());255validity.push_unchecked(val.is_some());256}257}258PrimitiveArray::new(259T::PRIMITIVE.into(),260buf.into(),261validity.into_opt_validity(),262)263}264265fn arr_from_iter_trusted<I>(iter: I) -> Self266where267I: IntoIterator<Item = Option<T>>,268I::IntoIter: TrustedLen,269{270let iter = iter.into_iter();271let n = iter.size_hint().1.expect("must have an upper bound");272let mut buf = Vec::with_capacity(n);273let mut validity = BitmapBuilder::with_capacity(n);274unsafe {275for val in iter {276buf.push_unchecked(val.unwrap_or_default());277validity.push_unchecked(val.is_some());278}279}280PrimitiveArray::new(281T::PRIMITIVE.into(),282buf.into(),283validity.into_opt_validity(),284)285}286287fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(288iter: I,289) -> Result<Self, E> {290let iter = iter.into_iter();291let n = iter.size_hint().0;292let mut buf = Vec::with_capacity(n);293let mut validity = BitmapBuilder::with_capacity(n);294unsafe {295for val in iter {296let val = val?;297// Use one check for both capacities.298if buf.len() == buf.capacity() {299buf.reserve(1);300validity.reserve(buf.capacity() - buf.len());301}302buf.push_unchecked(val.unwrap_or_default());303validity.push_unchecked(val.is_some());304}305}306Ok(PrimitiveArray::new(307T::PRIMITIVE.into(),308buf.into(),309validity.into_opt_validity(),310))311}312313fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>314where315I: IntoIterator<Item = Result<Option<T>, E>>,316I::IntoIter: TrustedLen,317{318let iter = iter.into_iter();319let n = iter.size_hint().1.expect("must have an upper bound");320let mut buf = Vec::with_capacity(n);321let mut validity = BitmapBuilder::with_capacity(n);322unsafe {323for val in iter {324let val = val?;325buf.push_unchecked(val.unwrap_or_default());326validity.push_unchecked(val.is_some());327}328}329Ok(PrimitiveArray::new(330T::PRIMITIVE.into(),331buf.into(),332validity.into_opt_validity(),333))334}335}336337// We don't use AsRef here because it leads to problems with conflicting implementations,338// as Rust considers that AsRef<[u8]> for Option<&[u8]> could be implemented.339trait IntoBytes {340type AsRefT: AsRef<[u8]>;341fn into_bytes(self) -> Self::AsRefT;342}343trait TrivialIntoBytes: AsRef<[u8]> {}344impl<T: TrivialIntoBytes> IntoBytes for T {345type AsRefT = Self;346fn into_bytes(self) -> Self {347self348}349}350impl TrivialIntoBytes for Vec<u8> {}351impl TrivialIntoBytes for Cow<'_, [u8]> {}352impl TrivialIntoBytes for &[u8] {}353impl TrivialIntoBytes for String {}354impl TrivialIntoBytes for &str {}355impl<'a> IntoBytes for Cow<'a, str> {356type AsRefT = Cow<'a, [u8]>;357fn into_bytes(self) -> Cow<'a, [u8]> {358match self {359Cow::Borrowed(a) => Cow::Borrowed(a.as_bytes()),360Cow::Owned(s) => Cow::Owned(s.into_bytes()),361}362}363}364365impl<T: IntoBytes> ArrayFromIter<T> for BinaryArray<i64> {366fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {367BinaryArray::from_iter_values(iter.into_iter().map(|s| s.into_bytes()))368}369370fn arr_from_iter_trusted<I>(iter: I) -> Self371where372I: IntoIterator<Item = T>,373I::IntoIter: TrustedLen,374{375unsafe {376// SAFETY: our iterator is TrustedLen.377MutableBinaryArray::from_trusted_len_values_iter_unchecked(378iter.into_iter().map(|s| s.into_bytes()),379)380.into()381}382}383384fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E> {385// No built-in for this?386let mut arr = MutableBinaryValuesArray::new();387let mut iter = iter.into_iter();388arr.reserve(iter.size_hint().0, 0);389iter.try_for_each(|x| -> Result<(), E> {390arr.push(x?.into_bytes());391Ok(())392})?;393Ok(arr.into())394}395396// No faster implementation than this available, fall back to default.397// fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>398}399400impl<T: IntoBytes> ArrayFromIter<Option<T>> for BinaryArray<i64> {401#[inline]402fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {403BinaryArray::from_iter(iter.into_iter().map(|s| Some(s?.into_bytes())))404}405406#[inline]407fn arr_from_iter_trusted<I>(iter: I) -> Self408where409I: IntoIterator<Item = Option<T>>,410I::IntoIter: TrustedLen,411{412unsafe {413// SAFETY: the iterator is TrustedLen.414BinaryArray::from_trusted_len_iter_unchecked(415iter.into_iter().map(|s| Some(s?.into_bytes())),416)417}418}419420fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(421iter: I,422) -> Result<Self, E> {423// No built-in for this?424let mut arr = MutableBinaryArray::new();425let mut iter = iter.into_iter();426arr.reserve(iter.size_hint().0, 0);427iter.try_for_each(|x| -> Result<(), E> {428arr.push(x?.map(|s| s.into_bytes()));429Ok(())430})?;431Ok(arr.into())432}433434fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>435where436I: IntoIterator<Item = Result<Option<T>, E>>,437I::IntoIter: TrustedLen,438{439unsafe {440// SAFETY: the iterator is TrustedLen.441BinaryArray::try_from_trusted_len_iter_unchecked(442iter.into_iter().map(|s| s.map(|s| Some(s?.into_bytes()))),443)444}445}446}447448impl<T: IntoBytes> ArrayFromIter<T> for BinaryViewArray {449#[inline]450fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {451MutableBinaryViewArray::from_values_iter(iter.into_iter().map(|a| a.into_bytes())).into()452}453454#[inline]455fn arr_from_iter_trusted<I>(iter: I) -> Self456where457I: IntoIterator<Item = T>,458I::IntoIter: TrustedLen,459{460Self::arr_from_iter(iter)461}462463fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E> {464let mut iter = iter.into_iter();465let mut arr = MutableBinaryViewArray::with_capacity(iter.size_hint().0);466iter.try_for_each(|x| -> Result<(), E> {467arr.push_value_ignore_validity(x?.into_bytes());468Ok(())469})?;470Ok(arr.into())471}472473// No faster implementation than this available, fall back to default.474// fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>475}476477impl<T: IntoBytes> ArrayFromIter<Option<T>> for BinaryViewArray {478#[inline]479fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {480MutableBinaryViewArray::from_iter(481iter.into_iter().map(|opt_a| opt_a.map(|a| a.into_bytes())),482)483.into()484}485486#[inline]487fn arr_from_iter_trusted<I>(iter: I) -> Self488where489I: IntoIterator<Item = Option<T>>,490I::IntoIter: TrustedLen,491{492Self::arr_from_iter(iter)493}494495fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(496iter: I,497) -> Result<Self, E> {498let mut iter = iter.into_iter();499let mut arr = MutableBinaryViewArray::with_capacity(iter.size_hint().0);500iter.try_for_each(|x| -> Result<(), E> {501let x = x?;502arr.push(x.map(|x| x.into_bytes()));503Ok(())504})?;505Ok(arr.into())506}507508// No faster implementation than this available, fall back to default.509// fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>510}511512/// We use this to reuse the binary collect implementation for strings.513/// # Safety514/// The array must be valid UTF-8.515unsafe fn into_utf8array(arr: BinaryArray<i64>) -> Utf8Array<i64> {516unsafe {517let (_dt, offsets, values, validity) = arr.into_inner();518Utf8Array::new_unchecked(ArrowDataType::LargeUtf8, offsets, values, validity)519}520}521522trait StrIntoBytes: IntoBytes {}523impl StrIntoBytes for String {}524impl StrIntoBytes for &str {}525impl StrIntoBytes for Cow<'_, str> {}526527impl<T: StrIntoBytes> ArrayFromIter<T> for Utf8ViewArray {528#[inline]529fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {530unsafe { BinaryViewArray::arr_from_iter(iter).to_utf8view_unchecked() }531}532533#[inline]534fn arr_from_iter_trusted<I>(iter: I) -> Self535where536I: IntoIterator<Item = T>,537I::IntoIter: TrustedLen,538{539Self::arr_from_iter(iter)540}541542fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E> {543unsafe { BinaryViewArray::try_arr_from_iter(iter).map(|arr| arr.to_utf8view_unchecked()) }544}545546// No faster implementation than this available, fall back to default.547// fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>548}549550impl<T: StrIntoBytes> ArrayFromIter<Option<T>> for Utf8ViewArray {551#[inline]552fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {553unsafe { BinaryViewArray::arr_from_iter(iter).to_utf8view_unchecked() }554}555556#[inline]557fn arr_from_iter_trusted<I>(iter: I) -> Self558where559I: IntoIterator<Item = Option<T>>,560I::IntoIter: TrustedLen,561{562Self::arr_from_iter(iter)563}564565fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(566iter: I,567) -> Result<Self, E> {568unsafe { BinaryViewArray::try_arr_from_iter(iter).map(|arr| arr.to_utf8view_unchecked()) }569}570571// No faster implementation than this available, fall back to default.572// fn try_arr_from_iter_trusted<E, I>(iter: I) -> Result<Self, E>573}574575impl<T: StrIntoBytes> ArrayFromIter<T> for Utf8Array<i64> {576#[inline(always)]577fn arr_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {578unsafe { into_utf8array(iter.into_iter().collect_arr()) }579}580581#[inline(always)]582fn arr_from_iter_trusted<I>(iter: I) -> Self583where584I: IntoIterator<Item = T>,585I::IntoIter: TrustedLen,586{587unsafe { into_utf8array(iter.into_iter().collect_arr()) }588}589590#[inline(always)]591fn try_arr_from_iter<E, I: IntoIterator<Item = Result<T, E>>>(iter: I) -> Result<Self, E> {592let arr = iter.into_iter().try_collect_arr()?;593unsafe { Ok(into_utf8array(arr)) }594}595596#[inline(always)]597fn try_arr_from_iter_trusted<E, I: IntoIterator<Item = Result<T, E>>>(598iter: I,599) -> Result<Self, E> {600let arr = iter.into_iter().try_collect_arr()?;601unsafe { Ok(into_utf8array(arr)) }602}603}604605impl<T: StrIntoBytes> ArrayFromIter<Option<T>> for Utf8Array<i64> {606#[inline(always)]607fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {608unsafe { into_utf8array(iter.into_iter().collect_arr()) }609}610611#[inline(always)]612fn arr_from_iter_trusted<I>(iter: I) -> Self613where614I: IntoIterator<Item = Option<T>>,615I::IntoIter: TrustedLen,616{617unsafe { into_utf8array(iter.into_iter().collect_arr()) }618}619620#[inline(always)]621fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(622iter: I,623) -> Result<Self, E> {624let arr = iter.into_iter().try_collect_arr()?;625unsafe { Ok(into_utf8array(arr)) }626}627628#[inline(always)]629fn try_arr_from_iter_trusted<E, I: IntoIterator<Item = Result<Option<T>, E>>>(630iter: I,631) -> Result<Self, E> {632let arr = iter.into_iter().try_collect_arr()?;633unsafe { Ok(into_utf8array(arr)) }634}635}636637impl ArrayFromIter<bool> for BooleanArray {638fn arr_from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self {639let iter = iter.into_iter();640let n = iter.size_hint().0;641let mut values = BitmapBuilder::with_capacity(n);642for val in iter {643values.push(val);644}645BooleanArray::new(ArrowDataType::Boolean, values.freeze(), None)646}647648// TODO: are efficient trusted collects for booleans worth it?649// fn arr_from_iter_trusted<I>(iter: I) -> Self650651fn try_arr_from_iter<E, I: IntoIterator<Item = Result<bool, E>>>(iter: I) -> Result<Self, E> {652let iter = iter.into_iter();653let n = iter.size_hint().0;654let mut values = BitmapBuilder::with_capacity(n);655for val in iter {656values.push(val?);657}658Ok(BooleanArray::new(659ArrowDataType::Boolean,660values.freeze(),661None,662))663}664665// fn try_arr_from_iter_trusted<E, I: IntoIterator<Item = Result<bool, E>>>(666}667668impl ArrayFromIter<Option<bool>> for BooleanArray {669fn arr_from_iter<I: IntoIterator<Item = Option<bool>>>(iter: I) -> Self {670let iter = iter.into_iter();671let n = iter.size_hint().0;672let mut values = BitmapBuilder::with_capacity(n);673let mut validity = BitmapBuilder::with_capacity(n);674for val in iter {675values.push(val.unwrap_or(false));676validity.push(val.is_some());677}678BooleanArray::new(679ArrowDataType::Boolean,680values.freeze(),681validity.into_opt_validity(),682)683}684685// fn arr_from_iter_trusted<I>(iter: I) -> Self686687fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<bool>, E>>>(688iter: I,689) -> Result<Self, E> {690let iter = iter.into_iter();691let n = iter.size_hint().0;692let mut values = BitmapBuilder::with_capacity(n);693let mut validity = BitmapBuilder::with_capacity(n);694for val in iter {695let val = val?;696values.push(val.unwrap_or(false));697validity.push(val.is_some());698}699Ok(BooleanArray::new(700ArrowDataType::Boolean,701values.freeze(),702validity.into_opt_validity(),703))704}705706// fn try_arr_from_iter_trusted<E, I: IntoIterator<Item = Result<Option<bool>, E>>>(707}708709// We don't use AsRef here because it leads to problems with conflicting implementations,710// as Rust considers that AsRef<dyn Array> for Option<&dyn Array> could be implemented.711trait AsArray {712fn as_array(&self) -> &dyn Array;713#[cfg(feature = "dtype-array")]714fn into_boxed_array(self) -> Box<dyn Array>; // Prevents unnecessary re-boxing.715}716impl AsArray for Box<dyn Array> {717fn as_array(&self) -> &dyn Array {718self.as_ref()719}720#[cfg(feature = "dtype-array")]721fn into_boxed_array(self) -> Box<dyn Array> {722self723}724}725impl<'a> AsArray for &'a dyn Array {726fn as_array(&self) -> &'a dyn Array {727*self728}729#[cfg(feature = "dtype-array")]730fn into_boxed_array(self) -> Box<dyn Array> {731self.to_boxed()732}733}734735// TODO: more efficient (fixed size) list collect routines.736impl<T: AsArray> ArrayFromIterDtype<T> for ListArray<i64> {737fn arr_from_iter_with_dtype<I: IntoIterator<Item = T>>(dtype: ArrowDataType, iter: I) -> Self {738let iter_values: Vec<T> = iter.into_iter().collect();739let mut builder = AnonymousListArrayBuilder::new(iter_values.len());740for arr in &iter_values {741builder.push(arr.as_array());742}743let inner = dtype744.inner_dtype()745.expect("expected nested type in ListArray collect");746builder747.finish(Some(&inner.underlying_physical_type()))748.unwrap()749}750751fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<T, E>>>(752dtype: ArrowDataType,753iter: I,754) -> Result<Self, E> {755let iter_values = iter.into_iter().collect::<Result<Vec<_>, E>>()?;756Ok(Self::arr_from_iter_with_dtype(dtype, iter_values))757}758}759760impl<T: AsArray> ArrayFromIterDtype<Option<T>> for ListArray<i64> {761fn arr_from_iter_with_dtype<I: IntoIterator<Item = Option<T>>>(762dtype: ArrowDataType,763iter: I,764) -> Self {765let iter_values: Vec<Option<T>> = iter.into_iter().collect();766let mut builder = AnonymousListArrayBuilder::new(iter_values.len());767for arr in &iter_values {768builder.push_opt(arr.as_ref().map(|a| a.as_array()));769}770let inner = dtype771.inner_dtype()772.expect("expected nested type in ListArray collect");773builder774.finish(Some(&inner.underlying_physical_type()))775.unwrap()776}777778fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<Option<T>, E>>>(779dtype: ArrowDataType,780iter: I,781) -> Result<Self, E> {782let iter_values = iter.into_iter().collect::<Result<Vec<_>, E>>()?;783let mut builder = AnonymousListArrayBuilder::new(iter_values.len());784for arr in &iter_values {785builder.push_opt(arr.as_ref().map(|a| a.as_array()));786}787let inner = dtype788.inner_dtype()789.expect("expected nested type in ListArray collect");790Ok(builder791.finish(Some(&inner.underlying_physical_type()))792.unwrap())793}794}795796impl<T: AsArray> ArrayFromIter<Option<T>> for ListArray<i64> {797fn arr_from_iter<I: IntoIterator<Item = Option<T>>>(iter: I) -> Self {798let iter = iter.into_iter();799let iter_values: Vec<Option<T>> = iter.into_iter().collect();800let mut builder = AnonymousListArrayBuilder::new(iter_values.len());801for arr in &iter_values {802builder.push_opt(arr.as_ref().map(|a| a.as_array()));803}804builder.finish(None).unwrap()805}806807fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<T>, E>>>(808iter: I,809) -> Result<Self, E> {810let iter_values = iter.into_iter().collect::<Result<Vec<_>, E>>()?;811let mut builder = AnonymousListArrayBuilder::new(iter_values.len());812for arr in &iter_values {813builder.push_opt(arr.as_ref().map(|a| a.as_array()));814}815Ok(builder.finish(None).unwrap())816}817}818819impl ArrayFromIterDtype<Box<dyn Array>> for FixedSizeListArray {820#[allow(unused_variables)]821fn arr_from_iter_with_dtype<I: IntoIterator<Item = Box<dyn Array>>>(822dtype: ArrowDataType,823iter: I,824) -> Self {825#[cfg(feature = "dtype-array")]826{827let ArrowDataType::FixedSizeList(_, width) = &dtype else {828panic!("FixedSizeListArray::arr_from_iter_with_dtype called with non-Array dtype");829};830let iter_values: Vec<_> = iter.into_iter().collect();831let mut builder = AnonymousFixedSizeListArrayBuilder::new(iter_values.len(), *width);832for arr in iter_values {833builder.push(arr.into_boxed_array());834}835let inner = dtype836.inner_dtype()837.expect("expected nested type in ListArray collect");838builder839.finish(Some(&inner.underlying_physical_type()))840.unwrap()841}842#[cfg(not(feature = "dtype-array"))]843panic!("activate 'dtype-array'")844}845846fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<Box<dyn Array>, E>>>(847dtype: ArrowDataType,848iter: I,849) -> Result<Self, E> {850let iter_values = iter.into_iter().collect::<Result<Vec<_>, E>>()?;851Ok(Self::arr_from_iter_with_dtype(dtype, iter_values))852}853}854855impl ArrayFromIterDtype<Option<Box<dyn Array>>> for FixedSizeListArray {856#[allow(unused_variables)]857fn arr_from_iter_with_dtype<I: IntoIterator<Item = Option<Box<dyn Array>>>>(858dtype: ArrowDataType,859iter: I,860) -> Self {861#[cfg(feature = "dtype-array")]862{863let ArrowDataType::FixedSizeList(_, width) = &dtype else {864panic!("FixedSizeListArray::arr_from_iter_with_dtype called with non-Array dtype");865};866let iter_values: Vec<_> = iter.into_iter().collect();867let mut builder = AnonymousFixedSizeListArrayBuilder::new(iter_values.len(), *width);868for arr in iter_values {869match arr {870Some(a) => builder.push(a.into_boxed_array()),871None => builder.push_null(),872}873}874let inner = dtype875.inner_dtype()876.expect("expected nested type in ListArray collect");877builder878.finish(Some(&inner.underlying_physical_type()))879.unwrap()880}881#[cfg(not(feature = "dtype-array"))]882panic!("activate 'dtype-array'")883}884885fn try_arr_from_iter_with_dtype<886E,887I: IntoIterator<Item = Result<Option<Box<dyn Array>>, E>>,888>(889dtype: ArrowDataType,890iter: I,891) -> Result<Self, E> {892let iter_values = iter.into_iter().collect::<Result<Vec<_>, E>>()?;893Ok(Self::arr_from_iter_with_dtype(dtype, iter_values))894}895}896897impl ArrayFromIter<Option<()>> for StructArray {898fn arr_from_iter<I: IntoIterator<Item = Option<()>>>(_iter: I) -> Self {899no_call_const!()900}901902fn try_arr_from_iter<E, I: IntoIterator<Item = Result<Option<()>, E>>>(903_iter: I,904) -> Result<Self, E> {905no_call_const!()906}907}908909impl ArrayFromIter<()> for StructArray {910fn arr_from_iter<I: IntoIterator<Item = ()>>(_iter: I) -> Self {911no_call_const!()912}913914fn try_arr_from_iter<E, I: IntoIterator<Item = Result<(), E>>>(_iter: I) -> Result<Self, E> {915no_call_const!()916}917}918919impl ArrayFromIterDtype<()> for StructArray {920fn arr_from_iter_with_dtype<I: IntoIterator<Item = ()>>(921_dtype: ArrowDataType,922_iter: I,923) -> Self {924no_call_const!()925}926927fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<(), E>>>(928_dtype: ArrowDataType,929_iter: I,930) -> Result<Self, E> {931no_call_const!()932}933}934935impl ArrayFromIterDtype<Option<()>> for StructArray {936fn arr_from_iter_with_dtype<I: IntoIterator<Item = Option<()>>>(937_dtype: ArrowDataType,938_iter: I,939) -> Self {940no_call_const!()941}942943fn try_arr_from_iter_with_dtype<E, I: IntoIterator<Item = Result<Option<()>, E>>>(944_dtype: ArrowDataType,945_iter: I,946) -> Result<Self, E> {947no_call_const!()948}949}950951952