alamb commented on code in PR #10893:
URL: https://github.com/apache/arrow-rs/pull/10893#discussion_r3875058489
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -1196,15 +1196,31 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
impl<T: ArrowPrimitiveType> From<PrimitiveArray<T>> for ArrayData {
fn from(array: PrimitiveArray<T>) -> Self {
- let builder = ArrayDataBuilder::new(array.data_type)
- .len(array.values.len())
- .nulls(array.nulls)
- .buffers(vec![array.values.into_inner()]);
-
- unsafe { builder.build_unchecked() }
+ let len = array.values.len();
+ primitive_to_array_data(array.data_type, len,
array.values.into_inner(), array.nulls)
Review Comment:
the point is to move the code into a single function as most of the logic
doesn't vary by type
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -1196,15 +1196,31 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
impl<T: ArrowPrimitiveType> From<PrimitiveArray<T>> for ArrayData {
fn from(array: PrimitiveArray<T>) -> Self {
- let builder = ArrayDataBuilder::new(array.data_type)
- .len(array.values.len())
- .nulls(array.nulls)
- .buffers(vec![array.values.into_inner()]);
-
- unsafe { builder.build_unchecked() }
+ let len = array.values.len();
+ primitive_to_array_data(array.data_type, len,
array.values.into_inner(), array.nulls)
}
}
+/// Converts the parts of a valid `PrimitiveArray` into [`ArrayData`]
+///
+/// Deliberately not generic over [`ArrowPrimitiveType`]: after extracting the
+/// untyped [`Buffer`] the conversion is identical for all primitive types, so
+/// keeping it non-generic means it is compiled once rather than once per type
+fn primitive_to_array_data(
+ data_type: DataType,
+ len: usize,
+ values: Buffer,
+ nulls: Option<NullBuffer>,
+) -> ArrayData {
+ let builder = ArrayDataBuilder::new(data_type)
+ .len(len)
+ .nulls(nulls)
+ .buffers(vec![values]);
+
+ // SAFETY: the parts came from a valid PrimitiveArray
Review Comment:
bonus we now get a safetly comment too
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]