hzuo commented on code in PR #5554:
URL: https://github.com/apache/arrow-rs/pull/5554#discussion_r1538388341


##########
arrow-ipc/src/writer.rs:
##########
@@ -2234,4 +2270,124 @@ mod tests {
         let in_batch = RecordBatch::try_new(schema, vec![values]).unwrap();
         roundtrip_ensure_sliced_smaller(in_batch, 1000);
     }
+
+    #[test]
+    fn test_decimal128_alignment16() {
+        const IPC_ALIGNMENT: u8 = 16;
+
+        for num_cols in 1..100 {
+            let num_rows = (num_cols * 7 + 11) % 100; // Deterministic swizzle
+
+            let mut fields = Vec::new();
+            let mut arrays = Vec::new();
+            for i in 0..num_cols {
+                let field = Field::new(&format!("col_{}", i), 
DataType::Decimal128(38, 10), true);
+                let array = Decimal128Array::from(vec![num_cols as i128; 
num_rows]);
+                fields.push(field);
+                arrays.push(Arc::new(array) as Arc<dyn Array>);
+            }
+            let schema = Schema::new(fields);
+            let batch = RecordBatch::try_new(Arc::new(schema), 
arrays).unwrap();
+
+            let mut writer = FileWriter::try_new_with_options(
+                Vec::new(),
+                batch.schema_ref(),
+                IpcWriteOptions::try_new(IPC_ALIGNMENT, false, 
MetadataVersion::V5).unwrap(),
+            )
+            .unwrap();
+            writer.write(&batch).unwrap();
+            writer.finish().unwrap();
+
+            let out: Vec<u8> = writer.into_inner().unwrap();
+
+            let buffer = Buffer::from_vec(out);
+            let trailer_start = buffer.len() - 10;
+            let footer_len =
+                
read_footer_length(buffer[trailer_start..].try_into().unwrap()).unwrap();
+            let footer =
+                root_as_footer(&buffer[trailer_start - 
footer_len..trailer_start]).unwrap();
+
+            let schema = fb_to_schema(footer.schema().unwrap());
+            assert_eq!(&schema, batch.schema().as_ref());
+
+            let decoder =
+                FileDecoder::new(Arc::new(schema), 
footer.version()).with_enforce_zero_copy(true);
+
+            assert_eq!(footer.dictionaries().unwrap().len(), 0);
+
+            let batches = footer.recordBatches().unwrap();
+            assert_eq!(batches.len(), 1);
+
+            let block = batches.get(0);
+            let block_len = block.bodyLength() as usize + 
block.metaDataLength() as usize;
+            let data = buffer.slice_with_length(block.offset() as _, 
block_len);
+
+            let batch2 = decoder.read_record_batch(block, 
&data).unwrap().unwrap();
+
+            assert_eq!(batch, batch2);
+        }
+    }
+
+    #[test]
+    fn test_decimal128_alignment8_is_unaligned() {
+        const IPC_ALIGNMENT: u8 = 8;

Review Comment:
   This test is equivalent to the current behavior without the changes in this 
PR, it's equivalent to using `pad_to_8` everywhere.
   
   Because the Decimal128Array is insufficiently aligned, this prevents readers 
from doing a _true_ zero-copy read (e.g. mmapping), because they need to copy 
the block into a more-aligned buffer.



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