Skip to content

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 29 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
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 Jul 28, 2022
598e948
compile protos and upload the generated json file (#6491)
milaGGL Jul 29, 2022
7f42490
Mila/count add api interface (#6499)
milaGGL Aug 4, 2022
981e6f1
create sample count test cases (#6506)
milaGGL Aug 4, 2022
bc61df7
Mila/count implement count query (#6528)
milaGGL Aug 24, 2022
c21304b
add test case for terminated firestore
milaGGL Aug 26, 2022
a3242ce
Mila/count add tests (#6566)
milaGGL Aug 30, 2022
6da4f4f
Mila/count export aggregate to api (#6575)
milaGGL Sep 1, 2022
fe871ce
change int32 to int64 (#6577)
milaGGL Sep 2, 2022
507e984
check client offline state and test (#6579)
milaGGL Sep 7, 2022
98189d5
Mila/count non lite api tests (#6581)
milaGGL Sep 8, 2022
0ebab69
Mila/count update api surface (#6589)
milaGGL Sep 13, 2022
4b273fe
remove DocumentFieldValue
milaGGL Sep 13, 2022
a9de825
Merge branch 'master' into mila/count
milaGGL Sep 13, 2022
576d202
fix lint error: Symbol not found for identifier
milaGGL Sep 13, 2022
dd693c6
format to pass lint check
milaGGL Sep 13, 2022
83fbc8d
remove the exports of aggregate query functions/types
milaGGL Sep 14, 2022
26a0bd2
Update aggregation.test.ts
milaGGL Sep 14, 2022
9cced28
skip count query test cases if not using emulator
milaGGL Sep 14, 2022
f490bfe
update the type of skipTestUnlessUsingEmulator
milaGGL Sep 14, 2022
078d581
roll back to any type for skipTestUnlessUsingEmulator
milaGGL Sep 15, 2022
a9ee57a
move aggregation test file into api_internal folder
milaGGL Sep 15, 2022
dbbc2f8
resolve comments
milaGGL Sep 15, 2022
c12d5a0
reformat to pass lint check
milaGGL Sep 15, 2022
74116a4
add changeset as requested by github
milaGGL Sep 15, 2022
de9dd9a
remove the changeset
milaGGL Sep 15, 2022
9438594
Merge branch 'master' into mila/count
milaGGL Sep 15, 2022
3a6cc27
add the changeset back
milaGGL Sep 15, 2022
0267c69
resolve comments
milaGGL Sep 16, 2022
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
5 changes: 5 additions & 0 deletions .changeset/hot-insects-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': minor
---

Implement count query for internal use.
39 changes: 39 additions & 0 deletions packages/firestore/src/api/aggregate.ts
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);
}
35 changes: 35 additions & 0 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import {
CredentialsProvider
} from '../api/credentials';
import { User } from '../auth/user';
import {
AggregateField,
AggregateQuerySnapshot,
getCount
} from '../lite-api/aggregate';
import { Query as LiteQuery } from '../lite-api/reference';
import { LocalStore } from '../local/local_store';
import {
localStoreExecuteQuery,
Expand All @@ -38,6 +44,7 @@ import { toByteStreamReader } from '../platform/byte_stream_reader';
import { newSerializer, newTextEncoder } from '../platform/serializer';
import { Datastore } from '../remote/datastore';
import {
canUseNetwork,
RemoteStore,
remoteStoreDisableNetwork,
remoteStoreEnableNetwork,
Expand Down Expand Up @@ -501,6 +508,34 @@ export function firestoreClientTransaction<T>(
return deferred.promise;
}

export function firestoreClientRunCountQuery(
client: FirestoreClient,
query: LiteQuery<unknown>
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> {
const deferred = new Deferred<
AggregateQuerySnapshot<{ count: AggregateField<number> }>
>();
client.asyncQueue.enqueueAndForget(async () => {
try {
const remoteStore = await getRemoteStore(client);
if (!canUseNetwork(remoteStore)) {
deferred.reject(
new FirestoreError(
Code.UNAVAILABLE,
'Failed to get count result because the client is offline.'
)
);
} else {
const result = await getCount(query);
deferred.resolve(result);
}
} catch (e) {
deferred.reject(e as Error);
}
});
return deferred.promise;
}

async function readDocumentFromCache(
localStore: LocalStore,
docKey: DocumentKey,
Expand Down
153 changes: 153 additions & 0 deletions packages/firestore/src/lite-api/aggregate.ts
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())
);
}
6 changes: 6 additions & 0 deletions packages/firestore/src/platform/node/grpc_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export class GrpcConnection implements Connection {
// We cache stubs for the most-recently-used token.
private cachedStub: GeneratedGrpcStub | null = null;

get shouldResourcePathBeIncludedInRequest(): boolean {
// Both `invokeRPC()` and `invokeStreamingRPC()` ignore their `path` arguments, and expect
// the "path" to be part of the given `request`.
return true;
}

constructor(protos: grpc.GrpcObject, private databaseInfo: DatabaseInfo) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.firestore = (protos as any)['google']['firestore']['v1'];
Expand Down
30 changes: 30 additions & 0 deletions packages/firestore/src/protos/firestore_proto_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,32 @@ export declare namespace firestoreV1ApiClientInterfaces {
readTime?: string;
skippedResults?: number;
}
interface RunAggregationQueryRequest {
parent?: string;
structuredAggregationQuery?: StructuredAggregationQuery;
transaction?: string;
newTransaction?: TransactionOptions;
readTime?: string;
}
interface RunAggregationQueryResponse {
result?: AggregationResult;
transaction?: string;
readTime?: string;
}
interface AggregationResult {
aggregateFields?: ApiClientObjectMap<Value>;
}
interface StructuredAggregationQuery {
structuredQuery?: StructuredQuery;
aggregations?: Aggregation[];
}
interface Aggregation {
count?: Count;
alias?: string;
}
interface Count {
upTo?: number;
}
interface Status {
code?: number;
message?: string;
Expand Down Expand Up @@ -479,6 +505,10 @@ export declare type RunQueryRequest =
firestoreV1ApiClientInterfaces.RunQueryRequest;
export declare type RunQueryResponse =
firestoreV1ApiClientInterfaces.RunQueryResponse;
export declare type RunAggregationQueryRequest =
firestoreV1ApiClientInterfaces.RunAggregationQueryRequest;
export declare type RunAggregationQueryResponse =
firestoreV1ApiClientInterfaces.RunAggregationQueryResponse;
export declare type Status = firestoreV1ApiClientInterfaces.Status;
export declare type StructuredQuery =
firestoreV1ApiClientInterfaces.StructuredQuery;
Expand Down
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;
}
Loading