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


##########
arrow-cast/src/cast/string.rs:
##########
@@ -38,6 +38,22 @@ pub(crate) fn value_to_string<O: OffsetSizeTrait>(
     Ok(Arc::new(builder.finish()))
 }
 
+pub(crate) fn value_to_string_view(
+    array: &dyn Array,
+    options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let mut builder = StringViewBuilder::with_capacity(array.len());
+    let formatter = ArrayFormatter::try_new(array, &options.format_options)?;
+    let nulls = array.nulls();
+    for i in 0..array.len() {
+        match nulls.map(|x| x.is_null(i)).unwrap_or_default() {
+            true => builder.append_null(),
+            false => formatter.value(i).write(&mut builder)?,
+        }
+    }
+    Ok(Arc::new(builder.finish()))
+}

Review Comment:
   I have been dreaming about this PR and how to unblock it.
   
   I think it is currently stalled by trying to figure out how to handle 
`std::fmt::Write`
   
   Here is an alternate proposal:
   1. Change this implementation to avoid reallocating on each row, but still 
copy
   2. Remove the std::fmt::Write implementation (and we can sort that out in a 
different PR)
   
   So the first point might look something like this:
   
   ```suggestion
   pub(crate) fn value_to_string_view(
       array: &dyn Array,
       options: &CastOptions,
   ) -> Result<ArrayRef, ArrowError> {
       let mut builder = StringViewBuilder::with_capacity(array.len());
       let formatter = ArrayFormatter::try_new(array, &options.format_options)?;
       let nulls = array.nulls();
       // buffer to avoid reallocating on each value
       // TODO: replace with write to builder after 
https://github.com/apache/arrow-rs/issues/6373
       mut buffer = String::new();
       for i in 0..array.len() {
           match nulls.map(|x| x.is_null(i)).unwrap_or_default() {
               true => builder.append_null(),
               false => {
                 // write to buffer first and then copy into target array
                 buffer.clear();
                 formatter.value(i).write(&mut buffer)?,
                 bulder.append_value(&buffer)
                }
           }
       }
       Ok(Arc::new(builder.finish()))
   }
   ```



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