Rich-T-kid commented on code in PR #10428:
URL: https://github.com/apache/arrow-rs/pull/10428#discussion_r3666680254


##########
arrow-flight/tests/encode_decode.rs:
##########
@@ -414,6 +417,122 @@ async fn test_mismatched_schema_message() {
     .await;
 }
 
+/// Encode `batch` to IPC, shift its body buffer by `alignment_offset` bytes 
from a
+/// 64-byte-aligned base, then decode with alignment enforcement enabled.
+fn decode_misaligned(
+    batch: RecordBatch,
+    alignment_offset: usize,
+) -> Result<RecordBatch, arrow_schema::ArrowError> {
+    use arrow_buffer::{Buffer, MutableBuffer};
+    use arrow_ipc::writer::{DictionaryTracker, IpcDataGenerator};
+
+    let ipc_gen = IpcDataGenerator {};
+    let mut dict_tracker = DictionaryTracker::new(false);
+    let (_, encoded) = ipc_gen
+        .encode(
+            &batch,
+            &mut dict_tracker,
+            &Default::default(),
+            &mut Default::default(),
+        )
+        .unwrap();
+
+    // MutableBuffer guarantees 64-byte alignment, so slicing at 
`alignment_offset`
+    // produces a pointer misaligned for types with stricter requirements.
+    let mut mis = MutableBuffer::with_capacity(encoded.arrow_data.len() + 
alignment_offset);
+    for _ in 0..alignment_offset {
+        mis.push(0_u8);
+    }
+    mis.extend_from_slice(&encoded.arrow_data);
+    let misaligned_buf = Buffer::from(mis).slice(alignment_offset);
+
+    let message = arrow_ipc::root_as_message(&encoded.ipc_message).unwrap();
+    let ipc_batch = message.header_as_record_batch().unwrap();
+
+    reader::RecordBatchDecoder::try_new(
+        &misaligned_buf,
+        ipc_batch,
+        batch.schema(),
+        &Default::default(),
+        &message.version(),
+    )
+    .unwrap()
+    .with_require_alignment(true)
+    .read_record_batch()
+}
+
+#[test]
+fn test_misaligned_int32() {
+    let err = decode_misaligned(
+        RecordBatch::try_from_iter(vec![(
+            "i32",
+            Arc::new(Int32Array::from(vec![1, 2, 3, 4])) as ArrayRef,
+        )])
+        .unwrap(),
+        1,
+    )
+    .unwrap_err();
+
+    assert!(
+        err.to_string().contains(
+            "Misaligned buffers[0] in array of type Int32, offset from 
expected alignment of 4 by 1"
+        ),
+        "unexpected error: {err}"
+    );
+}
+
+#[test]
+fn test_misaligned_large_string() {
+    let err = decode_misaligned(
+        RecordBatch::try_from_iter(vec![(
+            "str",
+            Arc::new(LargeStringArray::from(vec!["a", "bb", "ccc"])) as 
ArrayRef,
+        )])
+        .unwrap(),
+        1,
+    )
+    .unwrap_err();
+    assert!(
+        err.to_string().contains(
+            "Misaligned buffers[0] in array of type LargeUtf8, offset from 
expected alignment of 8 by 1"
+        ),
+        "unexpected error: {err}"
+    );
+}
+
+#[test]
+#[should_panic(expected = "Memory pointer is not aligned with the specified 
scalar type")]

Review Comment:
   The dense union panic originates inside arrow-buffer's ScalarBuffer code. 
Not from `decode_misaligned`'s own unwrap() 



-- 
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]

Reply via email to