Skip to content

Commit 54d4d70

Browse files
sum avg (#11650)
1 parent eaf81fa commit 54d4d70

18 files changed

+462
-665
lines changed

Firestore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.17.0
2+
- [feature] Add support for sum and average aggregate queries.
3+
14
# 10.16.0
25
- [fixed] Fixed an issue where Firestore's binary SwiftPM distribution would
36
not link properly when building a target for testing. This issue affected

Firestore/Example/Tests/Integration/API/FIRAggregateTests.mm

Lines changed: 116 additions & 239 deletions
Large diffs are not rendered by default.

Firestore/Example/Tests/Integration/API/FIRQueryTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,14 +1508,14 @@ - (void)testMultipleInequalityInAggregateQuery {
15081508
[FIRAggregateField aggregateFieldForSumOfField:@"sort"],
15091509
[FIRAggregateField aggregateFieldForAverageOfField:@"v"],
15101510
]]];
1511-
XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
1511+
XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForCount]],
15121512
[NSNumber numberWithLong:3L]);
15131513
XCTAssertEqual(
1514-
[[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"sort"]]
1514+
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"sort"]]
15151515
longValue],
15161516
6L);
15171517
XCTAssertEqual(
1518-
[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"v"]],
1518+
[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForAverageOfField:@"v"]],
15191519
[NSNumber numberWithDouble:1.0]);
15201520
}
15211521

Firestore/Source/API/FIRAggregateField.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
#import "FIRAggregateField.h"
1818

19-
#include <string>
20-
2119
#import "Firestore/Source/API/FIRAggregateField+Internal.h"
2220
#import "Firestore/Source/API/FIRFieldPath+Internal.h"
2321

22+
#import "Firestore/core/src/model/aggregate_field.h"
23+
2424
using firebase::firestore::model::AggregateField;
2525

2626
NS_ASSUME_NONNULL_BEGIN

Firestore/Source/API/FIRAggregateQuery+Internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ NS_ASSUME_NONNULL_BEGIN
2424
@interface FIRAggregateQuery (/* init */)
2525

2626
- (instancetype)init NS_UNAVAILABLE;
27-
- (instancetype)initWithQueryAndAggregations:(FIRQuery *)query
28-
aggregations:(NSArray<FIRAggregateField *> *)aggregations
27+
- (instancetype)initWithQuery:(FIRQuery *)query
28+
aggregateFields:(NSArray<FIRAggregateField *> *)aggregations
2929
NS_DESIGNATED_INITIALIZER;
3030

3131
@end

Firestore/Source/API/FIRAggregateQuery.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ @implementation FIRAggregateQuery {
3737
std::unique_ptr<AggregateQuery> _aggregateQuery;
3838
}
3939

