Skip to content

Commit

Permalink
also use polars count with util::count_rows() helper if polars featur…
Browse files Browse the repository at this point in the history
…es is enabled
  • Loading branch information
jqnatividad committed Mar 15, 2024
1 parent 41ad1ab commit 8d321fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cmd/count.rs
Expand Up @@ -155,7 +155,7 @@ fn count_input(
}

#[cfg(feature = "polars")]
fn polars_count_input(
pub fn polars_count_input(
conf: &Config,
low_memory: bool,
) -> Result<(u64, usize), crate::clitypes::CliError> {
Expand Down
26 changes: 25 additions & 1 deletion src/util.rs
Expand Up @@ -21,6 +21,8 @@ use serde::de::DeserializeOwned;
use serde::de::{Deserialize, Deserializer, Error};
use sysinfo::System;

#[cfg(feature = "polars")]
use crate::cmd::count::polars_count_input;
use crate::{
config,
config::{Config, Delimiter, DEFAULT_WTR_BUFFER_CAPACITY},
Expand Down Expand Up @@ -293,10 +295,32 @@ pub fn count_rows(conf: &Config) -> Result<u64, CliError> {
Ok(idx.count())
} else {
// index does not exist or is stale,
// count records by iterating through records
// count records by using polars mem-mapped reader if available
// otherwise, count records by iterating through records
// Do this only once per invocation and cache the result in ROW_COUNT,
// so we don't have to re-count rows every time we need to know the
// rowcount for CSVs that don't have an index.
#[cfg(feature = "polars")]
let count_opt = ROW_COUNT.get_or_init(|| {
if let Ok((count, _)) = polars_count_input(conf, false) {
Some(count)
} else {
// if polars_count_input fails, fall back to regular CSV reader
if let Ok(mut rdr) = conf.reader() {
let mut count = 0_u64;
let mut _record = csv::ByteRecord::new();
#[allow(clippy::used_underscore_binding)]
while rdr.read_byte_record(&mut _record).unwrap_or_default() {
count += 1;
}
Some(count)
} else {
None
}
}
});

#[cfg(not(feature = "polars"))]
let count_opt = ROW_COUNT.get_or_init(|| {
if let Ok(mut rdr) = conf.reader() {
let mut count = 0_u64;
Expand Down

0 comments on commit 8d321fe

Please sign in to comment.