Skip to content

Commit bf9773d

Browse files
authored
SDK - Allow ordering by columns in the documents table (#1462)
1 parent 548998f commit bf9773d

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

pgml-sdks/pgml/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,6 @@ mod tests {
15841584
"nested_number": {
15851585
"number": 3
15861586
},
1587-
15881587
"tie": 2,
15891588
})
15901589
.into(),
@@ -1646,6 +1645,26 @@ mod tests {
16461645
.collect::<Vec<_>>(),
16471646
vec![1, 2, 3]
16481647
);
1648+
let documents = collection
1649+
.get_documents(Some(json!({"order_by": { "COLUMN_id": "desc"}}).into()))
1650+
.await?;
1651+
assert_eq!(
1652+
documents
1653+
.iter()
1654+
.map(|d| d["row_id"].as_i64().unwrap())
1655+
.collect::<Vec<_>>(),
1656+
vec![3, 2, 1]
1657+
);
1658+
let documents = collection
1659+
.get_documents(Some(json!({"order_by": { "COLUMN_id": "asc"}}).into()))
1660+
.await?;
1661+
assert_eq!(
1662+
documents
1663+
.iter()
1664+
.map(|d| d["row_id"].as_i64().unwrap())
1665+
.collect::<Vec<_>>(),
1666+
vec![1, 2, 3]
1667+
);
16491668
collection.archive().await?;
16501669
Ok(())
16511670
}

pgml-sdks/pgml/src/order_by_builder.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@ pub(crate) struct OrderByBuilder<'a> {
77
column_name: &'a str,
88
}
99

10+
fn str_to_order(order: &str) -> anyhow::Result<Order> {
11+
match order {
12+
"asc" | "ASC" => Ok(Order::Asc),
13+
"desc" | "DESC" => Ok(Order::Desc),
14+
_ => anyhow::bail!("Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax"),
15+
}
16+
}
17+
1018
fn build_recursive_access(key: &str, value: &serde_json::Value) -> anyhow::Result<(String, Order)> {
1119
if value.is_object() {
1220
let (new_key, new_value) = value
1321
.as_object()
1422
.unwrap()
1523
.iter()
1624
.next()
17-
.context("Invalid order by")?;
25+
.context("Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax")?;
1826
let (path, order) = build_recursive_access(new_key, new_value)?;
1927
let path = format!("{},{}", key, path);
2028
Ok((path, order))
2129
} else if value.is_string() {
22-
let order = match value.as_str().unwrap() {
23-
"asc" | "ASC" => Order::Asc,
24-
"desc" | "DESC" => Order::Desc,
25-
_ => return Err(anyhow::anyhow!("Invalid order by")),
26-
};
30+
let order = str_to_order(value.as_str().unwrap())?;
2731
Ok((key.to_string(), order))
2832
} else {
29-
Err(anyhow::anyhow!("Invalid order by"))
33+
Err(anyhow::anyhow!("Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax"))
3034
}
3135
}
3236

@@ -42,17 +46,22 @@ impl<'a> OrderByBuilder<'a> {
4246
pub fn build(self) -> anyhow::Result<Vec<(SimpleExpr, Order)>> {
4347
self.filter
4448
.as_object()
45-
.context("Invalid order by")?
49+
.context("`order_by` must be an object")?
4650
.iter()
4751
.map(|(k, v)| {
48-
if let Ok((path, order)) = build_recursive_access(k, v) {
52+
if k.starts_with("COLUMN_") {
53+
Ok((
54+
Expr::cust(k.replace("COLUMN_", "")),
55+
str_to_order(v.as_str().context("Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax")?)?,
56+
))
57+
} else if let Ok((path, order)) = build_recursive_access(k, v) {
4958
let expr = Expr::cust(format!(
5059
"\"{}\".\"{}\"#>'{{{}}}'",
5160
self.table_name, self.column_name, path
5261
));
5362
Ok((expr, order))
5463
} else {
55-
Err(anyhow::anyhow!("Invalid order by"))
64+
Err(anyhow::anyhow!("Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax"))
5665
}
5766
})
5867
.collect()

0 commit comments

Comments
 (0)