Skip to content

Commit

Permalink
feat: implement fields for duckdb (#584)
Browse files Browse the repository at this point in the history
- Closes #581
  • Loading branch information
gadomski authored Jan 2, 2025
1 parent 0a5a045 commit 91aedfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- Convenience methods on `Search` ([#562](https://github.com/stac-utils/stac-rs/pull/562))
- Convenience methods on `Search` ([#562](https://github.com/stac-utils/stac-rs/pull/562), [#584](https://github.com/stac-utils/stac-rs/pull/584))

### Changed

Expand Down
8 changes: 7 additions & 1 deletion crates/api/src/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, GetItems, Items, Result, Sortby};
use crate::{Error, Fields, GetItems, Items, Result, Sortby};
use geojson::Geometry;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -112,6 +112,12 @@ impl Search {
self
}

/// Sets the fields of this search.
pub fn fields(mut self, fields: Fields) -> Search {
self.items.fields = Some(fields);
self
}

/// Returns an error if this search is invalid, e.g. if both bbox and intersects are specified.
///
/// Returns the search unchanged if it is valid.
Expand Down
31 changes: 26 additions & 5 deletions crates/duckdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl Client {
// Get suffix information early so we can take ownership of other parts of search as we go along.
let limit = search.items.limit.take();
let sortby = std::mem::take(&mut search.items.sortby);
let fields = std::mem::take(&mut search.items.fields);

let mut statement = self.connection.prepare(&format!(
"SELECT column_name FROM (DESCRIBE SELECT * from read_parquet('{}'))",
Expand All @@ -138,17 +139,26 @@ impl Client {
let mut has_end_datetime: bool = false;
for row in statement.query_map([], |row| row.get::<_, String>(0))? {
let column = row?;
if column == "geometry" {
columns.push("ST_AsWKB(geometry) geometry".to_string());
continue;
}
if column == "start_datetime" {
has_start_datetime = true;
}
if column == "end_datetime" {
has_end_datetime = true;
}
columns.push(format!("\"{}\"", column));

if let Some(fields) = fields.as_ref() {
if fields.exclude.contains(&column)
|| !(fields.include.is_empty() || fields.include.contains(&column))
{
continue;
}
}

if column == "geometry" {
columns.push("ST_AsWKB(geometry) geometry".to_string());
} else {
columns.push(format!("\"{}\"", column));
}
}

let mut wheres = Vec::new();
Expand Down Expand Up @@ -419,4 +429,15 @@ mod tests {
"S2B_MSIL2A_20241203T174629_R098_T13TDE_20241203T211406"
);
}

#[rstest]
fn search_fields(client: Client) {
let item_collection = client
.search_to_json(
"data/100-sentinel-2-items.parquet",
Search::default().fields("+id".parse().unwrap()).limit(1),
)
.unwrap();
assert_eq!(item_collection.items[0].len(), 1);
}
}

0 comments on commit 91aedfc

Please sign in to comment.