Skip to content

Tooling to enable generic keyset pagination across sources by expressing the keyset in CQL2. #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 295 additions & 2 deletions crates/api/src/items.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,160 @@
use crate::{Error, Fields, Filter, Result, Search, Sortby};
use std::collections::HashSet;

use crate::{Error, Fields, Filter, Result, Search, Sortby, Direction};
use chrono::{DateTime, FixedOffset};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use serde_json::{json, Map, Value};
use stac::{Bbox, Item};

/// A trait providing utility methods for JSON operations.
///
/// This trait defines methods for checking JSON operations, combining JSON values,
/// and creating JSON filters for various conditions.
/// A trait for performing JSON-based operations.
pub trait JsonOps {
/// Checks if the operation matches the given string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is Jason+cql2 should it live in cql2-rs?

fn is_op(&self, op: &str) -> bool;

/// Checks if the operation is a logical AND.
fn is_and(&self) -> bool;

/// Checks if the operation is a logical OR.
fn is_or(&self) -> bool;

/// Retrieves the arguments of the operation as a vector of values.
fn args(&self) -> Vec<Value>;

/// Performs a logical AND operation with the given value.
fn _and(&self, v: Value) -> Value;

/// Performs a logical OR operation with the given value.
fn _or(&self, v: Value) -> Value;

/// Checks if the value is not null.
fn _notnull(&self) -> Value;

/// Checks if the value is null.
fn _isnull(&self) -> Value;

/// Compares if the value is greater than the given value.
fn _gt(&self, v: Value) -> Value;

/// Compares if the value is less than the given value.
fn _lt(&self, v: Value) -> Value;

/// Compares if the value is equal to the given value.
fn _eq(&self, v: Value) -> Value;
}

impl JsonOps for Value {
fn is_op(&self, op: &str) -> bool {
if let Some(obj) = self.as_object() {
if let Some(op_value) = obj.get("op") {
if let Some(op_str) = op_value.as_str() {
return op_str == op;
}
}
}
false
}
fn is_and(&self) -> bool {
self.is_op("and")
}

fn is_or(&self) -> bool {
self.is_op("or")
}

fn args(&self) -> Vec<Value> {
if let Some(obj) = self.as_object(){
if let Some(args) = obj.get("args") {
if let Some(args_arr) = args.as_array() {
return args_arr.clone();
}
}
}
vec![self.clone()]
}

fn _and(&self, v: Value) -> Value {
let mut args: Vec<Value> = vec![];
if self.is_and() {
args.extend(self.args());
} else {
args.push(self.clone());
}

if v.is_and() {
args.extend(v.args());
} else {
args.push(v.clone());
}

json!({
"op": "and",
"args": args
})
}

fn _or(&self, v: Value) -> Value {
let mut args: Vec<Value> = vec![];
if self.is_or() {
args.extend(self.args());
} else {
args.push(self.clone());
}

if v.is_or() {
args.extend(v.args());
} else {
args.push(v.clone());
}

json!({
"op": "or",
"args": args
})
}

fn _notnull(&self) -> Value {
json!({
"op": "not",
"args": [{
"op": "isNull",
"args": [self]
}]
})
}

fn _isnull(&self) -> Value {
json!({
"op": "isNull",
"args": [self]
})
}

fn _gt(&self, v: Value) -> Value {
json!({
"op": ">=",
"args": [self, v]
})
}

fn _lt(&self, v: Value) -> Value {
json!({
"op": "<=",
"args": [self, v]
})
}
fn _eq(&self, v: Value) -> Value {
json!({
"op": "=",
"args": [self, v]
})
}
}

/// Parameters for the items endpoint from STAC API - Features.
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct Items {
Expand Down Expand Up @@ -50,6 +200,7 @@ pub struct Items {
/// Additional fields.
#[serde(flatten)]
pub additional_fields: Map<String, Value>,

}

/// GET parameters for the items endpoint from STAC API - Features.
Expand Down Expand Up @@ -299,6 +450,146 @@ impl Items {
}
Ok(self)
}

/// gets sortby ensuring defaults and no duplicates.
/// The default is to sort by datetime descending, then id ascending.
pub fn get_sortby(&self) -> Vec<Sortby>{
let mut outsorts = Vec::new();
let mut seen = HashSet::new();
let idsort = Sortby {
field: "id".to_string(),
direction: Direction::Ascending,
};
let dtsort = Sortby {
field: "datetime".to_string(),
direction: Direction::Descending,
};
if self.sortby.is_empty() {
outsorts.push(dtsort);
}
for s in self.sortby.iter() {
if seen.contains("id") {
outsorts.push(s.clone());
break;
} else if seen.contains(&s.field) {
continue;
} else {
_ = seen.insert(s.field.clone());
outsorts.push(s.clone());
}
}
if ! seen.contains("id") {
outsorts.push(idsort);
}
outsorts
}

