-
Notifications
You must be signed in to change notification settings - Fork 938
Mila/count #6597
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
Mila/count #6597
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
b92e492
Mila/count update proto (#6482)
milaGGL 598e948
compile protos and upload the generated json file (#6491)
milaGGL 7f42490
Mila/count add api interface (#6499)
milaGGL 981e6f1
create sample count test cases (#6506)
milaGGL bc61df7
Mila/count implement count query (#6528)
milaGGL c21304b
add test case for terminated firestore
milaGGL a3242ce
Mila/count add tests (#6566)
milaGGL 6da4f4f
Mila/count export aggregate to api (#6575)
milaGGL fe871ce
change int32 to int64 (#6577)
milaGGL 507e984
check client offline state and test (#6579)
milaGGL 98189d5
Mila/count non lite api tests (#6581)
milaGGL 0ebab69
Mila/count update api surface (#6589)
milaGGL 4b273fe
remove DocumentFieldValue
milaGGL a9de825
Merge branch 'master' into mila/count
milaGGL 576d202
fix lint error: Symbol not found for identifier
milaGGL dd693c6
format to pass lint check
milaGGL 83fbc8d
remove the exports of aggregate query functions/types
milaGGL 26a0bd2
Update aggregation.test.ts
milaGGL 9cced28
skip count query test cases if not using emulator
milaGGL f490bfe
update the type of skipTestUnlessUsingEmulator
milaGGL 078d581
roll back to any type for skipTestUnlessUsingEmulator
milaGGL a9ee57a
move aggregation test file into api_internal folder
milaGGL dbbc2f8
resolve comments
milaGGL c12d5a0
reformat to pass lint check
milaGGL 74116a4
add changeset as requested by github
milaGGL de9dd9a
remove the changeset
milaGGL 9438594
Merge branch 'master' into mila/count
milaGGL 3a6cc27
add the changeset back
milaGGL 0267c69
resolve comments
milaGGL 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
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,5 @@ | ||
--- | ||
'@firebase/firestore': minor | ||
--- | ||
|
||
Implement count query for internal use. |
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,39 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 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. | ||
*/ | ||
|
||
import { Query } from '../api'; | ||
import { firestoreClientRunCountQuery } from '../core/firestore_client'; | ||
import { AggregateField, AggregateQuerySnapshot } from '../lite-api/aggregate'; | ||
import { cast } from '../util/input_validation'; | ||
|
||
import { ensureFirestoreConfigured, Firestore } from './database'; | ||
|
||
/** | ||
* Executes the query and returns the results as a `AggregateQuerySnapshot` from the | ||
* server. Returns an error if the network is not available. | ||
* | ||
* @param query - The `Query` to execute. | ||
* | ||
* @returns A `Promise` that will be resolved with the results of the query. | ||
*/ | ||
export function getCountFromServer( | ||
query: Query<unknown> | ||
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> { | ||
const firestore = cast(query.firestore, Firestore); | ||
const client = ensureFirestoreConfigured(firestore); | ||
return firestoreClientRunCountQuery(client, query); | ||
} |
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
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,153 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 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. | ||
*/ | ||
|
||
import { deepEqual } from '@firebase/util'; | ||
|
||
import { Value } from '../protos/firestore_proto_api'; | ||
import { invokeRunAggregationQueryRpc } from '../remote/datastore'; | ||
import { hardAssert } from '../util/assert'; | ||
import { cast } from '../util/input_validation'; | ||
|
||
import { getDatastore } from './components'; | ||
import { Firestore } from './database'; | ||
import { Query, queryEqual } from './reference'; | ||
import { LiteUserDataWriter } from './reference_impl'; | ||
|
||
/** | ||
* An `AggregateField`that captures input type T. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export class AggregateField<T> { | ||
type = 'AggregateField'; | ||
} | ||
|
||
/** | ||
* Creates and returns an aggregation field that counts the documents in the result set. | ||
* @returns An `AggregateField` object with number input type. | ||
*/ | ||
export function count(): AggregateField<number> { | ||
return new AggregateField<number>(); | ||
} | ||
|
||
/** | ||
* The union of all `AggregateField` types that are returned from the factory | ||
* functions. | ||
*/ | ||
export type AggregateFieldType = ReturnType<typeof count>; | ||
|
||
/** | ||
* A type whose values are all `AggregateField` objects. | ||
* This is used as an argument to the "getter" functions, and the snapshot will | ||
* map the same names to the corresponding values. | ||
*/ | ||
export interface AggregateSpec { | ||
[field: string]: AggregateFieldType; | ||
} | ||
|
||
/** | ||
* A type whose keys are taken from an `AggregateSpec` type, and whose values | ||
* are the result of the aggregation performed by the corresponding | ||
* `AggregateField` from the input `AggregateSpec`. | ||
*/ | ||
export type AggregateSpecData<T extends AggregateSpec> = { | ||
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never; | ||
}; | ||
|
||
/** | ||
* An `AggregateQuerySnapshot` contains the results of running an aggregate query. | ||
*/ | ||
export class AggregateQuerySnapshot<T extends AggregateSpec> { | ||
readonly type = 'AggregateQuerySnapshot'; | ||
|
||
/** @hideconstructor */ | ||
constructor( | ||
readonly query: Query<unknown>, | ||
private readonly _data: AggregateSpecData<T> | ||
) {} | ||
|
||
/** | ||
* The results of the requested aggregations. The keys of the returned object | ||
* will be the same as those of the `AggregateSpec` object specified to the | ||
* aggregation method, and the values will be the corresponding aggregation | ||
* result. | ||
* | ||
* @returns The aggregation statistics result of running a query. | ||
*/ | ||
data(): AggregateSpecData<T> { | ||
return this._data; | ||
} | ||
} | ||
|
||
/** | ||
* Counts the number of documents in the result set of the given query, ignoring | ||
* any locally-cached data and any locally-pending writes and simply surfacing | ||
* whatever the server returns. If the server cannot be reached then the | ||
* returned promise will be rejected. | ||
* | ||
* @param query - The `Query` to execute. | ||
* | ||
* @returns An `AggregateQuerySnapshot` that contains the number of documents. | ||
*/ | ||
export function getCount( | ||
query: Query<unknown> | ||
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> { | ||
const firestore = cast(query.firestore, Firestore); | ||
const datastore = getDatastore(firestore); | ||
const userDataWriter = new LiteUserDataWriter(firestore); | ||
return invokeRunAggregationQueryRpc(datastore, query._query).then(result => { | ||
hardAssert( | ||
result[0] !== undefined, | ||
'Aggregation fields are missing from result.' | ||
); | ||
|
||
const counts = Object.entries(result[0]) | ||
.filter(([key, value]) => key === 'count_alias') | ||
.map(([key, value]) => userDataWriter.convertValue(value as Value)); | ||
|
||
const countValue = counts[0]; | ||
|
||
hardAssert( | ||
typeof countValue === 'number', | ||
'Count aggregate field value is not a number: ' + countValue | ||
); | ||
|
||
return Promise.resolve( | ||
new AggregateQuerySnapshot<{ count: AggregateField<number> }>(query, { | ||
count: countValue | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Compares two `AggregateQuerySnapshot` instances for equality. | ||
* Two `AggregateQuerySnapshot` instances are considered "equal" if they have | ||
* the same underlying query, and the same data. | ||
* | ||
* @param left - The `AggregateQuerySnapshot` to compare. | ||
* @param right - The `AggregateQuerySnapshot` to compare. | ||
* | ||
* @returns true if the AggregateQuerySnapshots are equal. | ||
*/ | ||
export function aggregateQuerySnapshotEqual<T extends AggregateSpec>( | ||
left: AggregateQuerySnapshot<T>, | ||
right: AggregateQuerySnapshot<T> | ||
): boolean { | ||
return ( | ||
queryEqual(left.query, right.query) && deepEqual(left.data(), right.data()) | ||
); | ||
} |
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
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
42 changes: 42 additions & 0 deletions
42
packages/firestore/src/protos/google/firestore/v1/aggregation_result.proto
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,42 @@ | ||
// Copyright 2022 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. | ||
|
||
syntax = "proto3"; | ||
|
||
package google.firestore.v1; | ||
|
||
import "google/firestore/v1/document.proto"; | ||
|
||
option csharp_namespace = "Google.Cloud.Firestore.V1"; | ||
option go_package = "google.golang.org/genproto/googleapis/firestore/v1;firestore"; | ||
option java_multiple_files = true; | ||
option java_outer_classname = "AggregationResultProto"; | ||
option java_package = "com.google.firestore.v1"; | ||
option objc_class_prefix = "GCFS"; | ||
option php_namespace = "Google\\Cloud\\Firestore\\V1"; | ||
option ruby_package = "Google::Cloud::Firestore::V1"; | ||
|
||
// The result of a single bucket from a Firestore aggregation query. | ||
// | ||
// The keys of `aggregate_fields` are the same for all results in an aggregation | ||
// query, unlike document queries which can have different fields present for | ||
// each result. | ||
message AggregationResult { | ||
// The result of the aggregation functions, ex: `COUNT(*) AS total_docs`. | ||
// | ||
// The key is the [alias][google.firestore.v1.StructuredAggregationQuery.Aggregation.alias] | ||
// assigned to the aggregation function on input and the size of this map | ||
// equals the number of aggregation functions in the query. | ||
map<string, Value> aggregate_fields = 2; | ||
} |
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.