Skip to content

Commit

Permalink
count: empty CSVs count as zero also for polars
Browse files Browse the repository at this point in the history
fixes #1741
  • Loading branch information
jqnatividad committed Apr 15, 2024
1 parent 0eefd8d commit 20204b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/cmd/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,19 @@ pub fn polars_count_input(
};

// read the file into a Polars LazyFrame
let lazy_df = LazyCsvReader::new(filepath.clone())
let lazy_df = match LazyCsvReader::new(filepath.clone())
.with_separator(conf.get_delimiter())
.with_comment_prefix(comment_prefix)
.low_memory(low_memory)
.finish()?;
.finish()
{
Ok(lazy_df) => lazy_df,
Err(e) => {
log::warn!("polars error: {}", e);
let (count_regular, _) = count_input(conf, false)?;
return Ok((count_regular, 0));
},
};

// and leverage the magic of Polars SQL with its lazy evaluation, to count the records
// in an optimized manner with its blazing fast multithreaded, mem-mapped CSV reader!
Expand Down
12 changes: 12 additions & 0 deletions tests/test_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ fn count_simple() {
assert_eq!(got, expected.to_string());
}

#[test]
fn count_empty() {
let wrk = Workdir::new("count_empty");
wrk.create_from_string("empty.csv", "");
let mut cmd = wrk.command("count");
cmd.arg("empty.csv");

let got: String = wrk.stdout(&mut cmd);
let expected = "0";
assert_eq!(got, expected.to_string());
}

#[test]
fn count_simple_tsv() {
let wrk = Workdir::new("count_simple_tsv");
Expand Down

0 comments on commit 20204b7

Please sign in to comment.