Skip to content

Commit

Permalink
editoast: add distinct_on parameter to Search derive macro
Browse files Browse the repository at this point in the history
Also fixes the performance issue of the operational point search.

Signed-off-by: Leo Valais <leo.valais97@gmail.com>
  • Loading branch information
leovalais committed Dec 23, 2024
1 parent 3bc6d02 commit c739668
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions editoast/editoast_derive/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use syn::DeriveInput;
)]
struct SearchParams {
table: String,
#[darling(default)]
distinct_on: Option<String>,
migration: Option<Migration>,
#[darling(default)]
joins: String,
Expand Down Expand Up @@ -165,6 +167,9 @@ pub fn expand_search(input: &DeriveInput) -> Result<TokenStream> {

let struct_name = &input.ident;
let table = params.table;
let distinct_on = params
.distinct_on
.map_or_else(|| quote! { None }, |d| quote! { Some(#d.to_owned()) });
let joins = match params.joins {
ref joins if !joins.is_empty() => quote! { Some(#joins.to_owned()) },
_ => quote! { None },
Expand Down Expand Up @@ -286,6 +291,7 @@ pub fn expand_search(input: &DeriveInput) -> Result<TokenStream> {
fn search_config() -> editoast_search::SearchConfig {
editoast_search::SearchConfig {
table: #table.to_owned(),
distinct_on: #distinct_on,
joins: #joins,
criterias: Vec::from([#criterias]),
properties: Vec::from([#properties]),
Expand Down
6 changes: 5 additions & 1 deletion editoast/editoast_search/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ pub fn query_into_sql(
}
let where_expression = context.search_ast_to_sql(&ast)?;
let table = &search_config.table;
let select = search_config.distinct_on.as_ref().map_or_else(
|| "SELECT".to_owned(),
|columns| format!("SELECT DISTINCT ON {columns}"),
);
let joins = search_config.joins.as_ref().cloned().unwrap_or_default();
let result_columns = search_config.result_columns();
let mut bindings = Default::default();
let constraints = where_expression.to_sql(&mut bindings);
let sql_code = format!(
"WITH _RESULT AS (
SELECT {result_columns}
{select} {result_columns}
FROM {table}
{joins}
WHERE {constraints}
Expand Down
1 change: 1 addition & 0 deletions editoast/editoast_search/src/search_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Property {

pub struct SearchConfig {
pub table: String,
pub distinct_on: Option<String>,
pub criterias: Vec<Criteria>,
pub properties: Vec<Property>,
pub joins: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ pub(super) struct SearchResultItemTrack {
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_operational_point",
distinct_on = "(infra_id, obj_id)",
migration(src_table = "infra_object_operational_point"),
joins = "
INNER JOIN infra_object_operational_point AS OP ON OP.id = search_operational_point.id
INNER JOIN (SELECT DISTINCT ON (infra_id, obj_id) * FROM infra_layer_operational_point)
AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id",
INNER JOIN infra_layer_operational_point AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id",
column(
name = "obj_id",
data_type = "varchar(255)",
Expand Down

0 comments on commit c739668

Please sign in to comment.