Skip to content

Commit 2a2774f

Browse files
allow setting a job config on BigQuery queries
1 parent 8fc6d9b commit 2a2774f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

parsons/google/google_bigquery.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from google.api_core import exceptions
1313
from google.cloud import bigquery
1414
from google.cloud.bigquery import dbapi, job
15-
from google.cloud.bigquery.job import ExtractJobConfig, LoadJobConfig
15+
from google.cloud.bigquery.job import ExtractJobConfig, LoadJobConfig, QueryJobConfig
1616
from google.oauth2.credentials import Credentials
1717

1818
from parsons.databases.database_connector import DatabaseConnector
@@ -249,6 +249,7 @@ def query(
249249
sql: str,
250250
parameters: Optional[Union[list, dict]] = None,
251251
return_values: bool = True,
252+
job_config: Optional[QueryJobConfig] = None,
252253
) -> Optional[Table]:
253254
"""
254255
Run a BigQuery query and return the results as a Parsons table.
@@ -277,6 +278,8 @@ def query(
277278
A valid BigTable statement
278279
parameters: dict
279280
A dictionary of query parameters for BigQuery.
281+
job_config: QueryJobConfig or None
282+
An optional QueryJobConfig object for custom behavior. See https://cloud.google.com/python/docs/reference/bigquery/latest#google.cloud.bigquery.job.QueryJobConfig
280283
281284
`Returns:`
282285
Parsons Table
@@ -285,11 +288,21 @@ def query(
285288

286289
with self.connection() as connection:
287290
return self.query_with_connection(
288-
sql, connection, parameters=parameters, return_values=return_values
291+
sql,
292+
connection,
293+
parameters=parameters,
294+
return_values=return_values,
295+
job_config=job_config,
289296
)
290297

291298
def query_with_connection(
292-
self, sql, connection, parameters=None, commit=True, return_values: bool = True
299+
self,
300+
sql,
301+
connection,
302+
parameters=None,
303+
commit=True,
304+
return_values: bool = True,
305+
job_config: Optional[QueryJobConfig] = None,
293306
):
294307
"""
295308
Execute a query against the BigQuery database, with an existing connection.
@@ -305,6 +318,8 @@ def query_with_connection(
305318
A list of python variables to be converted into SQL values in your query
306319
commit: boolean
307320
Must be true. BigQuery
321+
job_config: QueryJobConfig or None
322+
An optional QueryJobConfig object for custom behavior. See https://cloud.google.com/python/docs/reference/bigquery/latest#google.cloud.bigquery.job.QueryJobConfig
308323
309324
`Returns:`
310325
Parsons Table
@@ -325,7 +340,7 @@ def query_with_connection(
325340
# get our connection and cursor
326341
with self.cursor(connection) as cursor:
327342
# Run the query
328-
cursor.execute(sql, parameters)
343+
cursor.execute(sql, parameters, job_config=job_config)
329344

330345
if not return_values:
331346
return None

0 commit comments

Comments
 (0)