|
14 | 14 |
|
15 | 15 | """Client for interacting with the Google BigQuery API.""" |
16 | 16 |
|
17 | | -from __future__ import absolute_import |
18 | | -from __future__ import annotations |
19 | | -from __future__ import division |
| 17 | +from __future__ import absolute_import, annotations, division |
20 | 18 |
|
21 | | -from collections import abc as collections_abc |
22 | 19 | import copy |
23 | 20 | import datetime |
24 | 21 | import functools |
|
30 | 27 | import os |
31 | 28 | import tempfile |
32 | 29 | import typing |
| 30 | +import uuid |
| 31 | +import warnings |
| 32 | +from collections import abc as collections_abc |
33 | 33 | from typing import ( |
| 34 | + IO, |
34 | 35 | Any, |
35 | 36 | Callable, |
36 | 37 | Dict, |
37 | | - IO, |
38 | 38 | Iterable, |
39 | | - Mapping, |
40 | 39 | List, |
| 40 | + Mapping, |
41 | 41 | Optional, |
42 | 42 | Sequence, |
43 | 43 | Tuple, |
44 | 44 | Union, |
45 | 45 | ) |
46 | | -import uuid |
47 | | -import warnings |
48 | | - |
49 | | -import requests |
50 | | - |
51 | | -from google import resumable_media # type: ignore |
52 | | -from google.resumable_media.requests import MultipartUpload # type: ignore |
53 | | -from google.resumable_media.requests import ResumableUpload |
54 | 46 |
|
55 | 47 | import google.api_core.client_options |
56 | 48 | import google.api_core.exceptions as core_exceptions |
57 | | -from google.api_core.iam import Policy |
| 49 | +import google.cloud._helpers # type: ignore |
| 50 | +import requests |
| 51 | +from google import resumable_media # type: ignore |
58 | 52 | from google.api_core import page_iterator |
59 | 53 | from google.api_core import retry as retries |
60 | | -import google.cloud._helpers # type: ignore |
| 54 | +from google.api_core.iam import Policy |
61 | 55 | from google.cloud import exceptions # pytype: disable=import-error |
62 | | -from google.cloud.client import ClientWithProject # type: ignore # pytype: disable=import-error |
| 56 | +from google.cloud.client import ( |
| 57 | + ClientWithProject, # type: ignore # pytype: disable=import-error |
| 58 | +) |
| 59 | +from google.resumable_media.requests import ( |
| 60 | + MultipartUpload, # type: ignore |
| 61 | + ResumableUpload, |
| 62 | +) |
63 | 63 |
|
64 | 64 | try: |
65 | 65 | from google.cloud.bigquery_storage_v1.services.big_query_read.client import ( |
|
70 | 70 |
|
71 | 71 |
|
72 | 72 | from google.auth.credentials import Credentials |
73 | | -from google.cloud.bigquery._http import Connection |
74 | | -from google.cloud.bigquery import _job_helpers |
75 | | -from google.cloud.bigquery import _pandas_helpers |
76 | | -from google.cloud.bigquery import _versions_helpers |
77 | | -from google.cloud.bigquery import enums |
| 73 | +from google.cloud.bigquery import ( |
| 74 | + _job_helpers, |
| 75 | + _pandas_helpers, |
| 76 | + _versions_helpers, |
| 77 | + enums, |
| 78 | + job, |
| 79 | +) |
78 | 80 | from google.cloud.bigquery import exceptions as bq_exceptions |
79 | | -from google.cloud.bigquery import job |
80 | | -from google.cloud.bigquery._helpers import _get_sub_prop |
81 | | -from google.cloud.bigquery._helpers import _record_field_to_json |
82 | | -from google.cloud.bigquery._helpers import _str_or_none |
83 | | -from google.cloud.bigquery._helpers import _verify_job_config_type |
84 | | -from google.cloud.bigquery._helpers import _get_bigquery_host |
85 | | -from google.cloud.bigquery._helpers import _DEFAULT_HOST |
86 | | -from google.cloud.bigquery._helpers import _DEFAULT_HOST_TEMPLATE |
87 | | -from google.cloud.bigquery._helpers import _DEFAULT_UNIVERSE |
88 | | -from google.cloud.bigquery._helpers import _validate_universe |
89 | | -from google.cloud.bigquery._helpers import _get_client_universe |
90 | | -from google.cloud.bigquery._helpers import TimeoutType |
| 81 | +from google.cloud.bigquery._helpers import ( |
| 82 | + _DEFAULT_HOST, |
| 83 | + _DEFAULT_HOST_TEMPLATE, |
| 84 | + _DEFAULT_UNIVERSE, |
| 85 | + TimeoutType, |
| 86 | + _get_bigquery_host, |
| 87 | + _get_client_universe, |
| 88 | + _get_sub_prop, |
| 89 | + _record_field_to_json, |
| 90 | + _str_or_none, |
| 91 | + _validate_universe, |
| 92 | + _verify_job_config_type, |
| 93 | +) |
| 94 | +from google.cloud.bigquery._http import Connection |
91 | 95 | from google.cloud.bigquery._job_helpers import make_job_id as _make_job_id |
92 | | -from google.cloud.bigquery.dataset import Dataset |
93 | | -from google.cloud.bigquery.dataset import DatasetListItem |
94 | | -from google.cloud.bigquery.dataset import DatasetReference |
95 | | - |
| 96 | +from google.cloud.bigquery.dataset import Dataset, DatasetListItem, DatasetReference |
96 | 97 | from google.cloud.bigquery.enums import AutoRowIDs, DatasetView, UpdateMode |
97 | 98 | from google.cloud.bigquery.format_options import ParquetOptions |
98 | 99 | from google.cloud.bigquery.job import ( |
|
105 | 106 | QueryJob, |
106 | 107 | QueryJobConfig, |
107 | 108 | ) |
108 | | -from google.cloud.bigquery.model import Model |
109 | | -from google.cloud.bigquery.model import ModelReference |
110 | | -from google.cloud.bigquery.model import _model_arg_to_model_ref |
| 109 | +from google.cloud.bigquery.model import Model, ModelReference, _model_arg_to_model_ref |
111 | 110 | from google.cloud.bigquery.opentelemetry_tracing import create_span |
112 | 111 | from google.cloud.bigquery.query import _QueryResults |
113 | 112 | from google.cloud.bigquery.retry import ( |
| 113 | + DEFAULT_GET_JOB_TIMEOUT, |
114 | 114 | DEFAULT_JOB_RETRY, |
115 | 115 | DEFAULT_RETRY, |
116 | 116 | DEFAULT_TIMEOUT, |
117 | | - DEFAULT_GET_JOB_TIMEOUT, |
118 | 117 | POLLING_DEFAULT_VALUE, |
119 | 118 | ) |
120 | | -from google.cloud.bigquery.routine import Routine |
121 | | -from google.cloud.bigquery.routine import RoutineReference |
| 119 | +from google.cloud.bigquery.routine import Routine, RoutineReference |
122 | 120 | from google.cloud.bigquery.schema import SchemaField |
123 | | -from google.cloud.bigquery.table import _table_arg_to_table |
124 | | -from google.cloud.bigquery.table import _table_arg_to_table_ref |
125 | | -from google.cloud.bigquery.table import Table |
126 | | -from google.cloud.bigquery.table import TableListItem |
127 | | -from google.cloud.bigquery.table import TableReference |
128 | | -from google.cloud.bigquery.table import RowIterator |
| 121 | +from google.cloud.bigquery.table import ( |
| 122 | + RowIterator, |
| 123 | + Table, |
| 124 | + TableListItem, |
| 125 | + TableReference, |
| 126 | + _table_arg_to_table, |
| 127 | + _table_arg_to_table_ref, |
| 128 | +) |
129 | 129 |
|
130 | 130 | pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import() |
131 | 131 | pandas = ( |
@@ -621,11 +621,45 @@ def _ensure_bqstorage_client( |
621 | 621 | ) |
622 | 622 | return None |
623 | 623 |
|
624 | | - if bqstorage_client is None: # pragma: NO COVER |
| 624 | + # Import here since it should be installed if the BQ Storage client is |
| 625 | + # also installed. |
| 626 | + import google.api_core.gapic_v1.client_info |
| 627 | + |
| 628 | + try: |
| 629 | + import pandas_gbq # type: ignore |
| 630 | + except ImportError: |
| 631 | + pandas_gbq = None # type: ignore |
| 632 | + |
| 633 | + if pandas_gbq is None: |
| 634 | + user_agent = "pandas-gbq/0.0.0" |
| 635 | + else: |
| 636 | + user_agent = f"pandas-gbq/{pandas_gbq.__version__}" |
| 637 | + |
| 638 | + if client_info is None: |
| 639 | + amended_client_info = google.api_core.gapic_v1.client_info.ClientInfo( |
| 640 | + user_agent=user_agent, |
| 641 | + ) |
| 642 | + else: |
| 643 | + if client_info.user_agent is None: |
| 644 | + amended_user_agent = user_agent |
| 645 | + else: |
| 646 | + amended_user_agent = client_info.user_agent + " " + user_agent |
| 647 | + |
| 648 | + amended_client_info = google.api_core.gapic_v1.client_info.ClientInfo( |
| 649 | + python_version=client_info.python_version, |
| 650 | + grpc_version=client_info.grpc_version, |
| 651 | + api_core_version=client_info.api_core_version, |
| 652 | + gapic_version=client_info.gapic_version, |
| 653 | + user_agent=amended_user_agent, |
| 654 | + rest_version=client_info.rest_version, |
| 655 | + protobuf_runtime_version=client_info.protobuf_runtime_version, |
| 656 | + ) |
| 657 | + |
| 658 | + if bqstorage_client is None: |
625 | 659 | bqstorage_client = bigquery_storage.BigQueryReadClient( |
626 | 660 | credentials=self._credentials, |
627 | 661 | client_options=client_options, |
628 | | - client_info=client_info, # type: ignore # (None is also accepted) |
| 662 | + client_info=amended_client_info, |
629 | 663 | ) |
630 | 664 |
|
631 | 665 | return bqstorage_client |
|
0 commit comments