tustvold commented on code in PR #2040:
URL: https://github.com/apache/arrow-rs/pull/2040#discussion_r921477386


##########
arrow/src/ipc/writer.rs:
##########
@@ -1501,10 +1690,151 @@ mod tests {
     #[test]
     fn test_write_union_file_v4_v5() {
         write_union_file(
-            IpcWriteOptions::try_new(8, false, MetadataVersion::V4).unwrap(),
+            IpcWriteOptions::try_new(8, false, MetadataVersion::V4, 
false).unwrap(),
         );
         write_union_file(
-            IpcWriteOptions::try_new(8, false, MetadataVersion::V5).unwrap(),
+            IpcWriteOptions::try_new(8, false, MetadataVersion::V5, 
false).unwrap(),
+        );
+    }
+
+    fn serialize_with_truncate(record: &RecordBatch) -> Vec<u8> {
+        let write_options = IpcWriteOptions::default();
+        serialize(record, write_options)
+    }
+
+    fn serialize_without_truncate(record: &RecordBatch) -> Vec<u8> {
+        let write_options =
+            IpcWriteOptions::try_new(8, false, MetadataVersion::V5, 
false).unwrap();
+        serialize(record, write_options)
+    }
+
+    fn serialize(record: &RecordBatch, write_options: IpcWriteOptions) -> 
Vec<u8> {
+        let buffer: Vec<u8> = Vec::new();
+        let mut stream_writer =
+            StreamWriter::try_new_with_options(buffer, &record.schema(), 
write_options)
+                .unwrap();
+        stream_writer.write(record).unwrap();
+        stream_writer.finish().unwrap();
+        stream_writer.into_inner().unwrap()
+    }
+
+    fn deserialize(bytes: Vec<u8>) -> RecordBatch {
+        let mut stream_reader =
+            ipc::reader::StreamReader::try_new(std::io::Cursor::new(bytes), 
None)
+                .unwrap();
+        stream_reader.next().unwrap().unwrap()
+    }
+
+    #[test]
+    fn truncate_ipc_record_batch() {
+        fn create_batch(rows: usize) -> RecordBatch {
+            let schema = Schema::new(vec![
+                Field::new("a", DataType::Int32, false),
+                Field::new("b", DataType::Utf8, false),
+            ]);
+
+            let a = Int32Array::from(vec![1; rows]);
+            let b = StringArray::from(vec!["a"; rows]);
+
+            RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), 
Arc::new(b)])
+                .unwrap()
+        }
+
+        let big_record_batch = create_batch(65536);
+
+        let length = 5;
+        let small_record_batch = create_batch(length);
+
+        let offset = 2;
+        let record_batch_slice = big_record_batch.slice(offset, length);
+        assert!(
+            serialize_with_truncate(&big_record_batch).len()
+                > serialize_with_truncate(&small_record_batch).len()
+        );
+        assert_eq!(
+            serialize_with_truncate(&small_record_batch).len(),
+            serialize_with_truncate(&record_batch_slice).len()
+        );
+
+        assert_eq!(
+            deserialize(serialize_with_truncate(&record_batch_slice)),
+            small_record_batch
+        );
+        assert_eq!(
+            deserialize(serialize_with_truncate(&record_batch_slice)),
+            record_batch_slice
         );
+

Review Comment:
   ```suggestion
           assert_eq!(
               deserialize(serialize_without_truncate(&record_batch_slice)),
               record_batch_slice
           );
   ```
   Or something, basically I think the previous logic was wrong.



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