Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading null booleans from CSV #3523

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions arrow-csv/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ fn build_boolean_array(
.enumerate()
.map(|(row_index, row)| {
let s = row.get(col_idx);
if s.is_empty() {
return Ok(None);
}
let parsed = parse_bool(s);
match parsed {
Some(e) => Ok(Some(e)),
Expand Down Expand Up @@ -1122,6 +1125,7 @@ mod tests {
use std::io::{Cursor, Write};
use tempfile::NamedTempFile;

use arrow_array::cast::as_boolean_array;
use chrono::prelude::*;

#[test]
Expand Down Expand Up @@ -2067,4 +2071,30 @@ mod tests {
assert_eq!(b.num_rows(), expected, "{}", idx);
}
}

#[test]
fn test_null_boolean() {
let csv = "true,false\nFalse,True\n,True\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend also testing in the other column like

False,

let b = ReaderBuilder::new()
.build_buffered(Cursor::new(csv.as_bytes()))
.unwrap()
.next()
.unwrap()
.unwrap();

assert_eq!(b.num_rows(), 3);
assert_eq!(b.num_columns(), 2);

let c = as_boolean_array(b.column(0));
assert_eq!(c.null_count(), 1);
assert!(c.value(0));
assert!(!c.value(1));
assert!(c.is_null(2));

let c = as_boolean_array(b.column(1));
assert_eq!(c.null_count(), 0);
assert!(!c.value(0));
assert!(c.value(1));
assert!(c.value(2));
}
}