Ap-95671 commented on code in PR #12531:
URL: https://github.com/apache/arrow-rs/pull/12531#discussion_r4787119747
##########
arrow-select/src/coalesce.rs:
##########
@@ -866,6 +873,131 @@ impl BatchCoalescer {
}
}
+/// Compact the data buffers of view arrays nested inside `array`.
+///
+/// List, struct, and map columns are assembled from whole source arrays, so
+/// they never pass through the per-source compaction of
+/// `InProgressByteViewArray`. Walk them and compact any nested view array
+/// whose buffers hold more than twice the bytes its values use.
+/// Containers are rebuilt only when a child actually changes.
+fn compact_nested_views(array: ArrayRef) -> ArrayRef {
+ match array.data_type() {
+ DataType::List(_) => {
+ let list = array.as_any().downcast_ref::<ListArray>().unwrap();
+ let values = compact_nested_view_child(list.values().clone());
+ if Arc::ptr_eq(&values, list.values()) {
+ return array;
+ }
+ Arc::new(ListArray::new(
+ list.value_field().clone(),
+ list.offsets().clone(),
+ values,
+ list.nulls().cloned(),
+ ))
+ }
+ DataType::LargeList(_) => {
+ let list =
array.as_any().downcast_ref::<LargeListArray>().unwrap();
+ let values = compact_nested_view_child(list.values().clone());
+ if Arc::ptr_eq(&values, list.values()) {
+ return array;
+ }
+ Arc::new(LargeListArray::new(
+ list.value_field().clone(),
+ list.offsets().clone(),
+ values,
+ list.nulls().cloned(),
+ ))
+ }
+ DataType::FixedSizeList(_, _) => {
+ let list =
array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
+ let values = compact_nested_view_child(list.values().clone());
+ if Arc::ptr_eq(&values, list.values()) {
+ return array;
+ }
+ Arc::new(FixedSizeListArray::new(
+ list.value_field().clone(),
+ list.value_length(),
+ values,
+ list.nulls().cloned(),
+ ))
+ }
+ DataType::Struct(_) => {
+ let struct_array =
array.as_any().downcast_ref::<StructArray>().unwrap();
+ let mut changed = false;
+ let columns = struct_array
+ .columns()
+ .iter()
+ .map(|column| {
+ let compacted = compact_nested_view_child(column.clone());
+ changed |= !Arc::ptr_eq(&compacted, column);
+ compacted
+ })
+ .collect::<Vec<_>>();
+ if !changed {
+ return array;
+ }
+ Arc::new(StructArray::new(
+ struct_array.fields().clone(),
+ columns,
+ struct_array.nulls().cloned(),
+ ))
+ }
+ DataType::Map(_, ordered) => {
+ let map = array.as_any().downcast_ref::<MapArray>().unwrap();
+ let entries: ArrayRef = Arc::new(map.entries().clone());
+ let compacted = compact_nested_views(entries.clone());
+ if Arc::ptr_eq(&compacted, &entries) {
+ return array;
+ }
+ let entries = compacted
+ .as_any()
+ .downcast_ref::<StructArray>()
+ .unwrap()
+ .clone();
+ Arc::new(MapArray::new(
+ map.entries_field().clone(),
+ map.offsets().clone(),
+ entries,
+ map.nulls().cloned(),
+ *ordered,
+ ))
+ }
+ _ => array,
+ }
+}
+
+/// Compact `array` if it is a view array, or recurse into its containers.
+fn compact_nested_view_child(array: ArrayRef) -> ArrayRef {
+ match array.data_type() {
+ DataType::Utf8View => {
+ let view =
array.as_any().downcast_ref::<StringViewArray>().unwrap();
+ compact_view_array(view).map_or(array, |compacted|
Arc::new(compacted))
+ }
+ DataType::BinaryView => {
+ let view =
array.as_any().downcast_ref::<BinaryViewArray>().unwrap();
+ compact_view_array(view).map_or(array, |compacted|
Arc::new(compacted))
+ }
+ _ => compact_nested_views(array),
+ }
+}
+
+/// Copy `array` into tight buffers when its buffers hold more than twice the
+/// bytes its values use. Returns `None` when the original buffers are kept.
+fn compact_view_array<T: ByteViewType>(
+ array: &GenericByteViewArray<T>,
+) -> Option<GenericByteViewArray<T>> {
+ let used = array.total_buffer_bytes_used();
+ if used == 0 {
+ return None;
Review Comment:
One edge case to consider: a sparse take can retain only inline values, so
`used` is zero while the nested child still owns its original backing buffers.
Reusing the sparse-take test setup with 100 selected inline rows and 900
unselected 100-byte values, `List<Utf8View>` kept 90,000 backing bytes while
top-level `Utf8View` kept zero. Letting zero reach the existing capacity check
dropped the buffers in the reproduction. `gc()` has a fast path that reuses the
inline views, so it does not copy the string payload. An early
`data_buffers().is_empty()` check would keep buffer-free arrays cheap. Could
the sparse-take test cover this case too?
--
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]