Skip to content

Apache DataFusion SQL Query Engine

fix: set IPC alignment based on schema (#17363)

ding-young268e11513660
diff --git a/datafusion/physical-plan/src/spill/mod.rs b/datafusion/physical-plan/src/spill/mod.rs
index 4d107d8af6..fab62bff84 100644
--- a/datafusion/physical-plan/src/spill/mod.rs
+++ b/datafusion/physical-plan/src/spill/mod.rs
@@ -28,7 +28,7 @@ use std::ptr::NonNull;
use std::sync::Arc;
use std::task::{Context, Poll};
-use arrow::array::ArrayData;
+use arrow::array::{layout, ArrayData, BufferSpec};
use arrow::datatypes::{Schema, SchemaRef};
use arrow::ipc::{
reader::StreamReader,
@@ -308,7 +308,12 @@ impl IPCStreamWriter {
})?;
let metadata_version = MetadataVersion::V5;
- let alignment = 8;
+ // Depending on the schema, some array types such as StringViewArray require larger (16 byte in this case) alignment.
+ // If the actual buffer layout after IPC read does not satisfy the alignment requirement,
+ // Arrow ArrayBuilder will copy the buffer into a newly allocated, properly aligned buffer.
+ // This copying may lead to memory blowup during IPC read due to duplicated buffers.
+ // To avoid this, we compute the maximum required alignment based on the schema and configure the IPCStreamWriter accordingly.
+ let alignment = get_max_alignment_for_schema(schema);
let mut write_options =
IpcWriteOptions::try_new(alignment, false, metadata_version)?;
write_options = write_options.try_with_compression(compression_type.into())?;
@@ -341,6 +346,29 @@ impl IPCStreamWriter {
}
}
+// Returns the maximum byte alignment required by any field in the schema (>= 8), derived from Arrow buffer layouts.
+fn get_max_alignment_for_schema(schema: &Schema) -> usize {
+ let minimum_alignment = 8;
+ let mut max_alignment = minimum_alignment;
+ for field in schema.fields() {
+ let layout = layout(field.data_type());
+ let required_alignment = layout
+ .buffers
+ .iter()
+ .map(|buffer_spec| {
+ if let BufferSpec::FixedWidth { alignment, .. } = buffer_spec {
+ *alignment
+ } else {
+ minimum_alignment
+ }
+ })
+ .max()
+ .unwrap_or(minimum_alignment);
+ max_alignment = std::cmp::max(max_alignment, required_alignment);
+ }
+ max_alignment
+}
+
#[cfg(test)]
mod tests {
use super::in_progress_spill_file::InProgressSpillFile;
@@ -911,4 +939,19 @@ mod tests {
Ok(())
})
}
+
+ #[test]
+ fn test_alignment_for_schema() -> Result<()> {
+ let schema = Schema::new(vec![Field::new("strings", DataType::Utf8View, false)]);
+ let alignment = get_max_alignment_for_schema(&schema);
+ assert_eq!(alignment, 16);
+
+ let schema = Schema::new(vec![
+ Field::new("int32", DataType::Int32, false),
+ Field::new("int64", DataType::Int64, false),
+ ]);
+ let alignment = get_max_alignment_for_schema(&schema);
+ assert_eq!(alignment, 8);
+ Ok(())
+ }
}