-
Notifications
You must be signed in to change notification settings - Fork 625
Support Sum/Avg in Android SDK #4735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d8710d
Tests if existing count test passes
cherylEnkidu e0e36c1
add integration tests
cherylEnkidu af1bbd0
Add test coverage
cherylEnkidu 712d03d
Clean up code
cherylEnkidu 3b4ee6a
Address feedback
cherylEnkidu 9991a9a
Address feedback2
cherylEnkidu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,231 changes: 1,231 additions & 0 deletions
1,231
firebase-firestore/src/androidTest/java/com/google/firebase/firestore/AggregationTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.RestrictTo; | ||
import java.util.Objects; | ||
|
||
// TODO(sumavg): Remove the `hide` and scope annotations. | ||
/** @hide */ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY) | ||
public abstract class AggregateField { | ||
@Nullable private final FieldPath fieldPath; | ||
|
||
@NonNull private final String operator; | ||
|
||
@NonNull private final String alias; | ||
|
||
private AggregateField(@Nullable FieldPath fieldPath, @NonNull String operator) { | ||
this.fieldPath = fieldPath; | ||
this.operator = operator; | ||
|
||
// Use $operator_$field format if it's an aggregation of a specific field. For example: sum_foo. | ||
// Use $operator format if there's no field. For example: count. | ||
this.alias = operator + (fieldPath == null ? "" : "_" + fieldPath); | ||
} | ||
|
||
/** | ||
* Returns the field on which the aggregation takes place. Returns an empty string if there's no | ||
* field (e.g. for count). | ||
*/ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY) | ||
@NonNull | ||
public String getFieldPath() { | ||
MarkDuckworth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fieldPath == null ? "" : fieldPath.toString(); | ||
} | ||
|
||
/** Returns the alias used internally for this aggregate field. */ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY) | ||
@NonNull | ||
public String getAlias() { | ||
MarkDuckworth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return alias; | ||
} | ||
|
||
/** Returns a string representation of this aggregation's operator. For example: "sum" */ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY) | ||
@NonNull | ||
public String getOperator() { | ||
MarkDuckworth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return operator; | ||
} | ||
|
||
/** | ||
* Returns true if the given object is equal to this object. Two `AggregateField` objects are | ||
* considered equal if they have the same operator and operate on the same field. | ||
*/ | ||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
if (!(other instanceof AggregateField)) { | ||
return false; | ||
} | ||
|
||
AggregateField otherAggregateField = (AggregateField) other; | ||
if (fieldPath == null || otherAggregateField.fieldPath == null) { | ||
return fieldPath == null && otherAggregateField.fieldPath == null; | ||
} | ||
return operator.equals(otherAggregateField.getOperator()) | ||
&& getFieldPath().equals(otherAggregateField.getFieldPath()); | ||
} | ||
|
||
/** Calculates and returns the hash code for this object. */ | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getOperator(), getFieldPath()); | ||
} | ||
|
||
@NonNull | ||
public static CountAggregateField count() { | ||
return new CountAggregateField(); | ||
} | ||
|
||
@NonNull | ||
public static SumAggregateField sum(@NonNull String field) { | ||
return new SumAggregateField(FieldPath.fromDotSeparatedPath(field)); | ||
} | ||
|
||
@NonNull | ||
public static SumAggregateField sum(@NonNull FieldPath fieldPath) { | ||
return new SumAggregateField(fieldPath); | ||
} | ||
|
||
@NonNull | ||
public static AverageAggregateField average(@NonNull String field) { | ||
return new AverageAggregateField(FieldPath.fromDotSeparatedPath(field)); | ||
} | ||
|
||
@NonNull | ||
public static AverageAggregateField average(@NonNull FieldPath fieldPath) { | ||
return new AverageAggregateField(fieldPath); | ||
} | ||
|
||
public static class CountAggregateField extends AggregateField { | ||
private CountAggregateField() { | ||
super(null, "count"); | ||
} | ||
} | ||
|
||
public static class SumAggregateField extends AggregateField { | ||
private SumAggregateField(@NonNull FieldPath fieldPath) { | ||
super(fieldPath, "sum"); | ||
} | ||
} | ||
|
||
public static class AverageAggregateField extends AggregateField { | ||
private AverageAggregateField(@NonNull FieldPath fieldPath) { | ||
super(fieldPath, "average"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.