Skip to content

Commit 8e86353

Browse files
author
Conrad Dean
committed
Merge pull request tylertreat#1 from DataScience/add-lowlevel-hook-self-version-num
Add lowlevel hook self version num
2 parents f7ca89f + 692430f commit 8e86353

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

bigquery/client.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,15 @@ def __init__(self, bq_service, project_id, swallow_results=True):
120120
self.swallow_results = swallow_results
121121
self.cache = {}
122122

123-
def query(self, query, max_results=None, timeout=0, dry_run=False):
124-
"""Submit a query to BigQuery.
123+
def _submit_query_job(self, query_data):
124+
125+
""" Submit a query job to BigQuery
126+
127+
This is similar to BigQueryClient.query, but gives the user
125128
126129
Args:
127-
query: BigQuery query string.
128-
max_results: maximum number of rows to return per page of results.
129-
timeout: how long to wait for the query to complete, in seconds,
130-
before the request times out and returns.
131-
dry_run: if True, the query isn't actually run. A valid query will
132-
return an empty response, while an invalid one will return
133-
the same error message it would if it wasn't a dry run.
130+
query_data: query object as per "configuration.query" in
131+
https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query
134132
135133
Returns:
136134
job id and query results if query completed. If dry_run is True,
@@ -141,21 +139,15 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
141139
BigQueryTimeoutException on timeout
142140
"""
143141

144-
logging.debug('Executing query: %s' % query)
142+
logging.debug('Submitting query job: %s' % query_data)
145143

146144
job_collection = self.bigquery.jobs()
147-
query_data = {
148-
'query': query,
149-
'timeoutMs': timeout * 1000,
150-
'dryRun': dry_run,
151-
'maxResults': max_results,
152-
}
153145

154146
try:
155147
query_reply = job_collection.query(
156148
projectId=self.project_id, body=query_data).execute()
157149
except HttpError as e:
158-
if dry_run:
150+
if query_data.get("dryRun", False):
159151
return None, json.loads(e.content)
160152
raise
161153

@@ -166,12 +158,43 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
166158

167159
# raise exceptions if it's not an async query
168160
# and job is not completed after timeout
169-
if not job_complete and timeout:
161+
if not job_complete and query_data.get("timeoutMs", False):
170162
logging.error('BigQuery job %s timeout' % job_id)
171163
raise BigQueryTimeoutException()
172164

173165
return job_id, [self._transform_row(row, schema) for row in rows]
174166

167+
def query(self, query, max_results=None, timeout=0, dry_run=False):
168+
"""Submit a query to BigQuery.
169+
170+
Args:
171+
query: BigQuery query string.
172+
max_results: maximum number of rows to return per page of results.
173+
timeout: how long to wait for the query to complete, in seconds,
174+
before the request times out and returns.
175+
dry_run: if True, the query isn't actually run. A valid query will
176+
return an empty response, while an invalid one will return
177+
the same error message it would if it wasn't a dry run.
178+
179+
Returns:
180+
job id and query results if query completed. If dry_run is True,
181+
job id will be None and results will be empty if the query is valid
182+
or a dict containing the response if invalid.
183+
184+
Raises:
185+
BigQueryTimeoutException on timeout
186+
"""
187+
188+
logging.debug('Executing query: %s' % query)
189+
190+
query_data = {
191+
'query': query,
192+
'timeoutMs': timeout * 1000,
193+
'dryRun': dry_run,
194+
'maxResults': max_results,
195+
}
196+
return self._submit_query_job(query_data)
197+
175198
def get_query_schema(self, job_id):
176199
"""Retrieve the schema of a query by job id.
177200

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import find_packages
22
from setuptools import setup
33

4-
VERSION = '1.0.0'
4+
VERSION = '1.0.1.cpdean'
55

66
setup_args = dict(
77
name='BigQuery-Python',
@@ -26,4 +26,3 @@
2626

2727
if __name__ == '__main__':
2828
setup(**setup_args)
29-

0 commit comments

Comments
 (0)