Skip to content

Commit

Permalink
Code improvments to remove unwrap and intos
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartlynn committed Apr 26, 2024
1 parent 2cdca39 commit bc0cfd2
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct MetricRequest {
fn get_metrics_from_file(
file_url: &String,
columns: &[String],
geo_ids: Option<&[String]>,
geo_ids: Option<&[&str]>,
) -> Result<DataFrame> {
let mut cols: Vec<Expr> = columns.iter().map(|c| col(c)).collect();
cols.push(col(GEO_ID_COL_NAME));
Expand All @@ -39,7 +39,7 @@ fn get_metrics_from_file(
/// Given a set of metrics and optional geo_ids, this function will
/// retrive all the required metrics from the cloud blob storage
///
pub fn get_metrics(metrics: &[MetricRequest], geo_ids: Option<&[String]>) -> Result<DataFrame> {
pub fn get_metrics(metrics: &[MetricRequest], geo_ids: Option<&[&str]>) -> Result<DataFrame> {
let file_list: HashSet<String> = metrics.iter().map(|m| m.file.clone()).collect();

let dfs: Result<Vec<DataFrame>> = file_list
Expand All @@ -59,22 +59,22 @@ pub fn get_metrics(metrics: &[MetricRequest], geo_ids: Option<&[String]>) -> Res
})
.collect();

let joined_df = dfs?.iter().fold(None, |acc, df| match acc {
None => Some(df.clone()),
Some(acc_df) => Some(
// This unwrap should be ok because we would expect the
// previous calls to fail if "GEOID" is not present in
// it's columns
acc_df
.join(
df,
vec![GEO_ID_COL_NAME],
vec![GEO_ID_COL_NAME],
JoinArgs::new(JoinType::Inner),
)
.unwrap(),
),
});
let mut joined_df: Option<DataFrame> = None;

// Merge the dataframes from each remove file in to a single
// dataframe
for df in dfs? {
if let Some(prev_dfs) = joined_df {
joined_df = Some(prev_dfs.join(
&df,
vec![GEO_ID_COL_NAME],
vec![GEO_ID_COL_NAME],
JoinArgs::new(JoinType::Inner),
)?);
} else {
joined_df = Some(df.clone());
}
}

joined_df.with_context(|| "Failed to combine data queries")
}
Expand All @@ -91,7 +91,6 @@ mod tests {
column:"B17021_E006".into()
}];
let df = get_metrics(&metrics, None);
println!("{df:#?}");
assert!(df.is_ok(), "We should get back a result");
let df = df.unwrap();
assert_eq!(
Expand Down Expand Up @@ -123,10 +122,9 @@ mod tests {
}];
let df = get_metrics(
&metrics,
Some(&["1400000US01001020100".into(), "1400000US01001020300".into()]),
Some(&["1400000US01001020100", "1400000US01001020300"]),
);

println!("{df:#?}");
assert!(df.is_ok(), "We should get back a result");
let df = df.unwrap();
assert_eq!(
Expand Down

0 comments on commit bc0cfd2

Please sign in to comment.