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

BigQuery: Print offending SQL on error with QueryJob.result() #5408

Closed
bencaine1 opened this issue May 30, 2018 · 2 comments · Fixed by #8748
Closed

BigQuery: Print offending SQL on error with QueryJob.result() #5408

bencaine1 opened this issue May 30, 2018 · 2 comments · Fixed by #8748
Assignees
Labels
api: bigquery Issues related to the BigQuery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@bencaine1
Copy link

OS: Linux dc32b7e8763a 4.9.0-6-amd64 #1 SMP Debian 4.9.82-1+deb9u3 (2018-03-02) x86_64 x86_64 x86_64 GNU/Linux
Python version: Python 2.7.6
google-cloud-bigquery: 1.1.0

It would be really helpful if the error message to QueryJob.result() printed out the query, along with line and column numbers. Here's our current workaround, which unfortunately does not allow us to use the built-in Retry due to a nested try/except.

RETRYABLE_EXCEPTIONS = (InternalServerError, TooManyRequests, ServiceUnavailable, BadGateway)

...

    def _execute_query_job(self, query_job, query, max_wait_secs=None):
        # Need to do retries manually, since the built-in retry doesn't work within a try/except.
        start_time = datetime.datetime.utcnow()
        deadline = start_time + datetime.timedelta(seconds=max_wait_secs or self.max_wait_secs)
        i = 1
        while datetime.datetime.utcnow() < deadline:
            try:
                return query_job.result()
            except RETRYABLE_EXCEPTIONS:
                time.sleep(i**2)
                i += 1
            except GoogleCloudError as e:
                msg = str(e)
                # Put line numbers next to the SQL.
                lines = query.split('\n')
                longest = max(len(l) for l in lines)
                # Print out a 'ruler' above and below the SQL so we can judge columns.
                ruler = ' ' * 4 + '|'  # Left pad for the line numbers (4 digits plus ':')
                for _ in range(longest / 10):
                    ruler += ' ' * 4 + '.' + ' ' * 4 + '|'
                header = '-----Offending Sql Follows-----'
                padding = ' ' * ((longest - len(header)) / 2)
                msg += '\n\n{}{}\n\n{}\n{}\n{}'.format(padding, header, ruler, '\n'.join(
                    '{:4}:{}'.format(n + 1, line) for n, line in enumerate(lines)), ruler)
                raise RuntimeError(msg)
        raise RuntimeError('Deadline of {} seconds exceeded.'.format(max_wait_secs))
@tseaver tseaver added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. api: bigquery Issues related to the BigQuery API. labels May 30, 2018
@plamut plamut self-assigned this Jul 24, 2019
@tswast
Copy link
Contributor

tswast commented Jul 24, 2019

Import information for query errors includes the job ID, any error messages, and as suggested: the SQL text.

@plamut
Copy link
Contributor

plamut commented Jul 24, 2019

Makes sense, will add job ID as well.

plamut added a commit to plamut/google-cloud-python that referenced this issue Jul 24, 2019
The code for query format in exception messages is a modified version
of the original proposal by @bencaine1 in the feature request:

googleapis#5408 (comment)
plamut added a commit to plamut/google-cloud-python that referenced this issue Jul 24, 2019
The code for query format in exception messages is a modified version
of the original proposal by @bencaine1 in the feature request:

googleapis#5408 (comment)
plamut added a commit to plamut/google-cloud-python that referenced this issue Jul 26, 2019
The code for query format in exception messages is a modified version
of the original proposal by @bencaine1 in the feature request:

googleapis#5408 (comment)
tswast pushed a commit that referenced this issue Jul 26, 2019
* Include SQL query and job ID in job errors

The code for query format in exception messages is a modified version
of the original proposal by @bencaine1 in the feature request:

#5408 (comment)

* Simplify amending message on job result error

All GoogleAPICallError (GoogleCloudError) instances have the "message"
attribute, even in Python 3, thus the additional info can be appended
directly to that attribute.

* Change query header text in job error messages

It's not always the query that causes the error, thus this commit
makes the header text more neutral.
plamut added a commit to googleapis/python-bigquery that referenced this issue Feb 4, 2020
* Include SQL query and job ID in job errors

The code for query format in exception messages is a modified version
of the original proposal by @bencaine1 in the feature request:

googleapis/google-cloud-python#5408 (comment)

* Simplify amending message on job result error

All GoogleAPICallError (GoogleCloudError) instances have the "message"
attribute, even in Python 3, thus the additional info can be appended
directly to that attribute.

* Change query header text in job error messages

It's not always the query that causes the error, thus this commit
makes the header text more neutral.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants