Skip to content

Commit

Permalink
ENH: Add timeout support (googleapis#76)
Browse files Browse the repository at this point in the history
* Add timeout support

* Fix a pep8 issue

* Check before fetch results

* Add test

* timeoutMs -> timeout_ms

* Add changelog
  • Loading branch information
Takashi Nishibayashi authored and parthea committed Aug 4, 2017
1 parent be301e2 commit f6731a7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
0.2.1 / 2017-??-??
------------------

- :func:`read_gbq` now handles query configuration `query.timeoutMs` and stop waiting. (:issue:`76`)

0.2.0 / 2017-07-24
------------------

Expand Down
12 changes: 12 additions & 0 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ class NotFoundException(ValueError):
pass


class QueryTimeout(ValueError):
"""
Raised when the query job timeout
"""
pass


class StreamingInsertError(ValueError):
"""
Raised when BigQuery reports a streaming insert error.
Expand Down Expand Up @@ -536,6 +543,11 @@ def run_query(self, query, **kwargs):

while not query_reply.get('jobComplete', False):
self.print_elapsed_seconds(' Elapsed', 's. Waiting...')

timeout_ms = job_config['query'].get('timeoutMs')
if timeout_ms and timeout_ms < self.get_elapsed_seconds() * 1000:
raise QueryTimeout('Query timeout: {} ms'.format(timeout_ms))

try:
query_reply = job_collection.getQueryResults(
projectId=job_reference['projectId'],
Expand Down
13 changes: 13 additions & 0 deletions pandas_gbq/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,19 @@ def test_configuration_raises_value_error_with_multiple_config(self):
private_key=_get_private_key_path(),
configuration=config)

def test_timeout_configuration(self):
sql_statement = 'SELECT 1'
config = {
'query': {
"timeoutMs": 1
}
}
# Test that QueryTimeout error raises
with pytest.raises(gbq.QueryTimeout):
gbq.read_gbq(sql_statement, project_id=_get_project_id(),
private_key=_get_private_key_path(),
configuration=config)

def test_query_response_bytes(self):
assert self.gbq_connector.sizeof_fmt(999) == "999.0 B"
assert self.gbq_connector.sizeof_fmt(1024) == "1.0 KB"
Expand Down

0 comments on commit f6731a7

Please sign in to comment.