Skip to content

Commit

Permalink
Minor: improve docs for ByteViewArray->ByteArray From impl
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Oct 21, 2024
1 parent 2043353 commit 7de7da4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions arrow-array/src/array/byte_view_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,16 @@ impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T> {
}
}

/// Convert a [`GenericByteArray`] to a [`GenericByteViewArray`] but in a smart way:
/// If the offsets are all less than u32::MAX, then we directly build the view array on top of existing buffer.
/// Efficiently convert a [`GenericByteArray`] to a [`GenericByteViewArray`]
///
/// For example this method can convert a [`StringArray`] to a
/// [`StringViewArray`].
///
/// If the offsets are all less than u32::MAX, the new [`GenericByteViewArray`]
/// is build without copying the underlying string data (views are created
/// directly into the existing buffer)
///
/// [`StringArray`]: crate::StringArray
impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>
where
FROM: ByteArrayType,
Expand All @@ -616,6 +624,7 @@ where
};

if can_reuse_buffer {
// build views directly pointing to the existing buffer
let len = byte_array.len();
let mut views_builder = GenericByteViewBuilder::<V>::with_capacity(len);
let str_values_buf = byte_array.values().clone();
Expand All @@ -638,7 +647,9 @@ where
assert_eq!(views_builder.len(), len);
views_builder.finish()
} else {
// TODO: the first u32::MAX can still be reused
// otherwise, create a new buffer for large strings
// TODO: the original buffer could still be used
// until the offset reaches `u32::max`.
GenericByteViewArray::<V>::from_iter(byte_array.iter())
}
}
Expand Down

0 comments on commit 7de7da4

Please sign in to comment.