Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
fix csv writer buf realloc (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jun 1, 2022
1 parent fe1e576 commit 8ced21b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/io/csv/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fn new_utf8_serializer<'a, O: Offset>(
// in a delimited field
Some("") => buf.extend_from_slice(b"\"\""),
Some(s) => {
if s.len() < local_buf.len() * 3 {
if s.len() * 3 > local_buf.len() {
resize(&mut local_buf, s.len() * 3)
}
match ser_writer.field(s.as_bytes(), &mut local_buf) {
Expand Down
32 changes: 18 additions & 14 deletions tests/it/io/csv/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,22 @@ fn write_escaping() {
#[test]
fn write_escaping_resize_local_buf() {
// tests if local buffer reallocates properly
let a = Utf8Array::<i32>::from_slice(&[
"bar,123456789012345678901234567890123456789012345678901234567890",
]);
let columns = Chunk::new(vec![Arc::new(a) as Arc<dyn Array>]);

let mut writer = vec![];
let options = SerializeOptions::default();
write_chunk(&mut writer, &columns, &options).unwrap();
let csv = std::str::from_utf8(&writer).unwrap();

assert_eq!(
csv,
"\"bar,123456789012345678901234567890123456789012345678901234567890\"\n"
);
for payload in [
"bar,1234567890123456789012345678901234567890123456789012345678900293480293847",
"This is the mail system at host smtp.sciprofiles.com.I'm sorry to have to inform you that your message could notbe delivered to one or more recipients. It's attached below.For further assistance,bar",
] {
let a = Utf8Array::<i32>::from_slice(&[
payload
]);
let columns = Chunk::new(vec![Arc::new(a) as Arc<dyn Array>]);

let mut writer = vec![];
let options = SerializeOptions::default();
write_chunk(&mut writer, &columns, &options).unwrap();
let csv = std::str::from_utf8(&writer).unwrap();
assert_eq!(
csv,
format!("\"{}\"\n", payload)
);
}
}

0 comments on commit 8ced21b

Please sign in to comment.