From 0b423e69a617f4e267fcd26ef49eddd19bcad5bf Mon Sep 17 00:00:00 2001 From: Alexey Orlenko Date: Sat, 27 Jan 2024 01:16:10 +0100 Subject: [PATCH] Implement aggregation queries --- .../src/query_builder/select.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/query-engine/connectors/sql-query-connector/src/query_builder/select.rs b/query-engine/connectors/sql-query-connector/src/query_builder/select.rs index 08ad6b6b214b..fc8702fe5f7d 100644 --- a/query-engine/connectors/sql-query-connector/src/query_builder/select.rs +++ b/query-engine/connectors/sql-query-connector/src/query_builder/select.rs @@ -230,8 +230,8 @@ impl SelectBuilder { match vs { VirtualSelection::RelationCount(rf, filter) => { let table_alias = relation_count_alias_name(rf); - let table = - Table::from(self.build_relation_count_query(rf, filter, parent_alias, ctx)).alias(table_alias); + let table = Table::from(self.build_relation_count_query(vs.db_alias(), rf, filter, parent_alias, ctx)) + .alias(table_alias); select.left_join_lateral(table.on(ConditionTree::single(true.raw()))) } @@ -240,12 +240,25 @@ impl SelectBuilder { fn build_relation_count_query<'a>( &mut self, + selection_name: impl Into>, rf: &RelationField, filter: &Option, parent_alias: Alias, ctx: &Context<'_>, ) -> Select<'a> { - todo!() + let related_table_alias = self.next_alias(); + + let related_table = rf + .related_model() + .as_table(ctx) + .alias(related_table_alias.to_table_string()); + + let select = Select::from_table(related_table) + .value(count(asterisk()).alias(selection_name)) + .with_join_conditions(rf, parent_alias, related_table_alias, ctx) + .with_filters(filter.clone(), Some(related_table_alias), ctx); + + select } }