Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: Add Aggregate method to Query
  • Loading branch information
KKimj committed Apr 1, 2025
commit 2d2ecf2bb983afc3968cd937f1fd9a221a988959
6 changes: 6 additions & 0 deletions firestore/src/common/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ AggregateQuery Query::Count() const {
return internal_->Count();
}

AggregateQuery Query::Aggregate(const AggregateField& aggregate_field) const {
if (!internal_) return {};
return internal_->Aggregate(aggregate_field);
}


Query Query::Where(const Filter& filter) const {
if (!internal_) return {};
if (filter.IsEmpty()) {
Expand Down
16 changes: 16 additions & 0 deletions firestore/src/include/firebase/firestore/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ class Query {
*/
virtual AggregateQuery Count() const;

/**
* @brief Returns a query that calculates the specified aggregation over the
* documents in the result set of this query.
*
* The returned query, when executed, performs the specified aggregation
* without downloading the actual document data.
*
* Using the returned query to calculate aggregations is efficient because
* only the aggregation result, not the documents' data, is downloaded. This
* can be useful for aggregations over large result sets.
*
* @param aggregate_field The field to aggregate on.
* @return An aggregate query that performs the specified aggregation.
*/
virtual AggregateQuery Aggregate(const AggregateField& aggregate_field) const;

/**
* @brief Creates and returns a new Query with the additional filter.
*
Expand Down
5 changes: 5 additions & 0 deletions firestore/src/main/query_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ Future<QuerySnapshot> QueryInternal::Get(Source source) {

AggregateQuery QueryInternal::Count() { return MakePublic(query_.Count()); }

AggregateQuery QueryInternal::Aggregate(const AggregateField& aggregate_field) {
// TODO: Implement Aggregate
// return MakePublic(aggregate_field);
}

Query QueryInternal::Where(const Filter& filter) const {
SIMPLE_HARD_ASSERT(!filter.IsEmpty());
core::Filter core_filter =
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/main/query_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class QueryInternal {

AggregateQuery Count();

AggregateQuery Aggregate(const AggregateField& aggregate_field);

ListenerRegistration AddSnapshotListener(
MetadataChanges metadata_changes, EventListener<QuerySnapshot>* listener);

Expand Down