alamb commented on code in PR #5686:
URL: https://github.com/apache/arrow-rs/pull/5686#discussion_r1581191443


##########
arrow-cast/src/cast/mod.rs:
##########
@@ -2238,6 +2249,54 @@ 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();
+    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
+
+    let len = array.len();
+    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(unsafe {

Review Comment:
   ```suggestion
       /// Safety: the input was a valid array so the data was UTF8 (if string) 
and all offsets were valid
       /// and we created the views correctly
       Ok(Arc::new(unsafe {
   ```



-- 
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]

Reply via email to