GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [BigQuery] Feature Request - An easy way to get a QueryResults from a QueryJob · Issue #2083 · googleapis/google-cloud-python

[BigQuery] Feature Request - An easy way to get a QueryResults from a QueryJob #2083

New issue
Copy link
New issue
Copy link
Closed
Closed
[BigQuery] Feature Request - An easy way to get a QueryResults from a QueryJob#2083
Copy link
Assignees
tseaver
Labels
api: bigqueryIssues related to the BigQuery API.

Description

@tswast
tswast
opened on Aug 10, 2016
Issue body actions

Problem:

There isn't an easy way to fetch the row results of a client.run_async_query(job_id, query) call.

I have to do some yucky stuff to get it to work in a sample I'm working on.

client = bigquery.Client()
query_job = client.run_async_query(str(uuid.uuid4()), query)
query_job.use_legacy_sql = False
query_job.begin()

wait_for_job(query_job)

query_results = bigquery.query.QueryResults('', client)
query_results._properties['jobReference'] = {
    'jobId': query_job.name,
    'projectId': query_job.project
}

Possible Solutions:

  • Modify constructor for QueryResults or add a class function to take an optional job object.
  • Add a method to QueryJob get_query_results() that does this same yuckiness (modifying the internal _properties)

I'm sure there are other solutions, too, which is why I'm asking for input rather than going off and trying to fix right away.

Reference:

  • QueryJob: https://github.com/GoogleCloudPlatform/gcloud-python/blob/master/gcloud/bigquery/job.py#L933
  • QueryResults: https://github.com/GoogleCloudPlatform/gcloud-python/blob/master/gcloud/bigquery/query.py#L42
  • Docs: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-python/master/docs/bigquery-usage.rst Notice the async query does not show fetching row results.

Activity

  1. tswast
    added
    api: bigqueryIssues related to the BigQuery API.
    on Aug 10, 2016
  2. tswast commented on Aug 10, 2016

    @tswast
    tswast
    on Aug 10, 2016
    ContributorAuthor
    More actions

    Note: my current "solution" of modifying the internal _properties works, but it's not something I'd want to include in our docs.

  3. dhermes
    assigned
    tseaver
    on Aug 10, 2016
  4. dhermes commented on Aug 10, 2016

    @dhermes
    dhermes
    on Aug 10, 2016
    Contributor
    More actions

    Also seems a wait_for_job might be something for us to provide?

  5. tswast commented on Aug 10, 2016

    @tswast
    tswast
    on Aug 10, 2016
    ContributorAuthor
    More actions

    Nah, not necessarily. wait_for_job is more for the sample. It makes an asynchronous call into a synchronous one. ;-)

    def wait_for_job(job):
        while True:
            job.reload()  # Refreshes the state via a GET request.
            if job.state == 'DONE':
                if job.error_result:
                    raise RuntimeError(job.error_result)
                return
            time.sleep(1)
    

    This probably isn't the best way to do it, and if someone was using a real asynchronous framework like gevent, twisted, or tornado, they'd probably want to do something slightly different.

    Reacted by Danny Hermes
  6. tseaver commented on Aug 10, 2016

    @tseaver
    tseaver
    on Aug 10, 2016
    Contributor
    More actions

    @tswast Thanks for the report! As it sits today, QueryResults is intended to model a synchronous query: it surfaces the job property only once that has been populated from the server during its run() or fetch_data() methods.

    It looks as thought we need to split out the part which initiates the synchronous query and the bits which allow fetching the results, to make it clearer how the two are supposed to interact, and how to reuse the second bit with an async query job.

    The synchronous case would look something like:

    query = client.sync_query(QUERY_STRING)
    query.timeout_ms = 30000
    query.run()
    
    if not query.complete:
        wait_for_job(query.job)
    
    # if we know the result set is small
    for row in query.rows:
        do_something_with(row)
    
    #otherwise, page through them
    results = query.results()
    
    rows, total_count, token = results.fetch_data()
    
    while True:
        for row in rows:
            do_something_with(row)
        if token is None:
            break
        rows, total_count, token = results.fetch_data(
            page_token=token)

    The asyncronous case would look like:

    job = client.async_query(QUERY_STRING)
    job.begin()
    
    wait_for_job(job)
    
    results = job.results()
    
    rows, total_count, token = results.fetch_data()
    
    while True:
        for row in rows:
            do_something_with(row)
        if token is None:
            break
        rows, total_count, token = results.fetch_data(
            page_token=token)
  7. tswast commented on Aug 29, 2016

    @tswast
    tswast
    on Aug 29, 2016
    ContributorAuthor
    More actions

    job.results() for asynchronous queries sounds good to me.

    I don't think we'd need to change how synchronous queries work.

  8. tseaver
    added
    fix asap
    blocks-docs-samples
    on Sep 6, 2016
  9. theacodes commented on Sep 13, 2016

    @theacodes
    theacodes
    on Sep 13, 2016
    Contributor
    More actions

    Bump, any update on this?

  10. tseaver
    closed this as completedin #2321on Sep 15, 2016
  11. tswast
    added a commit that references this issue on Oct 5, 2016

    BigQuery - use new QueryJob.results() method.

    32d233b
  12. theacodes
    added a commit that references this issue on Oct 5, 2016

    BigQuery - use new QueryJob.results() method. (#555)

    07e92d7
  13. plamut
    added a commit that references this issue on Jun 25, 2020

    BigQuery - use new QueryJob.results() method. [(googleapis#555)](Goog…

    b3b063a
  14. plamut
    added a commit that references this issue on Jul 22, 2020

    BigQuery - use new QueryJob.results() method. [(googleapis#555)](Goog…

    6ce03fc
  15. shollyman
    added a commit that references this issue on Jul 22, 2020

    BigQuery - use new QueryJob.results() method. [(#555)](GoogleCloudPla…

    f43905b
  16. parthea
    added a commit that references this issue on Nov 24, 2025

    fix: require google.shopping.type >= 0.1.6 (#2083)

    69ef46b
  17. parthea
    added a commit that references this issue on Apr 1, 2026

    chore: fix e2e tests after move of bigquery-storage package (#2083)

    2ad83df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @tseaver
    tseaver

Labels

api: bigqueryIssues related to the BigQuery API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.