ebembi-crdb commented on code in PR #19444:
URL: https://github.com/apache/datafusion/pull/19444#discussion_r2758320060
##########
datafusion/physical-plan/src/spill/mod.rs:
##########
@@ -339,6 +339,97 @@ fn get_max_alignment_for_schema(schema: &Schema) -> usize {
max_alignment
}
+#[cfg(test)]
+const VIEW_SIZE_BYTES: usize = 16;
+#[cfg(test)]
+const INLINE_THRESHOLD: usize = 12;
+
+/// Performs garbage collection on view arrays before spilling.
+pub(crate) fn gc_view_arrays(batch: &RecordBatch) -> Result<RecordBatch> {
+ let mut new_columns: Vec<Arc<dyn Array>> =
Vec::with_capacity(batch.num_columns());
+ let mut any_gc_performed = false;
+
+ for array in batch.columns() {
+ let gc_array = match array.data_type() {
+ arrow::datatypes::DataType::Utf8View => {
+ let string_view = array
+ .as_any()
+ .downcast_ref::<StringViewArray>()
+ .expect("Utf8View array should downcast to
StringViewArray");
+ if should_gc_view_array(string_view.len(),
string_view.data_buffers()) {
+ any_gc_performed = true;
+ Arc::new(string_view.gc()) as Arc<dyn Array>
+ } else {
+ Arc::clone(array)
+ }
+ }
+ arrow::datatypes::DataType::BinaryView => {
+ let binary_view = array
+ .as_any()
+ .downcast_ref::<BinaryViewArray>()
+ .expect("BinaryView array should downcast to
BinaryViewArray");
+ if should_gc_view_array(binary_view.len(),
binary_view.data_buffers()) {
+ any_gc_performed = true;
+ Arc::new(binary_view.gc()) as Arc<dyn Array>
+ } else {
+ Arc::clone(array)
+ }
+ }
+ _ => Arc::clone(array),
+ };
+ new_columns.push(gc_array);
+ }
+
+ if any_gc_performed {
+ Ok(RecordBatch::try_new(batch.schema(), new_columns)?)
+ } else {
+ Ok(batch.clone())
+ }
+}
+
+fn should_gc_view_array(len: usize, data_buffers: &[arrow::buffer::Buffer]) ->
bool {
+ if len < 10 {
Review Comment:
Agreed, switched to 10KB buffer size threshold instead.
##########
datafusion/physical-plan/src/spill/mod.rs:
##########
@@ -339,6 +339,97 @@ fn get_max_alignment_for_schema(schema: &Schema) -> usize {
max_alignment
}
+#[cfg(test)]
+const VIEW_SIZE_BYTES: usize = 16;
+#[cfg(test)]
+const INLINE_THRESHOLD: usize = 12;
+
+/// Performs garbage collection on view arrays before spilling.
+pub(crate) fn gc_view_arrays(batch: &RecordBatch) -> Result<RecordBatch> {
+ let mut new_columns: Vec<Arc<dyn Array>> =
Vec::with_capacity(batch.num_columns());
+ let mut any_gc_performed = false;
+
+ for array in batch.columns() {
+ let gc_array = match array.data_type() {
+ arrow::datatypes::DataType::Utf8View => {
+ let string_view = array
+ .as_any()
+ .downcast_ref::<StringViewArray>()
+ .expect("Utf8View array should downcast to
StringViewArray");
+ if should_gc_view_array(string_view.len(),
string_view.data_buffers()) {
+ any_gc_performed = true;
+ Arc::new(string_view.gc()) as Arc<dyn Array>
+ } else {
+ Arc::clone(array)
+ }
+ }
+ arrow::datatypes::DataType::BinaryView => {
+ let binary_view = array
+ .as_any()
+ .downcast_ref::<BinaryViewArray>()
+ .expect("BinaryView array should downcast to
BinaryViewArray");
+ if should_gc_view_array(binary_view.len(),
binary_view.data_buffers()) {
+ any_gc_performed = true;
+ Arc::new(binary_view.gc()) as Arc<dyn Array>
+ } else {
+ Arc::clone(array)
+ }
+ }
+ _ => Arc::clone(array),
+ };
+ new_columns.push(gc_array);
+ }
+
+ if any_gc_performed {
+ Ok(RecordBatch::try_new(batch.schema(), new_columns)?)
+ } else {
+ Ok(batch.clone())
+ }
+}
+
+fn should_gc_view_array(len: usize, data_buffers: &[arrow::buffer::Buffer]) ->
bool {
+ if len < 10 {
+ return false;
+ }
+
+ let total_buffer_size: usize = data_buffers.iter().map(|b|
b.capacity()).sum();
Review Comment:
We need buffer capacity of just the data buffers to detect waste, added a
comment explaining why.
##########
datafusion/physical-plan/src/spill/mod.rs:
##########
@@ -339,6 +339,97 @@ fn get_max_alignment_for_schema(schema: &Schema) -> usize {
max_alignment
}
+#[cfg(test)]
+const VIEW_SIZE_BYTES: usize = 16;
+#[cfg(test)]
+const INLINE_THRESHOLD: usize = 12;
Review Comment:
Done, importing from `arrow_data` now.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]