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 — `google.cloud.bigquery.job.QueryJob.exception` has wrong behavior · Issue #451 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

google.cloud.bigquery.job.QueryJob.exception has wrong behavior #451

Copy link
Copy link
Closed
#467
Closed
google.cloud.bigquery.job.QueryJob.exception has wrong behavior#451
#467
Copy link
Assignees
plamut
Labels
api: bigqueryIssues related to the googleapis/python-bigquery API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Description

@alexandreyc
alexandreyc
opened on Dec 24, 2020
Issue body actions

Hello,

According to the documentation of google.cloud.bigquery.job.QueryJob.exception, this method should returns an exception (and not raises) if something went wrong. But according to my tests this is not always the case:

from google.cloud import bigquery

client = bigquery.Client()

# Correct behavior: returns an exception and does not raise.
query = 'WRONG QUERY'
job = client.query(query)
exc = job.exception()

# Wrong behavior: raises an exception instead of returning it.
query = 'ASSERT (SELECT 2 > 3) AS "Error !!!"'
job = client.query(query)
try:
    job.exception()
except:
    print('raised!')

Am I misunderstanding the documentation or is this the correct behavior?

I'm using google-cloud-bigquery==2.6.1 with Python 3.8.6. Let me know if you need more informations.

Thanks for your help,

Activity

  1. product-auto-label
    added
    api: bigqueryIssues related to the googleapis/python-bigquery API.
    on Dec 24, 2020
  2. yoshi-automation
    added
    triage meI really want to be triaged.
    on Dec 25, 2020
  3. HemangChothani
    added
    type: questionRequest for information or clarification. Not an issue.
    and removed
    triage meI really want to be triaged.
    on Dec 28, 2020
  4. HemangChothani commented on Dec 28, 2020

    @HemangChothani
    HemangChothani
    on Dec 28, 2020
    Contributor
    More actions

    @tswast @busunkim96 Please suggest.
    I think something is missing for the scenario when query.state is RUNNING and library wants to retry it, it internally call the client._get_query_results method which throws an error of BadRequest.

    I am not sure about to handle this in bigquery library and set the google.api_core.future.polling.PollingFuture.set_exception from the query.py file as error raised from google-cloud-core http client.

  5. alexandreyc commented on Dec 28, 2020

    @alexandreyc
    alexandreyc
    on Dec 28, 2020
    Author
    More actions

    Working on something related, I discovered that the problem does not exist when doing a double assertion:

    # Correct behavior: returns an exception and does not raise.
    query = 'ASSERT (SELECT 2 > 3) AS "Error !!!"; ASSERT (SELECT 2 > 3) AS "Error again !!!";'
    job = client.query(query)
    exc = job.exception()

    Hope it can help...

    Thanks,

    Reacted by HemangChothani
  6. HemangChothani commented on Dec 29, 2020

    @HemangChothani
    HemangChothani
    on Dec 29, 2020
    Contributor
    More actions

    @tswast Backend consider "INVALID_ARGUMENT" and gave the reason "invalidQuery" when query contains ASSERT, but when query contains only one ASSERT like query = 'ASSERT (SELECT 2 > 3) AS "Error !!!"' it returns the 400 status code , hence exception raised from http client, but when query contains multiple ASSERT like 'ASSERT (SELECT 2 > 3) AS "Error !!!"; ASSERT (SELECT 2 > 3) AS "Error again !!!";', it returns 200 status code and the error result with message.

  7. HemangChothani
    closed this as completedon Dec 29, 2020
  8. HemangChothani
    reopened this on Dec 29, 2020
  9. tswast commented on Jan 5, 2021

    @tswast
    tswast
    on Jan 5, 2021
    Contributor
    More actions

    Regarding #451 (comment) I believe this is a difference in backend API behavior of getQueryResults between scripting jobs and non-scripting query jobs.

    CC @shollyman

  10. tswast commented on Jan 5, 2021

    @tswast
    tswast
    on Jan 5, 2021
    Contributor
    More actions

    Regarding this issue, it appears that this library may be using the google-api-core library slightly incorrectly. The base class (where exception() is defined) assumes that _blocking_poll doesn't raise any exception

    https://github.com/googleapis/python-api-core/blob/b51b7f587042fe9340371c1b5c8e9adf8001c43a/google/api_core/future/polling.py#L129-L134

    Instead, it is expected that set_exception() is called.

    We do this here:

    python-bigquery/google/cloud/bigquery/job/base.py

    Line 582 in 2849e56

    self.set_exception(exception)

    But we are missing the case when the getQueryResults backend API throws an error.

    We probably need a try-catch block in QueryJob.done() as it is not expected (or documented) to raise an exception.

    python-bigquery/google/cloud/bigquery/job/query.py

    Line 1023 in 2849e56

    self._query_results = self._client._get_query_results(

    If _get_query_results does raise an exception, we might have to inspect it a bit to determine if the job is complete (failed) or it was just a transient issue.

  11. tswast
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    and removed
    type: questionRequest for information or clarification. Not an issue.
    on Jan 5, 2021
  12. plamut commented on Jan 7, 2021

    @plamut
    plamut
    on Jan 7, 2021
    Contributor
    More actions

    @tswast Has this proposal been confirmed? If yes, I can start implementing it (as there's no assignee on the ticket yet).

  13. tswast commented on Jan 7, 2021

    @tswast
    tswast
    on Jan 7, 2021
    Contributor
    More actions

    Yes. Let's make sure that exception() (and also done()) does not throw.

  14. tswast
    assigned
    plamut
    on Jan 7, 2021
  15. tswast commented on Jan 7, 2021

    @tswast
    tswast
    on Jan 7, 2021
    Contributor
    More actions

    result() should continue to throw (though it might throw because of set_exception instead of the current behavior that bubbles up from done())

  16. tswast
    closed this as completedin #467on Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @plamut
    plamut

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

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.