3939
4040import copy
4141import dataclasses
42+ import datetime
4243import functools
4344import uuid
4445import textwrap
@@ -120,7 +121,8 @@ def query_jobs_insert(
120121 retry : Optional [retries .Retry ],
121122 timeout : Optional [float ],
122123 job_retry : Optional [retries .Retry ],
123- callback : Callable ,
124+ * ,
125+ callback : Callable = lambda _ : None ,
124126) -> job .QueryJob :
125127 """Initiate a query using jobs.insert.
126128
@@ -146,15 +148,16 @@ def do_query():
146148
147149 try :
148150 query_job ._begin (retry = retry , timeout = timeout )
149- callback (
150- query_sent_factory (
151- query = query ,
152- billing_project = query_job .project ,
153- location = query_job .location ,
154- job_id = query_job .job_id ,
155- request_id = None ,
151+ if job_config is not None and not job_config .dry_run :
152+ callback (
153+ query_sent_factory (
154+ query = query ,
155+ billing_project = query_job .project ,
156+ location = query_job .location ,
157+ job_id = query_job .job_id ,
158+ request_id = None ,
159+ )
156160 )
157- )
158161 except core_exceptions .Conflict as create_exc :
159162 # The thought is if someone is providing their own job IDs and they get
160163 # their job ID generation wrong, this could end up returning results for
@@ -526,15 +529,16 @@ def do_query():
526529 request_body ["requestId" ] = request_id
527530 span_attributes = {"path" : path }
528531
529- callback (
530- query_sent_factory (
531- query = query ,
532- billing_project = project ,
533- location = location ,
534- job_id = None ,
535- request_id = request_id ,
532+ if "dryRun" not in request_body :
533+ callback (
534+ query_sent_factory (
535+ query = query ,
536+ billing_project = project ,
537+ location = location ,
538+ job_id = None ,
539+ request_id = request_id ,
540+ )
536541 )
537- )
538542
539543 # For easier testing, handle the retries ourselves.
540544 if retry is not None :
@@ -581,18 +585,25 @@ def do_query():
581585 callback = callback ,
582586 )
583587
584- callback (
585- QueryFinishedEvent (
586- billing_project = project ,
587- location = query_results .location ,
588- query_id = query_results .query_id ,
589- job_id = query_results .job_id ,
590- total_rows = query_results .total_rows ,
591- total_bytes_processed = query_results .total_bytes_processed ,
592- slot_millis = query_results .slot_millis ,
593- destination = None ,
588+ if "dryRun" not in request_body :
589+ callback (
590+ QueryFinishedEvent (
591+ billing_project = project ,
592+ location = query_results .location ,
593+ query_id = query_results .query_id ,
594+ job_id = query_results .job_id ,
595+ total_rows = query_results .total_rows ,
596+ total_bytes_processed = query_results .total_bytes_processed ,
597+ slot_millis = query_results .slot_millis ,
598+ destination = None ,
599+ # TODO(tswast): After
600+ # https://github.com/googleapis/python-bigquery/pull/2260 goes in, add
601+ # created, started, ended properties here.
602+ created = None ,
603+ started = None ,
604+ ended = None ,
605+ )
594606 )
595- )
596607 return table .RowIterator (
597608 client = client ,
598609 api_request = functools .partial (client ._call_api , retry , timeout = api_timeout ),
@@ -660,42 +671,51 @@ def _wait_or_cancel(
660671 retry : Optional [retries .Retry ],
661672 page_size : Optional [int ],
662673 max_results : Optional [int ],
663- callback : Callable ,
674+ * ,
675+ callback : Callable = lambda _ : None ,
664676) -> table .RowIterator :
665677 """Wait for a job to complete and return the results.
666678
667679 If we can't return the results within the ``wait_timeout``, try to cancel
668680 the job.
669681 """
670682 try :
671- callback (
672- QueryReceivedEvent (
673- billing_project = job .project ,
674- location = job .location ,
675- job_id = job .job_id ,
676- statement_type = job .statement_type ,
677- state = job .state ,
678- query_plan = job .query_plan ,
683+ if not job .dry_run :
684+ callback (
685+ QueryReceivedEvent (
686+ billing_project = job .project ,
687+ location = job .location ,
688+ job_id = job .job_id ,
689+ statement_type = job .statement_type ,
690+ state = job .state ,
691+ query_plan = job .query_plan ,
692+ created = job .created ,
693+ started = job .started ,
694+ ended = job .ended ,
695+ )
679696 )
680- )
681697 query_results = job .result (
682698 page_size = page_size ,
683699 max_results = max_results ,
684700 retry = retry ,
685701 timeout = wait_timeout ,
686702 )
687- callback (
688- QueryFinishedEvent (
689- billing_project = job .project ,
690- location = query_results .location ,
691- query_id = query_results .query_id ,
692- job_id = query_results .job_id ,
693- total_rows = query_results .total_rows ,
694- total_bytes_processed = query_results .total_bytes_processed ,
695- slot_millis = query_results .slot_millis ,
696- destination = job .destination ,
703+ if not job .dry_run :
704+ callback (
705+ QueryFinishedEvent (
706+ billing_project = job .project ,
707+ location = query_results .location ,
708+ query_id = query_results .query_id ,
709+ job_id = query_results .job_id ,
710+ total_rows = query_results .total_rows ,
711+ total_bytes_processed = query_results .total_bytes_processed ,
712+ slot_millis = query_results .slot_millis ,
713+ destination = job .destination ,
714+ created = job .created ,
715+ started = job .started ,
716+ ended = job .ended ,
717+ )
697718 )
698- )
699719 return query_results
700720 except Exception :
701721 # Attempt to cancel the job since we can't return the results.
@@ -719,6 +739,9 @@ class QueryFinishedEvent:
719739 total_rows : Optional [int ]
720740 total_bytes_processed : Optional [int ]
721741 slot_millis : Optional [int ]
742+ created : Optional [datetime .datetime ]
743+ started : Optional [datetime .datetime ]
744+ ended : Optional [datetime .datetime ]
722745
723746
724747@dataclasses .dataclass (frozen = True )
@@ -731,6 +754,9 @@ class QueryReceivedEvent:
731754 statement_type : Optional [str ]
732755 state : Optional [str ]
733756 query_plan : Optional [list [google .cloud .bigquery .job .query .QueryPlanEntry ]]
757+ created : Optional [datetime .datetime ]
758+ started : Optional [datetime .datetime ]
759+ ended : Optional [datetime .datetime ]
734760
735761
736762@dataclasses .dataclass (frozen = True )
0 commit comments