fix(firestore): Correct the cursors when LimitToLast is used#9413
Merged
bhshkh merged 8 commits intogoogleapis:mainfrom Feb 15, 2024
Merged
fix(firestore): Correct the cursors when LimitToLast is used#9413bhshkh merged 8 commits intogoogleapis:mainfrom
bhshkh merged 8 commits intogoogleapis:mainfrom
Conversation
gkevinzheng
approved these changes
Feb 12, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context:
Consider the Firestore collection has documents with following ids {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
While sending a Firestore run query request 2 cursors can be specified(StartAt and EndAt). Cursors have a field called Before which specifies whether the result should include the field at the cursor.
When the user runs below query:
The query sent to Firestore service should have:
Firestore service behavior:
When
Beforefield of StartAt is true, the response includes the key in StartAt cursorWhen
Beforefield of EndAt is true, the response does not include the key in EndAt cursorSo, the above query would return: {1, 2, 3, 4, 5}
While sending a Firestore run query request, a limit can be specified which limits the number of results retuned. Taking the previous example:
The request sent will; be
would return {1, 2, 3}
If user wants only the last 3 results i.e. {3, 4, 5}, the query they would have to use would be:
Since the Firestore service does not provide any LimitToLast field out of the box, Go library queries the documents in reverse order and then reverses the result. So, in the above example, the request sent should be:
This query would return results: {5, 4, 3} which then the library will reverse and return to the user.
Issue:
Below queries return incorrect results
StartAfter(5).EndBefore(1).LimitToLast(5).OrderBy("id", Desc)
Expected behavior {4, 3, 2}
Actual behavior {5, 4, 3, 2}
StartAt(5).EndAt(1).LimitToLast(5).OrderBy("id", Desc)
Expected behavior {5, 4, 3, 2, 1}
Actual behavior {4, 3, 2}
Cause:
But the current request sent has
Before: falseinEndAtcursor. So, the result contains the value in the EndAt cursor i.e. 5 is available in the results.But the current request sent has
Before: falseinStartAtcursor andBefore: trueinEndAtcursor. So, the result does not contain the values in the StartAt and EndAt cursors i.e. 1 and 5 are not available in the results.Fix:
Set the
startBeforeandendBeforefields correctly