Skip to content

Commit 66b16cb

Browse files
committed
Addressing code comments.
1 parent a962368 commit 66b16cb

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

packages/firestore/src/core/query.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ function queryMatchesPathAndCollectionGroup(
468468
* in the results.
469469
*/
470470
function queryMatchesOrderBy(query: Query, doc: Document): boolean {
471+
// We must use `queryOrderBy()` to get the list of all orderBys (both implicit and explicit).
472+
// Note that for OR queries, orderBy applies to all disjunction terms and implicit orderBys must
473+
// be taken into account. For example, the query "a > 1 || b==1" has an implicit "orderBy a" due
474+
// to the inequality, and is evaluated as "a > 1 orderBy a || b==1 orderBy a".
475+
// A document with content of {b:1} matches the filters, but does not match the orderBy because
476+
// it's missing the field 'a'.
471477
for (const orderBy of queryOrderBy(query)) {
472478
// order by key always matches
473479
if (!orderBy.field.isKeyField() && doc.data.field(orderBy.field) === null) {

packages/firestore/src/core/target.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,15 @@ export class CompositeFilter extends Filter {
786786
}
787787
}
788788

789+
// TODO(orquery) move compositeFilterWithAddedFilters to filter.ts in future refactor
790+
export function compositeFilterWithAddedFilters(
791+
compositeFilter: CompositeFilter,
792+
otherFilters: Filter[]
793+
): CompositeFilter {
794+
const mergedFilters = compositeFilter.filters.concat(otherFilters);
795+
return CompositeFilter.create(mergedFilters, CompositeOperator.AND);
796+
}
797+
789798
export function compositeFilterIsConjunction(
790799
compositeFilter: CompositeFilter
791800
): boolean {

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,9 @@ export class IndexedDbIndexManager implements IndexManager {
579579
let encoders: IndexByteEncoder[] = [];
580580
encoders.push(new IndexByteEncoder());
581581

582-
let boundIdx = 0;
582+
let valueIdx = 0;
583583
for (const segment of fieldIndexGetDirectionalSegments(fieldIndex)) {
584-
const value = values[boundIdx++];
584+
const value = values[valueIdx++];
585585
for (const encoder of encoders) {
586586
if (this.isInFilter(target, segment.fieldPath) && isArray(value)) {
587587
encoders = this.expandIndexValues(encoders, segment, value);

packages/firestore/src/util/logic_utils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
compositeFilterIsDisjunction,
2222
compositeFilterIsFlat,
2323
compositeFilterIsFlatConjunction,
24+
compositeFilterWithAddedFilters,
2425
CompositeOperator,
2526
FieldFilter,
2627
Filter
@@ -219,10 +220,7 @@ function applyDistributionCompositeFilters(
219220

220221
// Case 1 is a merge.
221222
if (compositeFilterIsConjunction(lhs) && compositeFilterIsConjunction(rhs)) {
222-
return CompositeFilter.create(
223-
lhs.filters.concat(rhs.filters),
224-
CompositeOperator.AND
225-
);
223+
return compositeFilterWithAddedFilters(lhs, rhs.getFilters());
226224
}
227225

228226
// Case 2,3,4 all have at least one side (lhs or rhs) that is a disjunction. In all three cases
@@ -245,9 +243,9 @@ function applyDistributionFieldAndCompositeFilters(
245243
// A & (B | C) --> (A & B) | (A & C)
246244
if (compositeFilterIsConjunction(compositeFilter)) {
247245
// Case 1
248-
return CompositeFilter.create(
249-
compositeFilter.filters.concat(fieldFilter.getFilters()),
250-
CompositeOperator.AND
246+
return compositeFilterWithAddedFilters(
247+
compositeFilter,
248+
fieldFilter.getFilters()
251249
);
252250
} else {
253251
// Case 2

packages/firestore/test/unit/core/query.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,10 @@ describe('Query', () => {
861861
nonMatching: MutableDocument[]
862862
): void {
863863
for (const doc of matching) {
864-
expect(queryMatches(query, doc)).true;
864+
expect(queryMatches(query, doc)).to.be.true;
865865
}
866866
for (const doc of nonMatching) {
867-
expect(queryMatches(query, doc)).false;
867+
expect(queryMatches(query, doc)).to.be.false;
868868
}
869869
}
870870

packages/firestore/test/unit/util/logic_utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('LogicUtils', () => {
178178
applyDistribution(orFilter(A, B), orFilter(C, D)),
179179
expectedResult
180180
)
181-
);
181+
).to.be.true;
182182
});
183183

184184
it('implements field filter compute DNF', () => {

0 commit comments

Comments
 (0)