diff --git a/arrow/src/pyarrow.rs b/arrow/src/pyarrow.rs index b05c967d7d9b..6effe1c03e01 100644 --- a/arrow/src/pyarrow.rs +++ b/arrow/src/pyarrow.rs @@ -363,13 +363,19 @@ impl FromPyArrow for RecordBatch { let schema_ptr = unsafe { schema_capsule.reference::() }; let ffi_array = unsafe { FFI_ArrowArray::from_raw(array_capsule.pointer().cast()) }; - let array_data = unsafe { ffi::from_ffi(ffi_array, schema_ptr) }.map_err(to_py_err)?; + let mut array_data = + unsafe { ffi::from_ffi(ffi_array, schema_ptr) }.map_err(to_py_err)?; if !matches!(array_data.data_type(), DataType::Struct(_)) { return Err(PyTypeError::new_err( "Expected Struct type from __arrow_c_array.", )); } let options = RecordBatchOptions::default().with_row_count(Some(array_data.len())); + // Ensure data is aligned (by potentially copying the buffers). + // This is needed because some python code (for example the + // python flight client) produces unaligned buffers + // See https://github.com/apache/arrow/issues/43552 for details + array_data.align_buffers(); let array = StructArray::from(array_data); // StructArray does not embed metadata from schema. We need to override // the output schema with the schema from the capsule.