Skip to content
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: unexpected results when combining start_at or end_at with limit_to_last in queries #27527

Closed
sarub0b0 opened this issue Oct 29, 2024 · 0 comments · Fixed by #28137
Closed
Assignees
Labels
api: firestore Issues related to the Firestore API.

Comments

@sarub0b0
Copy link
Contributor

When combining start_{at,after} or end_{at,before} with last_to_limit in a Firestore query, start_at and end_at are not applied properly, returning unexpected results.

Environment details

  • OS: macOS 14.6.1
  • Ruby version: 3.3.4
  • Gem name and version: google-cloud-firestore 2.16.0

Steps to reproduce

  1. Create documents in the data collection with an id field
     require "google/cloud/firestore"
    
     firestore = Google::Cloud::Firestore.new
    
     10.times do |i|
       doc = firestore.collection("data").doc
       doc.set({ id: i })
     end
  2. Run the sample code using end_before and limit_to_last
  3. An empty array is returned

Code example

# example
require 'google/cloud/firestore'

firestore = Google::Cloud::Firestore.new

col = firestore.col 'data'

query = col.order(:id, :asc).end_before(10).limit_to_last(5)

pp query

docs = query.get

pp docs.map(&:data)

Full backtrace

❯ GOOGLE_CLOUD_PROJECT=PROJECT_ID ruby sample.rb
#<Google::Cloud::Firestore::Query:0x0000000121db92b0
 @client=#<Google::Cloud::Firestore::Client:0x00000001220530e8 @service=Google::Cloud::Firestore::Service(PROJECT_ID)((default))>,
 @limit_type=:last,
 @parent_path="projects/PROJECT_ID/databases/(default)/documents",
 @query=
  <Google::Cloud::Firestore::V1::StructuredQuery: from: [<Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector: collection_id: "data", all_descendants: false>], order_by: [<Google::Cloud::Firestore::V1::StructuredQuery::Order: field: <Google::Cloud::Firestore::V1::StructuredQuery::FieldReference: field_path: "id">, direction: :DESCENDING>], start_at: <Google::Cloud::Firestore::V1::Cursor: values: [<Google::Cloud::Firestore::V1::Value: integer_value: 10>], before: false>, end_at: <Google::Cloud::Firestore::V1::Cursor: values: [<Google::Cloud::Firestore::V1::Value: integer_value: 10>], before: true>, offset: 0, limit: <Google::Protobuf::Int32Value: value: 5>>>
[]

Expected result

[{:id=>5}, {:id=>6}, {:id=>7}, {:id=>8}, {:id=>9}]

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants