@@ -215,11 +215,29 @@ export function isCollectionGroupQuery(query: Query): boolean {
215
215
* which can be different from the order by constraints the user provided (e.g.
216
216
* the SDK and backend always orders by `__name__`).
217
217
*/
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
+
219
228
const queryImpl = debugCast ( query , QueryImpl ) ;
220
229
if ( queryImpl . memoizedOrderBy === null ) {
221
230
queryImpl . memoizedOrderBy = [ ] ;
222
231
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
+
223
241
const inequalityField = getInequalityFilterField ( queryImpl ) ;
224
242
const firstOrderByField = getFirstOrderByField ( queryImpl ) ;
225
243
if ( inequalityField !== null && firstOrderByField === null ) {
@@ -267,13 +285,24 @@ export function queryOrderBy(query: Query): OrderBy[] {
267
285
* Converts this `Query` instance to it's corresponding `Target` representation.
268
286
*/
269
287
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 {
270
299
const queryImpl = debugCast ( query , QueryImpl ) ;
271
300
if ( ! queryImpl . memoizedTarget ) {
272
301
if ( queryImpl . limitType === LimitType . First ) {
273
302
queryImpl . memoizedTarget = newTarget (
274
303
queryImpl . path ,
275
304
queryImpl . collectionGroup ,
276
- queryOrderBy ( queryImpl ) ,
305
+ queryOrderBy ( queryImpl , settings ) ,
277
306
queryImpl . filters ,
278
307
queryImpl . limit ,
279
308
queryImpl . startAt ,
0 commit comments