Skip to content

Mila/count implement count query #6528

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 25 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function aggregateQueryEqual(left: AggregateQuery, right: AggregateQuery)

// @public
export class AggregateQuerySnapshot {
// (undocumented)
getCount(): number | null;
// (undocumented)
readonly query: AggregateQuery;
Expand Down Expand Up @@ -235,7 +236,7 @@ export class GeoPoint {
}

// @public (undocumented)
export function getAggregateFromServerDirect(aggregateQuery: AggregateQuery): Promise<AggregateQuerySnapshot>;
export function getAggregateFromServerDirect(query: AggregateQuery): Promise<AggregateQuerySnapshot>;

// @public
export function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
Expand Down
56 changes: 28 additions & 28 deletions packages/firestore/src/lite-api/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
* limitations under the License.
*/

import { CompositeFilterOpEnum } from '../protos/firestore_proto_api';
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';

/**
* A `AggregateQuery` computes some aggregation statistics from the result set of
* An `AggregateQuery` computes some aggregation statistics from the result set of
* a base `Query`.
*/
export class AggregateQuery {
Expand All @@ -44,7 +45,7 @@ export class AggregateQuery {
}

/**
* A `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
* An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
*/
export class AggregateQuerySnapshot {
readonly type = 'AggregateQuerySnapshot';
Expand All @@ -56,7 +57,7 @@ export class AggregateQuerySnapshot {
}

/**
* @return The result of a document count aggregation. Returns null if no count aggregation is
* @returns The result of a document count aggregation. Returns null if no count aggregation is
* available in the result.
*/
getCount(): number | null {
Expand All @@ -67,43 +68,42 @@ export class AggregateQuerySnapshot {
/**
* Creates an `AggregateQuery` counting the number of documents matching this query.
*
* @return An `AggregateQuery` object that can be used to count the number of documents in
* @returns An `AggregateQuery` object that can be used to count the number of documents in
* the result set of this query.
*/
export function countQuery(query: Query<unknown>): AggregateQuery {
return new AggregateQuery(query);
}

export function getAggregateFromServerDirect(
aggregateQuery: AggregateQuery
query: AggregateQuery
): Promise<AggregateQuerySnapshot> {
const firestore = cast(aggregateQuery.query.firestore, Firestore);
const firestore = cast(query.query.firestore, Firestore);
const datastore = getDatastore(firestore);
const userDataWriter = new LiteUserDataWriter(firestore);

return invokeRunAggregationQueryRpc(datastore, aggregateQuery).then(
result => {
hardAssert(
result[0] !== undefined,
'Aggregation fields are missing from result.'
);
return invokeRunAggregationQueryRpc(datastore, query).then(result => {
hardAssert(
result[0] !== undefined,
'Aggregation fields are missing from result.'
);

const countField = (result[0] as any).count_alias;
hardAssert(
countField !== undefined,
'Count field is missing from result.'
);
const counts = Object.entries(result[0])
.filter(([key, value]) => key === 'count_alias')
.map(([key, value]) => userDataWriter.convertValue(value as Value));

const countAggregateResult = userDataWriter.convertValue(countField);
hardAssert(
typeof countAggregateResult === 'number',
'Count aggeragte field is not a number: ' + countAggregateResult
);
return Promise.resolve(
new AggregateQuerySnapshot(aggregateQuery, countAggregateResult)
);
}
);
const count = counts[0];
hardAssert(
count !== undefined,
'count_alias property is missing from result.'
);
hardAssert(
typeof count === 'number',
'Count aggeragte field is not a number: ' + count
);

return Promise.resolve(new AggregateQuerySnapshot(query, count));
});
}

export function aggregateQueryEqual(
Expand Down
8 changes: 2 additions & 6 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import {
import { Timestamp } from '../../src/lite-api/timestamp';
import { runTransaction } from '../../src/lite-api/transaction';
import { writeBatch } from '../../src/lite-api/write_batch';

import {
DEFAULT_PROJECT_ID,
DEFAULT_SETTINGS
Expand Down Expand Up @@ -2058,26 +2057,23 @@ describe('countQuery()', () => {
it('AggregateQuery and AggregateQuerySnapshot inherits the original query', () => {
return withTestCollection(async coll => {
const query_ = query(coll);

const countQuery_ = countQuery(query_);
expect(countQuery_.type).to.equal('AggregateQuery');
expect(countQuery_.query).to.equal(query_);

const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.query).to.equal(countQuery_);
expect(snapshot.query.query).to.equal(query_);
});
});

it('empty test collection count', () => {
return withTestCollection(async coll => {
const countQuery_ = countQuery(query(coll));

const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(0);
});
});

it('test collection count with sample docs ', () => {
it('test collection count with 5 docs', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const countQuery_ = countQuery(query(collection));
const snapshot = await getAggregateFromServerDirect(countQuery_);
Expand Down