diff --git a/src/array/growable/dictionary.rs b/src/array/growable/dictionary.rs index 0b3c432525a..930f4268f28 100644 --- a/src/array/growable/dictionary.rs +++ b/src/array/growable/dictionary.rs @@ -88,11 +88,22 @@ impl<'a, T: DictionaryKey> GrowableDictionary<'a, T> { let validity = std::mem::take(&mut self.key_validity); let key_values = std::mem::take(&mut self.key_values); + #[cfg(debug_assertions)] + { + crate::array::specification::check_indexes(&key_values, self.values.len()).unwrap(); + } let keys = - PrimitiveArray::::try_new(T::PRIMITIVE.into(), key_values.into(), validity.into()) - .unwrap(); - - DictionaryArray::::try_new(self.data_type.clone(), keys, self.values.clone()).unwrap() + PrimitiveArray::::new(T::PRIMITIVE.into(), key_values.into(), validity.into()); + + // Safety - the invariant of this struct ensures that this is up-held + unsafe { + DictionaryArray::::try_new_unchecked( + self.data_type.clone(), + keys, + self.values.clone(), + ) + .unwrap() + } } } @@ -141,12 +152,7 @@ impl<'a, T: DictionaryKey> Growable<'a> for GrowableDictionary<'a, T> { impl<'a, T: DictionaryKey> From> for DictionaryArray { #[inline] - fn from(val: GrowableDictionary<'a, T>) -> Self { - let data_type = T::PRIMITIVE.into(); - let keys = - PrimitiveArray::::try_new(data_type, val.key_values.into(), val.key_validity.into()) - .unwrap(); - - DictionaryArray::::try_new(val.data_type.clone(), keys, val.values).unwrap() + fn from(mut val: GrowableDictionary<'a, T>) -> Self { + val.to() } } diff --git a/src/array/growable/list.rs b/src/array/growable/list.rs index 41db9ae7892..0e1e7ceb5f8 100644 --- a/src/array/growable/list.rs +++ b/src/array/growable/list.rs @@ -104,12 +104,20 @@ impl<'a, O: Offset> GrowableList<'a, O> { let offsets = std::mem::take(&mut self.offsets); let values = self.values.as_box(); - ListArray::::new( - self.arrays[0].data_type().clone(), - offsets.into(), - values, - validity.into(), - ) + #[cfg(debug_assertions)] + { + crate::array::specification::try_check_offsets(&offsets, values.len()).unwrap(); + } + + // Safety - the invariant of this struct ensures that this is up-held + unsafe { + ListArray::::new_unchecked( + self.arrays[0].data_type().clone(), + offsets.into(), + values, + validity.into(), + ) + } } } @@ -135,15 +143,7 @@ impl<'a, O: Offset> Growable<'a> for GrowableList<'a, O> { } impl<'a, O: Offset> From> for ListArray { - fn from(val: GrowableList<'a, O>) -> Self { - let mut values = val.values; - let values = values.as_box(); - - ListArray::::new( - val.arrays[0].data_type().clone(), - val.offsets.into(), - values, - val.validity.into(), - ) + fn from(mut val: GrowableList<'a, O>) -> Self { + val.to() } } diff --git a/src/array/growable/utf8.rs b/src/array/growable/utf8.rs index c50c6fd4dbc..adfdba2cd53 100644 --- a/src/array/growable/utf8.rs +++ b/src/array/growable/utf8.rs @@ -55,13 +55,19 @@ impl<'a, O: Offset> GrowableUtf8<'a, O> { let offsets = std::mem::take(&mut self.offsets); let values = std::mem::take(&mut self.values); + #[cfg(debug_assertions)] + { + crate::array::specification::try_check_offsets_and_utf8(&offsets, &values).unwrap(); + } + unsafe { - Utf8Array::::from_data_unchecked( + Utf8Array::::try_new_unchecked( self.arrays[0].data_type().clone(), offsets.into(), values.into(), validity.into(), ) + .unwrap() } } } @@ -99,14 +105,7 @@ impl<'a, O: Offset> Growable<'a> for GrowableUtf8<'a, O> { } impl<'a, O: Offset> From> for Utf8Array { - fn from(val: GrowableUtf8<'a, O>) -> Self { - unsafe { - Utf8Array::::from_data_unchecked( - val.arrays[0].data_type().clone(), - val.offsets.into(), - val.values.into(), - val.validity.into(), - ) - } + fn from(mut val: GrowableUtf8<'a, O>) -> Self { + val.to() } }