/// Uses the sorting from the items structure to calculate a cql2-json filter to get the next page.
/// # Examples
///
/// ```
/// use stac_api::Items;
/// use stac::Item;
/// use serde_json::json;
/// use stac_api::JsonOps;
///
/// let itemjson = json!({"id": "item-id", "datetime": "2023-10-09T00:00:00Z"});
/// let item: Item = serde_json::from_value(itemjson).unwrap();
/// let items = Items::default();
/// let filter = items.next_page_cql2(&item);
///
/// dbg!(&filter);
/// assert_eq!(filter, json!({
/// "op": "or",
/// "args": [
/// {
/// "op": "<=",
/// "args": [
/// {"property": "datetime"},
/// {"timestamp": "2023-10-09T00:00:00Z"}
/// ]
/// },
/// {
/// "op": "isNull",
/// "args": [
/// {"property": "datetime"}
/// ]
/// },
/// {
/// "op": "and",
/// "args": [
/// {
/// "op": "=",
/// "args": [
/// {"property": "datetime"},
/// {"timestamp": "2023-10-09T00:00:00Z"}
/// ]
/// },
/// {
/// "op": ">=",
/// "args": [
/// {"property": "id"},
/// "item-id"
/// ]
/// }
/// ]
/// }
/// ]
/// }));
/// ```
pub fn next_page_cql2(&self, next_item: &Item) -> Value {
let sortby = self.get_sortby();

let mut first_run = true;
let mut andfilters: Value = json!({});
let mut orfilters: Value = json!({});

for sort in sortby {
let property = json!({
"property": sort.field
});

let field_value = next_item
.get_property(&sort.field);

let equality_filter: Value;

match &field_value {
Some(v) => {
equality_filter = property._eq(v.clone());
}
None => {
equality_filter = property._isnull();
}
}

let base_filter = match (sort.direction, &field_value) {
(Direction::Descending, None) => {
property._isnull()
}
(Direction::Descending, Some(v)) => {
property._lt(v.clone())._or(property._isnull())
}
(Direction::Ascending, None) => {
property._notnull()
}
(Direction::Ascending, Some(v)) => {
property._gt(v.clone().clone())
}
};


if first_run {
andfilters = equality_filter;
orfilters = base_filter;
first_run = false;
} else {
orfilters = orfilters._or(andfilters._and(base_filter));
andfilters = andfilters._and(equality_filter);
}
}
orfilters
}
}

impl TryFrom<Items> for GetItems {
Expand Down Expand Up @@ -409,6 +700,8 @@ impl stac::Fields for Items {
}
}



fn maybe_parse_from_rfc3339(s: &str) -> Result<Option<DateTime<FixedOffset>>> {
if s.is_empty() || s == ".." {
Ok(None)
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub use error::Error;
pub use fields::Fields;
pub use filter::Filter;
pub use item_collection::{Context, ItemCollection};
pub use items::{GetItems, Items};
pub use items::{GetItems, Items, JsonOps};
pub use root::Root;
pub use search::{GetSearch, Search};
pub use sort::{Direction, Sortby};
Expand Down
21 changes: 20 additions & 1 deletion crates/core/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chrono::{DateTime, FixedOffset, NaiveDateTime, Utc};
use geojson::{Feature, Geometry, feature::Id};
use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
use serde_json::{json, Map, Value};
use stac_derive::{Links, Migrate, SelfHref};
use std::path::Path;
use url::Url;
Expand Down Expand Up @@ -590,6 +590,25 @@ impl Item {
properties,
})
}

/// Gets a property by key looking in order from root level then properties.
pub fn get_property(&self, key: &str) -> Option<Value> {
let j: Map<String, Value> = self.clone().try_into().expect("Could not convert item to Map");
let val = j
.get(key)
.or_else(
|| j.get("properties").unwrap().get(key)
)
.or_else(
|| j.get("properties").unwrap().get("additional_fields").unwrap().get(key)
)
.map(|v| v.clone());
if val.is_some() && key.ends_with("datetime") {
Some(json!({"timestamp": val.unwrap()}))
} else {
val
}
}
}

impl Assets for Item {
Expand Down
Loading