alamb commented on code in PR #5686:
URL: https://github.com/apache/arrow-rs/pull/5686#discussion_r1580831616
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -2238,6 +2249,50 @@ where
Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
}
+/// Helper function to cast from one `ByteArrayType` array to `ByteViewType`
array.
+fn cast_byte_to_view<FROM, V>(array: &dyn Array) -> Result<ArrayRef,
ArrowError>
+where
+ FROM: ByteArrayType,
+ FROM::Offset: OffsetSizeTrait + ToPrimitive,
+ V: ByteViewType,
+{
+ let data = array.to_data();
+ let len = array.len();
+ assert_eq!(data.data_type(), &FROM::DATA_TYPE);
+ let str_values_buf = data.buffers()[1].clone();
+ let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
+
+ let mut views_builder = BufferBuilder::<u128>::new(len);
+ for w in offsets.windows(2) {
+ let offset = w[0].to_u32().unwrap();
+ let end = w[1].to_u32().unwrap();
+ let value_buf = &str_values_buf[offset as usize..end as usize];
+ let length = end - offset;
+
+ if length <= 12 {
+ let mut view_buffer = [0; 16];
+ view_buffer[0..4].copy_from_slice(&length.to_le_bytes());
+ view_buffer[4..4 + value_buf.len()].copy_from_slice(value_buf);
+ views_builder.append(u128::from_le_bytes(view_buffer));
+ } else {
+ let view = ByteView {
+ length,
+ prefix:
u32::from_le_bytes(value_buf[0..4].try_into().unwrap()),
+ buffer_index: 0,
+ offset,
+ };
+ views_builder.append(view.into());
+ }
+ }
+
+ assert_eq!(views_builder.len(), len);
+ Ok(Arc::new(GenericByteViewArray::<V>::new(
Review Comment:
I think this should use `GenericByteViewArray::new_unchecked` as the input
was valid and the code constructs valid output by construction there is no need
to re-validate it
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -2238,6 +2249,50 @@ where
Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
}
+/// Helper function to cast from one `ByteArrayType` array to `ByteViewType`
array.
+fn cast_byte_to_view<FROM, V>(array: &dyn Array) -> Result<ArrayRef,
ArrowError>
+where
+ FROM: ByteArrayType,
+ FROM::Offset: OffsetSizeTrait + ToPrimitive,
+ V: ByteViewType,
+{
+ let data = array.to_data();
+ let len = array.len();
+ assert_eq!(data.data_type(), &FROM::DATA_TYPE);
+ let str_values_buf = data.buffers()[1].clone();
+ let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
+
+ let mut views_builder = BufferBuilder::<u128>::new(len);
+ for w in offsets.windows(2) {
+ let offset = w[0].to_u32().unwrap();
+ let end = w[1].to_u32().unwrap();
+ let value_buf = &str_values_buf[offset as usize..end as usize];
+ let length = end - offset;
+
+ if length <= 12 {
Review Comment:
As an aside (not for this PR) it would be great to somehow encapsulate this
logic into a struct to avoid having to copy the same pattern it so many times.
I took a shot at this here https://github.com/apache/arrow-rs/pull/5619
--
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]