pchintar commented on code in PR #9971:
URL: https://github.com/apache/arrow-rs/pull/9971#discussion_r3249964669
##########
arrow-ipc/src/reader.rs:
##########
@@ -72,6 +72,58 @@ fn read_buffer(
}
}
}
+
+/// Source for IPC body buffers.
+///
+/// Most decode paths use an already materialized [`Buffer`] and slice into it
+/// using the offsets from the IPC metadata. Keeping this behind a small helper
+/// lets typed buffer reads share the same bounds handling as regular buffer
+/// reads.
+enum IpcBufferSource<'a> {
+ Buffer(&'a Buffer),
+}
+
+impl<'a> IpcBufferSource<'a> {
+ fn read_buffer(
+ &self,
+ buf: &crate::Buffer,
+ compression: Option<CompressionCodec>,
+ decompression_context: &mut DecompressionContext,
+ ) -> Result<Buffer, ArrowError> {
+ match self {
+ Self::Buffer(data) => read_buffer(buf, data, compression,
decompression_context),
+ }
+ }
+ /// Reads a physical IPC buffer that is expected to contain `len` values of
+ /// type `T`.
+ ///
+ /// The returned value is still a [`Buffer`], not a [`ScalarBuffer<T>`].
This
+ /// preserves the existing alignment behavior: properly aligned buffers
remain
+ /// zero-copy, while unaligned buffers are still handled later by
+ /// `ArrayDataBuilder::align_buffers` according to `require_alignment`.
+ fn read_typed_buffer<T: ArrowNativeType>(
+ &self,
+ buf: &crate::Buffer,
+ len: usize,
+ compression: Option<CompressionCodec>,
+ decompression_context: &mut DecompressionContext,
+ ) -> Result<Buffer, ArrowError> {
+ let byte_len = len
+ .checked_mul(std::mem::size_of::<T>())
+ .ok_or_else(|| ArrowError::IpcError("Buffer length
overflow".to_string()))?;
+
+ let buffer = self.read_buffer(buf, compression,
decompression_context)?;
+ // Some invalid or legacy IPC inputs may contain shorter buffers than
+ // implied by the schema. Preserve the existing behavior and let array
+ // construction/validation report the error.
+ if buffer.len() <= byte_len ||
buffer.as_ptr().align_offset(std::mem::align_of::<T>()) != 0
Review Comment:
I removed the "buffer.as_ptr().align_offset(std::mem::align_of::<T>()) != 0"
part as it was redundant
--
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]