Skip to content

Commit d69945e

Browse files
authored
Merge pull request #10 from astronomy-commons/filters_to_non_selected_cols
returns filters columns as well to work with client
2 parents c3d04ae + 3c09737 commit d69945e

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/loaders/parquet/parse_params.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@ use regex::Regex;
88
/// # Returns
99
///
1010
/// A vector of Polars with the selected columns.
11-
pub fn parse_columns_from_params_to_str( params: &HashMap<String, String> ) -> Option<Vec<String>> {
11+
pub fn parse_columns_from_params_to_str(params: &HashMap<String, String>) -> Option<Vec<String>> {
1212
// Parse columns from params
13-
if let Some(cols) = params.get("columns") {
14-
let cols = cols.split(",").collect::<Vec<_>>();
15-
let select_cols = cols.iter().map(|x| x.to_string()).collect::<Vec<_>>();
16-
return Some(select_cols);
13+
14+
// Initialize a set of columns to return
15+
let mut select_cols = if let Some(cols) = params.get("columns") {
16+
cols.split(",").map(|x| x.to_string()).collect::<Vec<_>>()
17+
} else {
18+
Vec::new()
19+
};
20+
21+
// If filters exist, extract and add filter columns if not already present
22+
if let Some(query) = params.get("filters") {
23+
let re = Regex::new(r"([0-9a-zA-Z_]+)([!<>=]+)([-+]?[0-9]*\.?[0-9]*)").unwrap();
24+
25+
for filter in query.split(",") {
26+
if let Some(captures) = re.captures(filter) {
27+
let filter_col = captures.get(1).unwrap().as_str();
28+
29+
// Add filter column only if it's not already in select_cols
30+
if !select_cols.contains(&filter_col.to_string()) {
31+
select_cols.push(filter_col.to_string());
32+
}
33+
}
34+
}
35+
}
36+
37+
// Return Some(select_cols) if not empty, otherwise None
38+
if !select_cols.is_empty() {
39+
Some(select_cols)
40+
} else {
41+
None
1742
}
18-
None
1943
}
2044

2145
/// # Arguments

0 commit comments

Comments
 (0)