diff --git a/src/compute/cast/mod.rs b/src/compute/cast/mod.rs index 19abb22dca2..7b4231f9605 100644 --- a/src/compute/cast/mod.rs +++ b/src/compute/cast/mod.rs @@ -2,7 +2,6 @@ use crate::{ array::*, - buffer::Buffer, datatypes::*, error::{ArrowError, Result}, }; @@ -290,9 +289,12 @@ fn cast_list( } fn cast_list_to_large_list(array: &ListArray, to_type: &DataType) -> ListArray { - let offsets = array.offsets(); - let offsets = offsets.iter().map(|x| *x as i64); - let offets = Buffer::from_trusted_len_iter(offsets); + let offets = array + .offsets() + .iter() + .map(|x| *x as i64) + .collect::>() + .into(); ListArray::::from_data( to_type.clone(), @@ -303,13 +305,16 @@ fn cast_list_to_large_list(array: &ListArray, to_type: &DataType) -> ListAr } fn cast_large_to_list(array: &ListArray, to_type: &DataType) -> ListArray { - let offsets = array.offsets(); - let offsets = offsets.iter().map(|x| *x as i32); - let offets = Buffer::from_trusted_len_iter(offsets); + let offsets = array + .offsets() + .iter() + .map(|x| *x as i32) + .collect::>() + .into(); ListArray::::from_data( to_type.clone(), - offets, + offsets, array.values().clone(), array.validity().cloned(), ) @@ -385,10 +390,10 @@ pub fn cast(array: &dyn Array, to_type: &DataType, options: CastOptions) -> Resu // cast primitive to list's primitive let values = cast(array, &to.data_type, options)?.into(); // create offsets, where if array.len() = 2, we have [0,1,2] - let offsets = - unsafe { Buffer::from_trusted_len_iter_unchecked(0..=array.len() as i32) }; + let offsets = (0..=array.len() as i32).collect::>(); - let list_array = ListArray::::from_data(to_type.clone(), offsets, values, None); + let list_array = + ListArray::::from_data(to_type.clone(), offsets.into(), values, None); Ok(Box::new(list_array)) } diff --git a/src/compute/cast/utf8_to.rs b/src/compute/cast/utf8_to.rs index 20711261fb2..ac6e60ade19 100644 --- a/src/compute/cast/utf8_to.rs +++ b/src/compute/cast/utf8_to.rs @@ -2,13 +2,15 @@ use std::convert::TryFrom; use chrono::Datelike; -use crate::{array::*, buffer::Buffer, datatypes::DataType, types::NativeType}; use crate::{ + array::*, + datatypes::DataType, error::{ArrowError, Result}, temporal_conversions::{ utf8_to_naive_timestamp_ns as utf8_to_naive_timestamp_ns_, utf8_to_timestamp_ns as utf8_to_timestamp_ns_, EPOCH_DAYS_FROM_CE, }, + types::NativeType, }; use super::CastOptions; @@ -145,24 +147,32 @@ pub fn utf8_to_timestamp_ns( /// Conversion of utf8 pub fn utf8_to_large_utf8(from: &Utf8Array) -> Utf8Array { let data_type = Utf8Array::::default_data_type(); + let validity = from.validity().cloned(); let values = from.values().clone(); - let offsets = from.offsets().iter().map(|x| *x as i64); - let offsets = Buffer::from_trusted_len_iter(offsets); - unsafe { - Utf8Array::::from_data_unchecked(data_type, offsets, values, from.validity().cloned()) - } + let offsets = from + .offsets() + .iter() + .map(|x| *x as i64) + .collect::>() + .into(); + // Safety: sound because `offsets` fulfills the same invariants as `from.offsets()` + unsafe { Utf8Array::::from_data_unchecked(data_type, offsets, values, validity) } } /// Conversion of utf8 pub fn utf8_large_to_utf8(from: &Utf8Array) -> Result> { let data_type = Utf8Array::::default_data_type(); + let validity = from.validity().cloned(); let values = from.values().clone(); let _ = i32::try_from(*from.offsets().last().unwrap()).map_err(ArrowError::from_external_error)?; - let offsets = from.offsets().iter().map(|x| *x as i32); - let offsets = Buffer::from_trusted_len_iter(offsets); - Ok(unsafe { - Utf8Array::::from_data_unchecked(data_type, offsets, values, from.validity().cloned()) - }) + let offsets = from + .offsets() + .iter() + .map(|x| *x as i32) + .collect::>() + .into(); + // Safety: sound because `offsets` fulfills the same invariants as `from.offsets()` + Ok(unsafe { Utf8Array::::from_data_unchecked(data_type, offsets, values, validity) }) }