2929import google .api_core .exceptions
3030import google .api_core .retry
3131import google .cloud .bigquery as bigquery
32+ import google .cloud .bigquery ._job_helpers
33+ import google .cloud .bigquery .table
3234
3335from bigframes .core import log_adapter
3436import bigframes .core .compile .googlesql as googlesql
37+ import bigframes .core .events
3538import bigframes .core .sql
36- import bigframes .formatting_helpers as formatting_helpers
3739import bigframes .session .metrics
3840
3941CHECK_DRIVE_PERMISSIONS = "\n Check https://cloud.google.com/bigquery/docs/query-drive-data#Google_Drive_permissions."
@@ -238,6 +240,15 @@ def add_and_trim_labels(job_config):
238240 )
239241
240242
243+ def publish_bq_event (event ):
244+ if isinstance (event , google .cloud .bigquery ._job_helpers .QuerySentEvent ):
245+ bf_event = bigframes .core .events .BigQuerySentEvent .from_bqclient (event )
246+ else :
247+ bf_event = bigframes .core .events .BigQueryUnknownEvent (event )
248+
249+ bigframes .core .events .publisher .send (bf_event )
250+
251+
241252@overload
242253def start_query_with_client (
243254 bq_client : bigquery .Client ,
@@ -249,7 +260,7 @@ def start_query_with_client(
249260 timeout : Optional [float ],
250261 metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
251262 query_with_job : Literal [True ],
252- ) -> Tuple [bigquery .table .RowIterator , bigquery .QueryJob ]:
263+ ) -> Tuple [google . cloud . bigquery .table .RowIterator , bigquery .QueryJob ]:
253264 ...
254265
255266
@@ -264,7 +275,7 @@ def start_query_with_client(
264275 timeout : Optional [float ],
265276 metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
266277 query_with_job : Literal [False ],
267- ) -> Tuple [bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
278+ ) -> Tuple [google . cloud . bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
268279 ...
269280
270281
@@ -280,7 +291,7 @@ def start_query_with_client(
280291 metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
281292 query_with_job : Literal [True ],
282293 job_retry : google .api_core .retry .Retry ,
283- ) -> Tuple [bigquery .table .RowIterator , bigquery .QueryJob ]:
294+ ) -> Tuple [google . cloud . bigquery .table .RowIterator , bigquery .QueryJob ]:
284295 ...
285296
286297
@@ -296,7 +307,7 @@ def start_query_with_client(
296307 metrics : Optional [bigframes .session .metrics .ExecutionMetrics ],
297308 query_with_job : Literal [False ],
298309 job_retry : google .api_core .retry .Retry ,
299- ) -> Tuple [bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
310+ ) -> Tuple [google . cloud . bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
300311 ...
301312
302313
@@ -315,23 +326,25 @@ def start_query_with_client(
315326 # https://github.com/googleapis/python-bigquery/pull/2256 merged, likely
316327 # version 3.36.0 or later.
317328 job_retry : google .api_core .retry .Retry = third_party_gcb_retry .DEFAULT_JOB_RETRY ,
318- ) -> Tuple [bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
329+ ) -> Tuple [google . cloud . bigquery .table .RowIterator , Optional [bigquery .QueryJob ]]:
319330 """
320331 Starts query job and waits for results.
321332 """
333+ # Note: Ensure no additional labels are added to job_config after this
334+ # point, as `add_and_trim_labels` ensures the label count does not
335+ # exceed MAX_LABELS_COUNT.
336+ add_and_trim_labels (job_config )
337+
322338 try :
323- # Note: Ensure no additional labels are added to job_config after this
324- # point, as `add_and_trim_labels` ensures the label count does not
325- # exceed MAX_LABELS_COUNT.
326- add_and_trim_labels (job_config )
327339 if not query_with_job :
328- results_iterator = bq_client .query_and_wait (
340+ results_iterator = bq_client ._query_and_wait_bigframes (
329341 sql ,
330342 job_config = job_config ,
331343 location = location ,
332344 project = project ,
333345 api_timeout = timeout ,
334346 job_retry = job_retry ,
347+ callback = publish_bq_event ,
335348 )
336349 if metrics is not None :
337350 metrics .count_job_stats (row_iterator = results_iterator )
@@ -350,14 +363,32 @@ def start_query_with_client(
350363 ex .message += CHECK_DRIVE_PERMISSIONS
351364 raise
352365
353- opts = bigframes .options .display
354- if opts .progress_bar is not None and not query_job .configuration .dry_run :
355- results_iterator = formatting_helpers .wait_for_query_job (
356- query_job ,
357- progress_bar = opts .progress_bar ,
366+ if not query_job .configuration .dry_run :
367+ bigframes .core .events .publisher .send (
368+ bigframes .core .events .BigQuerySentEvent (
369+ sql ,
370+ billing_project = query_job .project ,
371+ location = query_job .location ,
372+ job_id = query_job .job_id ,
373+ request_id = None ,
374+ )
375+ )
376+ results_iterator = query_job .result ()
377+ if not query_job .configuration .dry_run :
378+ bigframes .core .events .publisher .send (
379+ bigframes .core .events .BigQueryFinishedEvent (
380+ billing_project = query_job .project ,
381+ location = query_job .location ,
382+ job_id = query_job .job_id ,
383+ destination = query_job .destination ,
384+ total_rows = results_iterator .total_rows ,
385+ total_bytes_processed = query_job .total_bytes_processed ,
386+ slot_millis = query_job .slot_millis ,
387+ created = query_job .created ,
388+ started = query_job .started ,
389+ ended = query_job .ended ,
390+ )
358391 )
359- else :
360- results_iterator = query_job .result ()
361392
362393 if metrics is not None :
363394 metrics .count_job_stats (query_job = query_job )
0 commit comments