This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new be24e4f1e1 arrow-cast: Add display formatter for ListView (#9175)
be24e4f1e1 is described below

commit be24e4f1e16029562f2fed9e36ae70c6f7d3016b
Author: Frederic Branczyk <[email protected]>
AuthorDate: Thu Jan 15 18:22:18 2026 +0100

    arrow-cast: Add display formatter for ListView (#9175)
    
    # Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax.
    -->
    
    Closes https://github.com/apache/arrow-rs/issues/9173
    
    # Are these changes tested?
    
    I didn't add unit tests since the other formatters don't appear to be
    unit tested either, is the idea to test these via sql logic tests in df?
    
    # Are there any user-facing changes?
    
    No, just additive functionality
    
    @alamb @Jefffrey
---
 arrow-cast/src/display.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/arrow-cast/src/display.rs b/arrow-cast/src/display.rs
index bfd0f06dbe..59dfa26c93 100644
--- a/arrow-cast/src/display.rs
+++ b/arrow-cast/src/display.rs
@@ -531,6 +531,8 @@ fn make_default_display_index<'a>(
         }
         DataType::List(_) => array_format(as_generic_list_array::<i32>(array), 
options),
         DataType::LargeList(_) => 
array_format(as_generic_list_array::<i64>(array), options),
+        DataType::ListView(_) => array_format(array.as_list_view::<i32>(), 
options),
+        DataType::LargeListView(_) => 
array_format(array.as_list_view::<i64>(), options),
         DataType::FixedSizeList(_, _) => {
             let a = 
array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
             array_format(a, options)
@@ -1190,6 +1192,27 @@ impl<'a, O: OffsetSizeTrait> DisplayIndexState<'a> for 
&'a GenericListArray<O> {
     }
 }
 
+impl<'a, O: OffsetSizeTrait> DisplayIndexState<'a> for &'a 
GenericListViewArray<O> {
+    type State = ArrayFormatter<'a>;
+
+    fn prepare(&self, options: &FormatOptions<'a>) -> Result<Self::State, 
ArrowError> {
+        let field = match (*self).data_type() {
+            DataType::ListView(f) => f,
+            DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        make_array_formatter(self.values().as_ref(), options, 
Some(field.as_ref()))
+    }
+
+    fn write(&self, s: &Self::State, idx: usize, f: &mut dyn Write) -> 
FormatResult {
+        let offsets = self.value_offsets();
+        let sizes = self.value_sizes();
+        let start = offsets[idx].as_usize();
+        let end = start + sizes[idx].as_usize();
+        write_list(f, start..end, s)
+    }
+}
+
 impl<'a> DisplayIndexState<'a> for &'a FixedSizeListArray {
     type State = (usize, ArrayFormatter<'a>);
 
@@ -1516,4 +1539,34 @@ mod tests {
             array_value_to_string(&map_array, 3).unwrap()
         );
     }
+
+    #[test]
+    fn test_list_view_to_string() {
+        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, 
_>(vec![
+            Some(vec![Some(1), Some(2), Some(3)]),
+            None,
+            Some(vec![Some(4), None, Some(6)]),
+            Some(vec![]),
+        ]);
+
+        assert_eq!("[1, 2, 3]", array_value_to_string(&list_view, 0).unwrap());
+        assert_eq!("", array_value_to_string(&list_view, 1).unwrap());
+        assert_eq!("[4, , 6]", array_value_to_string(&list_view, 2).unwrap());
+        assert_eq!("[]", array_value_to_string(&list_view, 3).unwrap());
+    }
+
+    #[test]
+    fn test_large_list_view_to_string() {
+        let list_view = LargeListViewArray::from_iter_primitive::<Int32Type, 
_, _>(vec![
+            Some(vec![Some(1), Some(2), Some(3)]),
+            None,
+            Some(vec![Some(4), None, Some(6)]),
+            Some(vec![]),
+        ]);
+
+        assert_eq!("[1, 2, 3]", array_value_to_string(&list_view, 0).unwrap());
+        assert_eq!("", array_value_to_string(&list_view, 1).unwrap());
+        assert_eq!("[4, , 6]", array_value_to_string(&list_view, 2).unwrap());
+        assert_eq!("[]", array_value_to_string(&list_view, 3).unwrap());
+    }
 }

Reply via email to