@@ -262,73 +262,7 @@ def publish_bq_event(event):
262262 return publish_bq_event
263263
264264
265- @overload
266- def start_query_with_client (
267- bq_client : bigquery .Client ,
268- sql : str ,
269- * ,
270- job_config : bigquery .QueryJobConfig ,
271- location : Optional [str ],
272- project : Optional [str ],
273- timeout : Optional [float ],
274- metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
275- query_with_job : Literal [True ],
276- publisher : bigframes .core .events .Publisher ,
277- session = None ,
278- ) -> Tuple [google .cloud .bigquery .table .RowIterator , bigquery .QueryJob ]: ...
279-
280-
281- @overload
282- def start_query_with_client (
283- bq_client : bigquery .Client ,
284- sql : str ,
285- * ,
286- job_config : bigquery .QueryJobConfig ,
287- location : Optional [str ],
288- project : Optional [str ],
289- timeout : Optional [float ],
290- metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
291- query_with_job : Literal [False ],
292- publisher : bigframes .core .events .Publisher ,
293- session = None ,
294- ) -> Tuple [google .cloud .bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]: ...
295-
296-
297- @overload
298- def start_query_with_client (
299- bq_client : bigquery .Client ,
300- sql : str ,
301- * ,
302- job_config : bigquery .QueryJobConfig ,
303- location : Optional [str ],
304- project : Optional [str ],
305- timeout : Optional [float ],
306- metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
307- query_with_job : Literal [True ],
308- job_retry : google .api_core .retry .Retry ,
309- publisher : bigframes .core .events .Publisher ,
310- session = None ,
311- ) -> Tuple [google .cloud .bigquery .table .RowIterator , bigquery .QueryJob ]: ...
312-
313-
314- @overload
315- def start_query_with_client (
316- bq_client : bigquery .Client ,
317- sql : str ,
318- * ,
319- job_config : bigquery .QueryJobConfig ,
320- location : Optional [str ],
321- project : Optional [str ],
322- timeout : Optional [float ],
323- metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
324- query_with_job : Literal [False ],
325- job_retry : google .api_core .retry .Retry ,
326- publisher : bigframes .core .events .Publisher ,
327- session = None ,
328- ) -> Tuple [google .cloud .bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]: ...
329-
330-
331- def start_query_with_client (
265+ def start_query_with_job (
332266 bq_client : bigquery .Client ,
333267 sql : str ,
334268 * ,
@@ -337,7 +271,6 @@ def start_query_with_client(
337271 project : Optional [str ] = None ,
338272 timeout : Optional [float ] = None ,
339273 metrics : Optional [bigframes .session .metrics .ExecutionMetrics ] = None ,
340- query_with_job : bool = True ,
341274 # TODO(tswast): We can stop providing our own default once we use a
342275 # google-cloud-bigquery version with
343276 # https://github.com/googleapis/python-bigquery/pull/2256 merged, likely
@@ -355,20 +288,6 @@ def start_query_with_client(
355288 add_and_trim_labels (job_config , session = session )
356289
357290 try :
358- if not query_with_job :
359- results_iterator = bq_client ._query_and_wait_bigframes (
360- sql ,
361- job_config = job_config ,
362- location = location ,
363- project = project ,
364- api_timeout = timeout ,
365- job_retry = job_retry ,
366- callback = create_bq_event_callback (publisher ),
367- )
368- if metrics is not None :
369- metrics .count_job_stats (row_iterator = results_iterator )
370- return results_iterator , None
371-
372291 query_job = bq_client .query (
373292 sql ,
374293 job_config = job_config ,
@@ -382,6 +301,61 @@ def start_query_with_client(
382301 ex .message += CHECK_DRIVE_PERMISSIONS
383302 raise
384303
304+ results_iterator = query_job .result ()
305+ _publish_events (
306+ query_job = query_job ,
307+ total_rows = results_iterator .total_rows ,
308+ sql = sql ,
309+ publisher = publisher ,
310+ metrics = metrics ,
311+ )
312+ return results_iterator , query_job
313+
314+
315+ def start_query_job_optional (
316+ bq_client : bigquery .Client ,
317+ sql : str ,
318+ * ,
319+ job_config : bigquery .QueryJobConfig ,
320+ location : Optional [str ] = None ,
321+ project : Optional [str ] = None ,
322+ timeout : Optional [float ] = None ,
323+ metrics : Optional [bigframes .session .metrics .ExecutionMetrics ] = None ,
324+ # TODO(tswast): We can stop providing our own default once we use a
325+ # google-cloud-bigquery version with
326+ # https://github.com/googleapis/python-bigquery/pull/2256 merged, likely
327+ # version 3.36.0 or later.
328+ job_retry : google .api_core .retry .Retry = third_party_gcb_retry .DEFAULT_JOB_RETRY ,
329+ publisher : bigframes .core .events .Publisher ,
330+ session = None ,
331+ ):
332+ add_and_trim_labels (job_config , session = session )
333+ try :
334+ results_iterator = bq_client ._query_and_wait_bigframes (
335+ sql ,
336+ job_config = job_config ,
337+ location = location ,
338+ project = project ,
339+ api_timeout = timeout ,
340+ job_retry = job_retry ,
341+ callback = create_bq_event_callback (publisher ),
342+ )
343+ if metrics is not None :
344+ metrics .count_job_stats (row_iterator = results_iterator )
345+ return results_iterator , None
346+ except google .api_core .exceptions .Forbidden as ex :
347+ if "Drive credentials" in ex .message :
348+ ex .message += CHECK_DRIVE_PERMISSIONS
349+ raise
350+
351+
352+ def _publish_events (
353+ query_job : bigquery .QueryJob ,
354+ sql : str ,
355+ total_rows : Optional [int ],
356+ publisher : bigframes .core .events .Publisher ,
357+ metrics : Optional [bigframes .session .metrics .ExecutionMetrics ] = None ,
358+ ):
385359 if not query_job .configuration .dry_run :
386360 publisher .publish (
387361 bigframes .core .events .BigQuerySentEvent (
@@ -392,15 +366,14 @@ def start_query_with_client(
392366 request_id = None ,
393367 )
394368 )
395- results_iterator = query_job .result ()
396369 if not query_job .configuration .dry_run :
397370 publisher .publish (
398371 bigframes .core .events .BigQueryFinishedEvent (
399372 billing_project = query_job .project ,
400373 location = query_job .location ,
401374 job_id = query_job .job_id ,
402375 destination = query_job .destination ,
403- total_rows = results_iterator . total_rows ,
376+ total_rows = total_rows ,
404377 total_bytes_processed = query_job .total_bytes_processed ,
405378 slot_millis = query_job .slot_millis ,
406379 created = query_job .created ,
@@ -411,7 +384,6 @@ def start_query_with_client(
411384
412385 if metrics is not None :
413386 metrics .count_job_stats (query_job = query_job )
414- return results_iterator , query_job
415387
416388
417389def delete_tables_matching_session_id (
@@ -473,15 +445,14 @@ def create_bq_dataset_reference(
473445 """
474446 job_config = google .cloud .bigquery .QueryJobConfig ()
475447
476- _ , query_job = start_query_with_client (
448+ _ , query_job = start_query_with_job (
477449 bq_client ,
478450 "SELECT 1" ,
479451 location = location ,
480452 job_config = job_config ,
481453 project = project ,
482454 timeout = None ,
483455 metrics = None ,
484- query_with_job = True ,
485456 publisher = publisher ,
486457 )
487458
0 commit comments