-
Notifications
You must be signed in to change notification settings - Fork 78
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
Firestore limit_to_last(n) returns the first n elements in a reverse order instead of returning the last n elements #536
Comments
I have the same issue |
The limit-to-last implementation in python-firestore is broken when startAt, endAt, startAfter, or endBefore is specified. The reason is that the implementation must flip these "cursors", in addition to flipping the ordering, and the implementation only flips the ordering: python-firestore/google/cloud/firestore_v1/query.py Lines 153 to 163 in 6480f6a
Here is the example of the correct implementation from the nodejs-firestore SDK: |
The good news is that there is a relatively-easy workaround. Limit-to-last queries are implemented entirely in the client, and do not need any special server support. This means that you can implement limit-to-last yourself to work around this bug. Here is an example. The code below is all pseudocode for simplicity, not real Python code. Suppose you have these documents in your database:
Suppose you wanted to do limitToLast(3).orderBy("age", "ASC").endBefore(doc5), which would produce the result [doc2, doc3, doc4]. You could instead run this query: limit(3).orderBy("age", "DESC").startAfter(doc5). This query would produce the result [doc4, doc3, doc2] so the final step would be to reverse the order of the result set, to [doc2, doc3, doc4]. |
When limit_to_last was set, we need to reverse the order. However due to error in comparing the order direction, it was not properly set. comparing `order.direction == self.ASCENDING` is always `False` because there are two different types. The correct way is by comparing `order.direction.name == self.ASCENDING` Fixes #536
The bug is due to this line
The test I've just opened a PR to fix it #692 |
When limit_to_last was set, we need to reverse the order. However due to error in comparing the order direction, it was not properly set. comparing `order.direction == self.ASCENDING` is always `False` because there are two different types. The correct way is by comparing `order.direction.name == self.ASCENDING` Fixes #536
Describe your environment
Describe the problem
As per the documentation for limit_to_last clause, it should return the last n elements that match the query. However, when executing a simple query (i.e.
db.collection("numbers").order_by("number").limit_to_last(5)
) it returns the first 5 elements but in a reverse order.Steps to reproduce
Create a collection and add data (in my case, I created a "numbers" collection, and inserted a total of 50 documents (from number 0 to 49) as follows:
And then, execute the following query and print the results:
Expected output:
Real output:
In case you find it useful, I also tried to reproduce this behavior in the Node.js Admin SDK, and I obtained the desired output (I mean, limitToLast() works perfectly fine):
Node.js code:
Node.js output:

Thanks.
The text was updated successfully, but these errors were encountered: