use super::super::table::RawCsv;
use std::collections::HashSet;
impl RawCsv {
pub(super) fn clone_raw(&self) -> RawCsv {
RawCsv {
headers: self.headers.clone(),
rows: self.rows.clone(),
nulls: self.nulls.clone(),
row_ids: self.row_ids.clone(),
}
}
}
pub(super) fn dedupe_by_pk(raw: &RawCsv, pk_col: &str) -> RawCsv {
let Some(idx) = raw.col_index(pk_col) else {
return raw.clone_raw();
};
let mut seen: HashSet<String> = HashSet::new();
let mut new_rows = Vec::new();
let mut new_nulls = Vec::new();
let mut new_row_ids = Vec::new();
for r in 0..raw.row_count() {
if raw.nulls[r][idx] {
new_rows.push(raw.rows[r].clone());
new_nulls.push(raw.nulls[r].clone());
new_row_ids.push(raw.row_id(r));
continue;
}
let key = raw.rows[r][idx].clone();
if seen.insert(key) {
new_rows.push(raw.rows[r].clone());
new_nulls.push(raw.nulls[r].clone());
new_row_ids.push(raw.row_id(r));
}
}
RawCsv {
headers: raw.headers.clone(),
rows: new_rows,
nulls: new_nulls,
row_ids: new_row_ids,
}
}
pub(super) fn subset_rows(raw: &RawCsv, rows: &[usize]) -> RawCsv {
RawCsv {
headers: raw.headers.clone(),
rows: rows.iter().map(|&r| raw.rows[r].clone()).collect(),
nulls: rows.iter().map(|&r| raw.nulls[r].clone()).collect(),
row_ids: rows.iter().map(|&r| raw.row_id(r)).collect(),
}
}