Skip to content

Commit 4d7e2c5

Browse files
committed
Do not send implicit order-by for aggregation queries.
1 parent 80a7f7c commit 4d7e2c5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/firestore/src/core/query.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,29 @@ export function isCollectionGroupQuery(query: Query): boolean {
215215
* which can be different from the order by constraints the user provided (e.g.
216216
* the SDK and backend always orders by `__name__`).
217217
*/
218-
export function queryOrderBy(query: Query): OrderBy[] {
218+
export function queryOrderBy(
219+
query: Query,
220+
settings?: { includeImplicitOrderBy: boolean }
221+
): OrderBy[] {
222+
if (!settings) {
223+
settings = {
224+
includeImplicitOrderBy: true
225+
};
226+
}
227+
219228
const queryImpl = debugCast(query, QueryImpl);
220229
if (queryImpl.memoizedOrderBy === null) {
221230
queryImpl.memoizedOrderBy = [];
222231

232+
// If there are no explicit order-by operators, and we are not including
233+
// implicit order-bys, then return the empty memoizedOrderBy result
234+
if (
235+
queryImpl.explicitOrderBy.length === 0 &&
236+
!settings.includeImplicitOrderBy
237+
) {
238+
return queryImpl.memoizedOrderBy;
239+
}
240+
223241
const inequalityField = getInequalityFilterField(queryImpl);
224242
const firstOrderByField = getFirstOrderByField(queryImpl);
225243
if (inequalityField !== null && firstOrderByField === null) {
@@ -267,13 +285,24 @@ export function queryOrderBy(query: Query): OrderBy[] {
267285
* Converts this `Query` instance to it's corresponding `Target` representation.
268286
*/
269287
export function queryToTarget(query: Query): Target {
288+
return _queryToTarget(query);
289+
}
290+
291+
export function aggregateQueryToTarget(query: Query): Target {
292+
return _queryToTarget(query, { includeImplicitOrderBy: false });
293+
}
294+
295+
export function _queryToTarget(
296+
query: Query,
297+
settings?: { includeImplicitOrderBy: boolean }
298+
): Target {
270299
const queryImpl = debugCast(query, QueryImpl);
271300
if (!queryImpl.memoizedTarget) {
272301
if (queryImpl.limitType === LimitType.First) {
273302
queryImpl.memoizedTarget = newTarget(
274303
queryImpl.path,
275304
queryImpl.collectionGroup,
276-
queryOrderBy(queryImpl),
305+
queryOrderBy(queryImpl, settings),
277306
queryImpl.filters,
278307
queryImpl.limit,
279308
queryImpl.startAt,

packages/firestore/src/remote/datastore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { CredentialsProvider } from '../api/credentials';
1919
import { User } from '../auth/user';
2020
import { Aggregate } from '../core/aggregate';
21-
import { Query, queryToTarget } from '../core/query';
21+
import { aggregateQueryToTarget, Query, queryToTarget } from '../core/query';
2222
import { Document } from '../model/document';
2323
import { DocumentKey } from '../model/document_key';
2424
import { Mutation } from '../model/mutation';
@@ -248,7 +248,7 @@ export async function invokeRunAggregationQueryRpc(
248248
const datastoreImpl = debugCast(datastore, DatastoreImpl);
249249
const { request, aliasMap } = toRunAggregationQueryRequest(
250250
datastoreImpl.serializer,
251-
queryToTarget(query),
251+
aggregateQueryToTarget(query),
252252
aggregates
253253
);
254254

0 commit comments

Comments
 (0)