40-
- (instancetype)initWithQueryAndAggregations:(FIRQuery *)query
41-
aggregations:(NSArray<FIRAggregateField *> *)aggregations {
40+
- (instancetype)initWithQuery:(FIRQuery *)query
41+
aggregateFields:(NSArray<FIRAggregateField *> *)aggregateFields {
4242
if (self = [super init]) {
4343
_query = query;
4444

4545
std::vector<AggregateField> _aggregateFields;
46-
for (FIRAggregateField *field in aggregations) {
46+
for (FIRAggregateField *field in aggregateFields) {
4747
_aggregateFields.push_back([field createInternalValue]);
4848
}
4949

Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,4 @@ NS_ASSUME_NONNULL_BEGIN
3535

3636
@end
3737

38-
// TODO(sum/avg) move the contents of this FuturePublicApi category to
39-
// ../Public/FirebaseFirestore/FIRAggregateQuerySnapshot.h
40-
@interface FIRAggregateQuerySnapshot (FuturePublicApi)
41-
42-
/**
43-
* Gets the aggregation result for the specified aggregation without loss of precision. No coercion
44-
* of data types or values is performed.
45-
*
46-
* See the `AggregateField` class for the expected aggregration result values and types. Numeric
47-
* aggregation results will be boxed in an `NSNumber`.
48-
*
49-
* @param aggregation An instance of `AggregateField` that specifies which aggregation result to
50-
* return.
51-
* @return Returns the aggregation result from the server without loss of precision.
52-
* @warning Throws an `InvalidArgument` exception if the aggregation was not requested in the
53-
* `AggregateQuery`.
54-
* @see `AggregateField`
55-
*/
56-
- (nullable id)valueForAggregation:(FIRAggregateField *)aggregation NS_SWIFT_NAME(get(_:));
57-
58-
@end
59-
6038
NS_ASSUME_NONNULL_END

Firestore/Source/API/FIRAggregateQuerySnapshot.mm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,25 @@ - (NSUInteger)hash {
7171
#pragma mark - Public Methods
7272

7373
- (NSNumber *)count {
74-
return (NSNumber *)[self valueForAggregation:[FIRAggregateField aggregateFieldForCount]];
74+
return (NSNumber *)[self valueForAggregateField:[FIRAggregateField aggregateFieldForCount]];
7575
}
7676

7777
- (FIRAggregateQuery *)query {
7878
return _query;
7979
}
8080

81-
- (nullable id)valueForAggregation:(FIRAggregateField *)aggregation {
81+
- (id)valueForAggregateField:(FIRAggregateField *)aggregateField {
8282
FIRServerTimestampBehavior serverTimestampBehavior = FIRServerTimestampBehaviorNone;
83-
AggregateAlias alias = [aggregation createAlias];
83+
AggregateAlias alias = [aggregateField createAlias];
8484
absl::optional<google_firestore_v1_Value> fieldValue = _result.Get(alias.StringValue());
8585
if (!fieldValue) {
8686
std::string path{""};
87-
if (aggregation.fieldPath) {
88-
path = [aggregation.fieldPath internalValue].CanonicalString();
87+
if (aggregateField.fieldPath) {
88+
path = [aggregateField.fieldPath internalValue].CanonicalString();
8989
}
9090

91-
ThrowInvalidArgument("'%s(%s)' was not requested in the aggregation query.", [aggregation name],
92-
path);
91+
ThrowInvalidArgument("'%s(%s)' was not requested in the aggregation query.",
92+
[aggregateField name], path);
9393
}
9494
FSTUserDataWriter *dataWriter =
9595
[[FSTUserDataWriter alloc] initWithFirestore:_query.query.firestore.wrapped

Firestore/Source/API/FIRQuery+Internal.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,4 @@ NS_ASSUME_NONNULL_BEGIN
4747

4848
@end
4949

50-
// TODO(sum/avg) move the contents of this FuturePublicApi category to
51-
// ../Public/FirebaseFirestore/FIRAggregateQuerySnapshot.h
52-
@interface FIRQuery (FuturePublicApi)
53-
54-
/**
55-
* Creates and returns a new `AggregateQuery` that aggregates the documents in the result set
56-
* of this query, without actually downloading the documents.
57-
*
58-
* Using an `AggregateQuery` to perform aggregations is efficient because only the final aggregation
59-
* values, not the documents' data, is downloaded. The query can even aggregate the documents if the
60-
* result set would be prohibitively large to download entirely (e.g. thousands of documents).
61-
*
62-
* @param aggregations Specifies the aggregation operations to perform on the result set of this
63-
* query.
64-
*
65-
* @return An `AggregateQuery` encapsulating this `Query` and `AggregateField`s, which can be used
66-
* to query the server for the aggregation results.
67-
*/
68-
- (FIRAggregateQuery *)aggregate:(NSArray<FIRAggregateField *> *)aggregations
69-
NS_SWIFT_NAME(aggregate(_:));
70-
71-
@end
72-
7350
NS_ASSUME_NONNULL_END

Firestore/Source/API/FIRQuery.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues {
485485

486486
- (FIRAggregateQuery *)count {
487487
FIRAggregateField *countAF = [FIRAggregateField aggregateFieldForCount];
488-
return [[FIRAggregateQuery alloc] initWithQueryAndAggregations:self aggregations:@[ countAF ]];
488+
return [[FIRAggregateQuery alloc] initWithQuery:self aggregateFields:@[ countAF ]];
489489
}
490490

491-
- (FIRAggregateQuery *)aggregate:(NSArray<FIRAggregateField *> *)aggregations {
492-
return [[FIRAggregateQuery alloc] initWithQueryAndAggregations:self aggregations:aggregations];
491+
- (FIRAggregateQuery *)aggregate:(NSArray<FIRAggregateField *> *)aggregateFields {
492+
return [[FIRAggregateQuery alloc] initWithQuery:self aggregateFields:aggregateFields];
493493
}
494494

495495
#pragma mark - Private Methods

0 commit comments

Comments
 (0)