Skip to content

Commit a1942ba

Browse files
Allow job_id to be explicitly specified. (#3779)
1 parent 1fcd53d commit a1942ba

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

bigquery/google/cloud/bigquery/dbapi/cursor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _set_rowcount(self, query_results):
100100
total_rows = num_dml_affected_rows
101101
self.rowcount = total_rows
102102

103-
def execute(self, operation, parameters=None):
103+
def execute(self, operation, parameters=None, job_id=None):
104104
"""Prepare and execute a database operation.
105105
106106
.. note::
@@ -128,12 +128,17 @@ def execute(self, operation, parameters=None):
128128
:type parameters: Mapping[str, Any] or Sequence[Any]
129129
:param parameters:
130130
(Optional) dictionary or sequence of parameter values.
131+
132+
:type job_id: str
133+
:param job_id: (Optional) The job_id to use. If not set, a job ID
134+
is generated at random.
131135
"""
132136
self._query_results = None
133137
self._page_token = None
134138
self._has_fetched_all_rows = False
135139
client = self.connection._client
136-
job_id = str(uuid.uuid4())
140+
if job_id is None:
141+
job_id = str(uuid.uuid4())
137142

138143
# The DB-API uses the pyformat formatting, since the way BigQuery does
139144
# query parameters was not one of the standard options. Convert both

bigquery/tests/unit/test_dbapi_cursor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ def test_fetchall_w_row(self):
170170
self.assertEqual(len(rows), 1)
171171
self.assertEqual(rows[0], (1,))
172172

173+
def test_execute_custom_job_id(self):
174+
from google.cloud.bigquery.dbapi import connect
175+
client = self._mock_client(rows=[], num_dml_affected_rows=0)
176+
connection = connect(client)
177+
cursor = connection.cursor()
178+
cursor.execute('SELECT 1;', job_id='foo')
179+
self.assertEqual(client.run_async_query.mock_calls[0][1][0], 'foo')
180+
173181
def test_execute_w_dml(self):
174182
from google.cloud.bigquery.dbapi import connect
175183
connection = connect(

0 commit comments

Comments
 (0)