Skip to content

Firestore: Using offset greater than 1000 on a query doesn't work #6924

Closed

Description

@KiwiPolarBear

Environment details

macOS Mojave 10.14.1
Python 3.6
google-cloud-firestore==0.30.0

Steps to reproduce

  1. Create a query to get a large collection (over 1000 documents)
  2. Add an offset greater than 1000 to the query
  3. Get the query and iterate over the documents
  4. You should get an exception about having no documents

Code example

from google.cloud import firestore

client = firestore.Client()

documents = client.collection('Collection').order_by('field').offset(1001).get()

for document in documents:
    pass

Stack trace

ValueError: Unexpected server response. All responses other than the first must contain a document. The response at index 1 was
read_time {
  seconds: 1544736718
  nanos: 109409000
}
skipped_results: 1
.

Activity

  1. added
    type: question
    api: firestore
    on Dec 13, 2018
  2. tseaver commented on Dec 13, 2018

    @tseaver
    Contributor

    Can you please show the whole traceback? Also, are you sure there are at least 1000 documents in the collection?

  3. KiwiPolarBear commented on Dec 13, 2018

    @KiwiPolarBear
    Author

    Sure.

    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "/Source/....../scripts/.......py", line 21, in <module>
        for document in documents:
      File "/Source/......../venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/query.py", line 599, in get
        raise ValueError(msg)
    ValueError: Unexpected server response. All responses other than the first must contain a document. The response at index 1 was
    read_time {
      seconds: 1544741917
      nanos: 761441000
    }
    skipped_results: 1
    .
    

    I am certain there are more than 1000 documents as making the same query with an offset of 1000, returns more documents.

  4. tseaver commented on Dec 13, 2018

    @tseaver
    Contributor

    There is no indication in the StructuredQuery API docs of any upper bound on limit, and the google-cloud-firestore code doesn't do anything particular with the value beyond copying it into that message.

    @shollyman Can you shed light on why the server would be returning a RunQueryResponse here with no document set? If it is expected, what is the expected behavior within the client library?

  5. hishnash commented on Jan 14, 2019

    @hishnash

    on: 0.31.0

    my stack trace is when I do .offset(1001)

      File "/usr/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/query.py", line 758, in get
        raise ValueError(msg)
    ValueError: Unexpected server response. All responses other than the first must contain a document. The response at index 1 was
    read_time {
      seconds: 1547577043
      nanos: 738488000
    }
    skipped_results: 200
    
  6. timkock commented on Jan 23, 2019

    @timkock

    same issue, abandoning firestore for the moment as we can't get data out

    page_size = 1000
    cursor = 0
    limit = 100000
    data = []
    
    t_collection = client.collection(collection)
    while True:
        t_query = t_collection.offset(cursor).limit(page_size)
        docs = [x.to_dict() for x in list(t_query.get())]
        if(len(docs)) == 0 or cursor >= limit:
            break
        else:
            data = data + docs
            print(cursor, len(docs), len(data))
            cursor += len(docs)
        
    print('were done', len(data))

    has following response after getting ~2k documents out

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-9-e21161698f76> in <module>
          7 while True:
          8     t_query = t_collection.offset(cursor).limit(page_size)
    ----> 9     docs = [x.to_dict() for x in list(t_query.get())]
         10     if(len(docs)) == 0 or cursor >= limit:
         11         break
    
    ~/.virtualenvs/broadleaf/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/query.py in get(self, transaction)
        756                 if index != 0:
        757                     msg = _EMPTY_DOC_TEMPLATE.format(index, response_pb)
    --> 758                     raise ValueError(msg)
        759                 empty_stream = skipped_results == 0
        760             else:
    
    ValueError: Unexpected server response. All responses other than the first must contain a document. The response at index 1 was
    read_time {
      seconds: 1548254968
      nanos: 205102000
    }
    skipped_results: 1000
    .
    
  7. tseaver commented on Jan 23, 2019

    @tseaver
    Contributor

    Hmm, looking at the API docs for RunQueryResponse, I note that the document field description says:

    A query result. Not set when reporting partial progress.

    Response fields:

    • The transaction field can only be set on the first response in the stream.
    • read_time doesn't seem sensible if there is no document.
    • skipped_results: I guess the back end could send a document-less response with this field set as a "still skipping" notification?
  8. tseaver commented on Jan 23, 2019

    @tseaver
    Contributor

    Looking at Query.get, which loops over the stream of RunQueryResponse messages, ISTM that we could just not raise the error for the empty document. In fact, the whole thing could be simplified to the following, given that we don't return the skipped_results anywhere:

            for response_pb in response_iterator:
                snapshot, _ = _query_response_to_snapshot(
                    response_pb, self._parent, expected_prefix
                )
                if snapshot is not None:
                    yield snapshot

    I don't think raising exceptions about potential error cases from the back-end (non-empty response after empty first response, empty response after non-empty response) is useful here, given that we know the back end can return the empty response beyond the first one.

  9. assigned and unassigned on Jan 23, 2019
  10. tseaver commented on Jan 23, 2019

    @tseaver
    Contributor

    @KiwiPolarBear I'm sorry this issue has been frustrating. Could you try the patch in PR #7206 and see if it resolves your issue?

  11. hishnash commented on Jan 23, 2019

    @hishnash

    @tseaver I have been able to test that branch I can confirm the bug seems fixed

    for at least offset sizes of upto 8500 (with limit = 100) (that is the largest single collection I have in my current data set)

    it also appears to return the same set of items as using the start_after with a DocumentSnapshot

        try:
            first = next(collection.order_by(field).limit(1).get())
        except StopIteration:
            return
        yield first
    
        document = first
    
        while document is not None:
            documents = collection.order_by(field)
    
            chunk = documents.start_after(
                document
            ).limit(limit).get()
    
            document = None
    
            for document in chunk:
                yield document
  12. added a commit that references this issue on Jan 28, 2019
    e79dfde
  13. added a commit that references this issue on Nov 24, 2025
    9d59c0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: firestorebackendtype: question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions