From a6aecce8a3d2fa371da2ae556c52506661d47c04 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Tue, 27 Sep 2022 13:50:27 +0100 Subject: [PATCH] Add PrimitiveArray::reinterpret_cast (#2785) --- arrow-array/src/array/primitive_array.rs | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs index f9e4e7675da..758d57c8ab9 100644 --- a/arrow-array/src/array/primitive_array.rs +++ b/arrow-array/src/array/primitive_array.rs @@ -332,6 +332,35 @@ impl PrimitiveArray { ) -> impl Iterator> + 'a { indexes.map(|opt_index| opt_index.map(|index| self.value_unchecked(index))) } + + /// Reinterprets this array's contents as a different data type without copying + /// + /// This can be used to efficiently convert between primitive arrays with the + /// same underlying representation + /// + /// Note: this will not modify the underlying values, and therefore may change + /// the semantic values of the array, e.g. 100 milliseconds in a [`TimestampNanosecondArray`] + /// will become 100 seconds in a [`TimestampSecondArray`]. + /// + /// For casts that preserve the semantic value, check out the [compute kernels] + /// + /// [compute kernels](https://docs.rs/arrow/latest/arrow/compute/kernels/cast/index.html) + /// + /// ``` + /// # use arrow_array::{Int64Array, TimestampNanosecondArray}; + /// let a = Int64Array::from_iter_values([1, 2, 3, 4]); + /// let b: TimestampNanosecondArray = a.reinterpret_cast(); + /// ``` + pub fn reinterpret_cast(&self) -> PrimitiveArray + where + K: ArrowPrimitiveType, + { + let d = self.data.clone().into_builder().data_type(K::DATA_TYPE); + + // SAFETY: + // Native type is the same + PrimitiveArray::from(unsafe { d.build_unchecked() }) + } } impl From> for ArrayData {