Skip to content

Fix the cursor expanding result set bug #6166

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 7 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
62 changes: 37 additions & 25 deletions packages/firestore/src/core/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ import {
isReferenceValue,
MAX_VALUE,
MIN_VALUE,
singleValueBoundCompare,
typeOrder,
valueCompare,
valueEquals,
valuesGetLowerBound,
valuesGetUpperBound,
valuesMax,
valuesMin
valuesGetUpperBound
} from '../model/values';
import { Value as ProtoValue } from '../protos/firestore_proto_api';
import { debugAssert, debugCast, fail } from '../util/assert';
Expand Down Expand Up @@ -293,13 +292,13 @@ export function targetGetNotInValues(

/**
* Returns a lower bound of field values that can be used as a starting point to
* scan the index defined by `fieldIndex`. Returns `null` if no lower bound
* scan the index defined by `fieldIndex`. Returns `MIN_VALUE` if no lower bound
* exists.
*/
export function targetGetLowerBound(
target: Target,
fieldIndex: FieldIndex
): Bound | null {
): Bound {
const values: ProtoValue[] = [];
let inclusive = true;

Expand All @@ -311,10 +310,6 @@ export function targetGetLowerBound(
? targetGetAscendingBound(target, segment.fieldPath, target.startAt)
: targetGetDescendingBound(target, segment.fieldPath, target.startAt);

if (!segmentBound.value) {
// No lower bound exists
return null;
}
values.push(segmentBound.value);
inclusive &&= segmentBound.inclusive;
}
Expand All @@ -323,13 +318,13 @@ export function targetGetLowerBound(

/**
* Returns an upper bound of field values that can be used as an ending point
* when scanning the index defined by `fieldIndex`. Returns `null` if no
* when scanning the index defined by `fieldIndex`. Returns `MAX_VALUE` if no
* upper bound exists.
*/
export function targetGetUpperBound(
target: Target,
fieldIndex: FieldIndex
): Bound | null {
): Bound {
const values: ProtoValue[] = [];
let inclusive = true;

Expand All @@ -341,10 +336,6 @@ export function targetGetUpperBound(
? targetGetDescendingBound(target, segment.fieldPath, target.endAt)
: targetGetAscendingBound(target, segment.fieldPath, target.endAt);

if (!segmentBound.value) {
// No upper bound exists
return null;
}
values.push(segmentBound.value);
inclusive &&= segmentBound.inclusive;
}
Expand All @@ -360,13 +351,14 @@ function targetGetAscendingBound(
target: Target,
fieldPath: FieldPath,
bound: Bound | null
): { value: ProtoValue | undefined; inclusive: boolean } {
let value: ProtoValue | undefined = undefined;
): { value: ProtoValue; inclusive: boolean } {
let value: ProtoValue = MIN_VALUE;

let inclusive = true;

// Process all filters to find a value for the current field segment
for (const fieldFilter of targetGetFieldFiltersForPath(target, fieldPath)) {
let filterValue: ProtoValue | undefined = undefined;
let filterValue: ProtoValue = MIN_VALUE;
let filterInclusive = true;

switch (fieldFilter.op) {
Expand All @@ -391,7 +383,12 @@ function targetGetAscendingBound(
// Remaining filters cannot be used as lower bounds.
}

if (valuesMax(value, filterValue) === filterValue) {
if (
singleValueBoundCompare(
{ value, inclusive },
{ value: filterValue, inclusive: filterInclusive }
) < 0
) {
value = filterValue;
inclusive = filterInclusive;
}
Expand All @@ -404,7 +401,12 @@ function targetGetAscendingBound(
const orderBy = target.orderBy[i];
if (orderBy.field.isEqual(fieldPath)) {
const cursorValue = bound.position[i];
if (valuesMax(value, cursorValue) === cursorValue) {
if (
singleValueBoundCompare(
{ value, inclusive },
{ value: cursorValue, inclusive: bound.inclusive }
) < 0
) {
value = cursorValue;
inclusive = bound.inclusive;
}
Expand All @@ -424,13 +426,13 @@ function targetGetDescendingBound(
target: Target,
fieldPath: FieldPath,
bound: Bound | null
): { value: ProtoValue | undefined; inclusive: boolean } {
let value: ProtoValue | undefined = undefined;
): { value: ProtoValue; inclusive: boolean } {
let value: ProtoValue = MAX_VALUE;
let inclusive = true;

// Process all filters to find a value for the current field segment
for (const fieldFilter of targetGetFieldFiltersForPath(target, fieldPath)) {
let filterValue: ProtoValue | undefined = undefined;
let filterValue: ProtoValue = MAX_VALUE;
let filterInclusive = true;

switch (fieldFilter.op) {
Expand All @@ -456,7 +458,12 @@ function targetGetDescendingBound(
// Remaining filters cannot be used as upper bounds.
}

if (valuesMin(value, filterValue) === filterValue) {
if (
singleValueBoundCompare(
{ value, inclusive },
{ value: filterValue, inclusive: filterInclusive }
) > 0
) {
value = filterValue;
inclusive = filterInclusive;
}
Expand All @@ -469,7 +476,12 @@ function targetGetDescendingBound(
const orderBy = target.orderBy[i];
if (orderBy.field.isEqual(fieldPath)) {
const cursorValue = bound.position[i];
if (valuesMin(value, cursorValue) === cursorValue) {
if (
singleValueBoundCompare(
{ value, inclusive },
{ value: cursorValue, inclusive: bound.inclusive }
) > 0
) {
value = cursorValue;
inclusive = bound.inclusive;
}
Expand Down
61 changes: 19 additions & 42 deletions packages/firestore/src/local/indexeddb_index_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ export class IndexedDbIndexManager implements IndexManager {
index!.indexId,
arrayValues,
lowerBoundEncoded,
!!lowerBound && lowerBound.inclusive,
lowerBound.inclusive,
upperBoundEncoded,
!!upperBound && upperBound.inclusive,
upperBound.inclusive,
notInEncoded
);
return PersistencePromise.forEach(
Expand Down Expand Up @@ -331,9 +331,9 @@ export class IndexedDbIndexManager implements IndexManager {
private generateIndexRanges(
indexId: number,
arrayValues: ProtoValue[] | null,
lowerBounds: Uint8Array[] | null,
lowerBounds: Uint8Array[],
lowerBoundInclusive: boolean,
upperBounds: Uint8Array[] | null,
upperBounds: Uint8Array[],
upperBoundInclusive: boolean,
notInValues: Uint8Array[]
): IDBKeyRange[] {
Expand All @@ -343,10 +343,7 @@ export class IndexedDbIndexManager implements IndexManager {
// combined with the values from the query bounds.
const totalScans =
(arrayValues != null ? arrayValues.length : 1) *
Math.max(
lowerBounds != null ? lowerBounds.length : 1,
upperBounds != null ? upperBounds.length : 1
);
Math.max(lowerBounds.length, upperBounds.length);
const scansPerArrayElement =
totalScans / (arrayValues != null ? arrayValues.length : 1);

Expand All @@ -356,22 +353,18 @@ export class IndexedDbIndexManager implements IndexManager {
? this.encodeSingleElement(arrayValues[i / scansPerArrayElement])
: EMPTY_VALUE;

const lowerBound = lowerBounds
? this.generateLowerBound(
indexId,
arrayValue,
lowerBounds[i % scansPerArrayElement],
lowerBoundInclusive
)
: this.generateEmptyBound(indexId);
const upperBound = upperBounds
? this.generateUpperBound(
indexId,
arrayValue,
upperBounds[i % scansPerArrayElement],
upperBoundInclusive
)
: this.generateEmptyBound(indexId + 1);
const lowerBound = this.generateLowerBound(
indexId,
arrayValue,
lowerBounds[i % scansPerArrayElement],
lowerBoundInclusive
);
const upperBound = this.generateUpperBound(
indexId,
arrayValue,
upperBounds[i % scansPerArrayElement],
upperBoundInclusive
);

const notInBound = notInValues.map(notIn =>
this.generateLowerBound(
Expand Down Expand Up @@ -420,19 +413,6 @@ export class IndexedDbIndexManager implements IndexManager {
return inclusive ? entry.successor() : entry;
}

/**
* Generates an empty bound that scopes the index scan to the current index
* and user.
*/
private generateEmptyBound(indexId: number): IndexEntry {
return new IndexEntry(
indexId,
DocumentKey.empty(),
EMPTY_VALUE,
EMPTY_VALUE
);
}

private getFieldIndex(
transaction: PersistenceTransaction,
target: Target
Expand Down Expand Up @@ -572,11 +552,8 @@ export class IndexedDbIndexManager implements IndexManager {
private encodeBound(
fieldIndex: FieldIndex,
target: Target,
bound: Bound | null
): Uint8Array[] | null {
if (bound == null) {
return null;
}
bound: Bound
): Uint8Array[] {
return this.encodeValues(fieldIndex, target, bound.position);
}

Expand Down
44 changes: 22 additions & 22 deletions packages/firestore/src/model/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function typeOrder(value: Value): TypeOrder {
if (isServerTimestamp(value)) {
return TypeOrder.ServerTimestampValue;
} else if (isMaxValue(value)) {
return TypeOrder.ArrayValue;
return TypeOrder.ObjectValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be TypeOrder.MaxValue? Android reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
return TypeOrder.ObjectValue;
} else {
Expand Down Expand Up @@ -350,6 +350,14 @@ function compareArrays(left: ArrayValue, right: ArrayValue): number {
}

function compareMaps(left: MapValue, right: MapValue): number {
if (left === MAX_VALUE.mapValue && right === MAX_VALUE.mapValue) {
return 0;
} else if (left === MAX_VALUE.mapValue) {
return 1;
} else if (right === MAX_VALUE.mapValue) {
return -1;
}

const leftMap = left.fields || {};
const leftKeys = Object.keys(leftMap);
const rightMap = right.fields || {};
Expand Down Expand Up @@ -670,28 +678,20 @@ export function valuesGetUpperBound(value: Value): Value {
}
}

export function valuesMax(
left: Value | undefined,
right: Value | undefined
): Value | undefined {
if (left === undefined) {
return right;
} else if (right === undefined) {
return left;
} else {
return valueCompare(left, right) > 0 ? left : right;
export function singleValueBoundCompare(
left: { value: Value; inclusive: boolean },
right: { value: Value; inclusive: boolean }
): number {
const cmp = valueCompare(left.value, right.value);
if (cmp !== 0) {
return cmp;
}
}

export function valuesMin(
left: Value | undefined,
right: Value | undefined
): Value | undefined {
if (left === undefined) {
return right;
} else if (right === undefined) {
return left;
} else {
return valueCompare(left, right) < 0 ? left : right;
if (left.inclusive && !right.inclusive) {
return -1;
} else if (!left.inclusive && right.inclusive) {
return 1;
}

return 0;
}
17 changes: 17 additions & 0 deletions packages/firestore/test/unit/local/index_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,23 @@ describe('IndexedDbIndexManager', async () => {
await verifyResults(queryWithRestrictedBound, 'coll/val4');
});

it('cannot expand result set from a cursor', async () => {
await indexManager.addFieldIndex(
fieldIndex('coll', { fields: [['c', IndexKind.ASCENDING]] })
);
await addDoc('coll/val1', { 'a': 1, 'b': 1, 'c': 3 });
await addDoc('coll/val2', { 'a': 2, 'b': 2, 'c': 2 });

const testingQuery = queryWithStartAt(
queryWithAddedOrderBy(
queryWithAddedFilter(query('coll'), filter('c', '>', 2)),
orderBy('c', 'asc')
),
bound([2], /* inclusive= */ true)
);
await verifyResults(testingQuery, 'coll/val1');
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add another test (or just add to the existing test) for the other direction? e.g. c < 2 order by c DESC with bound 2 inclusive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that uncovers one more bug.

it('support advances queries', async () => {
// This test compares local query results with those received from the Java
// Server SDK.
Expand Down
Loading