Skip to content

Commit 75168c7

Browse files
committed
fix unit tests
1 parent de668d6 commit 75168c7

File tree

4 files changed

+280
-65
lines changed

4 files changed

+280
-65
lines changed

google/cloud/bigquery/_job_helpers.py

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import copy
4141
import dataclasses
42+
import datetime
4243
import functools
4344
import uuid
4445
import 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)

google/cloud/bigquery/job/query.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,8 @@ def result( # type: ignore # (incompatible with supertype)
15501550
return _EmptyRowIterator(
15511551
project=self.project,
15521552
location=self.location,
1553+
schema=self.schema,
1554+
total_bytes_processed=self.total_bytes_processed,
15531555
# Intentionally omit job_id and query_id since this doesn't
15541556
# actually correspond to a finished query job.
15551557
)
@@ -1737,7 +1739,11 @@ def is_job_done():
17371739
project=self.project,
17381740
job_id=self.job_id,
17391741
query_id=self.query_id,
1742+
schema=self.schema,
17401743
num_dml_affected_rows=self._query_results.num_dml_affected_rows,
1744+
query=self.query,
1745+
total_bytes_processed=self.total_bytes_processed,
1746+
slot_millis=self.slot_millis,
17411747
)
17421748

17431749
# We know that there's at least 1 row, so only treat the response from

google/cloud/bigquery/table.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ def __init__(
18261826
page_start=_rows_page_start,
18271827
next_token="pageToken",
18281828
)
1829-
schema = _to_schema_fields(schema)
1829+
schema = _to_schema_fields(schema) if schema else ()
18301830
self._field_to_index = _helpers._field_to_index_mapping(schema)
18311831
self._page_size = page_size
18321832
self._preserve_order = False
@@ -2888,7 +2888,6 @@ class _EmptyRowIterator(RowIterator):
28882888
statements.
28892889
"""
28902890

2891-
schema = ()
28922891
pages = ()
28932892
total_rows = 0
28942893

0 commit comments

Comments
 (0)