8
8
)
9
9
from google .api_core import retry_async as retries
10
10
import asyncio
11
- import google .auth .transport ._aiohttp_requests
12
11
13
12
14
- class AsyncClient ():
13
+ class AsyncClient :
15
14
def __init__ (self , * args , ** kwargs ):
16
15
self ._client = Client (* args , ** kwargs )
17
16
18
-
19
17
async def query_and_wait (
20
18
self ,
21
19
query ,
@@ -29,14 +27,14 @@ async def query_and_wait(
29
27
job_retry : retries .AsyncRetry = DEFAULT_ASYNC_JOB_RETRY ,
30
28
page_size : Optional [int ] = None ,
31
29
max_results : Optional [int ] = None ,
32
- ) -> RowIterator :
33
-
30
+ ) -> RowIterator :
34
31
if project is None :
35
32
project = self ._client .project
36
33
37
34
if location is None :
38
35
location = self ._client .location
39
36
37
+ # for some reason these cannot find the function call
40
38
# if job_config is not None:
41
39
# self._client._verify_job_config_type(job_config, QueryJobConfig)
42
40
@@ -62,7 +60,7 @@ async def query_and_wait(
62
60
)
63
61
64
62
65
- async def async_query_and_wait (
63
+ async def async_query_and_wait (
66
64
client : "Client" ,
67
65
query : str ,
68
66
* ,
@@ -76,23 +74,24 @@ async def async_query_and_wait(
76
74
page_size : Optional [int ] = None ,
77
75
max_results : Optional [int ] = None ,
78
76
) -> table .RowIterator :
79
-
80
77
# Some API parameters aren't supported by the jobs.query API. In these
81
78
# cases, fallback to a jobs.insert call.
82
79
if not _job_helpers ._supported_by_jobs_query (job_config ):
83
80
return await async_wait_or_cancel (
84
- asyncio .to_thread (_job_helpers .query_jobs_insert ( # throw in a background thread
85
- client = client ,
86
- query = query ,
87
- job_id = None ,
88
- job_id_prefix = None ,
89
- job_config = job_config ,
90
- location = location ,
91
- project = project ,
92
- retry = retry ,
93
- timeout = api_timeout ,
94
- job_retry = job_retry ,
95
- )),
81
+ asyncio .to_thread (
82
+ _job_helpers .query_jobs_insert (
83
+ client = client ,
84
+ query = query ,
85
+ job_id = None ,
86
+ job_id_prefix = None ,
87
+ job_config = job_config ,
88
+ location = location ,
89
+ project = project ,
90
+ retry = retry ,
91
+ timeout = api_timeout ,
92
+ job_retry = job_retry ,
93
+ )
94
+ ),
96
95
api_timeout = api_timeout ,
97
96
wait_timeout = wait_timeout ,
98
97
retry = retry ,
@@ -113,21 +112,20 @@ async def async_query_and_wait(
113
112
if os .getenv ("QUERY_PREVIEW_ENABLED" , "" ).casefold () == "true" :
114
113
request_body ["jobCreationMode" ] = "JOB_CREATION_OPTIONAL"
115
114
116
-
117
115
request_body ["requestId" ] = _job_helpers .make_job_id ()
118
116
span_attributes = {"path" : path }
119
117
120
- # For easier testing, handle the retries ourselves.
121
118
if retry is not None :
122
- response = retry ( client ._call_api )( # ASYNCHRONOUS HTTP CALLS aiohttp (optional of google-auth)
123
- retry = None , # We're calling the retry decorator ourselves, async_retries
119
+ response = client ._call_api ( # ASYNCHRONOUS HTTP CALLS aiohttp (optional of google-auth), add back retry( )
120
+ retry = None , # We're calling the retry decorator ourselves, async_retries, need to implement after making HTTP calls async
124
121
span_name = "BigQuery.query" ,
125
122
span_attributes = span_attributes ,
126
123
method = "POST" ,
127
124
path = path ,
128
125
data = request_body ,
129
126
timeout = api_timeout ,
130
127
)
128
+
131
129
else :
132
130
response = client ._call_api (
133
131
retry = None ,
@@ -141,9 +139,7 @@ async def async_query_and_wait(
141
139
142
140
# Even if we run with JOB_CREATION_OPTIONAL, if there are more pages
143
141
# to fetch, there will be a job ID for jobs.getQueryResults.
144
- query_results = google .cloud .bigquery .query ._QueryResults .from_api_repr (
145
- await response
146
- )
142
+ query_results = google .cloud .bigquery .query ._QueryResults .from_api_repr (response )
147
143
page_token = query_results .page_token
148
144
more_pages = page_token is not None
149
145
@@ -161,7 +157,7 @@ async def async_query_and_wait(
161
157
max_results = max_results ,
162
158
)
163
159
164
- result = table .RowIterator ( # async of RowIterator? async version without all the pandas stuff
160
+ result = table .RowIterator ( # async of RowIterator? async version without all the pandas stuff
165
161
client = client ,
166
162
api_request = functools .partial (client ._call_api , retry , timeout = api_timeout ),
167
163
path = None ,
@@ -177,12 +173,12 @@ async def async_query_and_wait(
177
173
num_dml_affected_rows = query_results .num_dml_affected_rows ,
178
174
)
179
175
180
-
181
176
if job_retry is not None :
182
- return job_retry (result ) # AsyncRetries, new default objects, default_job_retry_async, default_retry_async
177
+ return job_retry (result )
183
178
else :
184
179
return result
185
180
181
+
186
182
async def async_wait_or_cancel (
187
183
job : job .QueryJob ,
188
184
api_timeout : Optional [float ],
@@ -192,17 +188,19 @@ async def async_wait_or_cancel(
192
188
max_results : Optional [int ],
193
189
) -> table .RowIterator :
194
190
try :
195
- return asyncio .to_thread (job .result ( # run in a background thread
196
- page_size = page_size ,
197
- max_results = max_results ,
198
- retry = retry ,
199
- timeout = wait_timeout ,
200
- ))
191
+ return asyncio .to_thread (
192
+ job .result ( # run in a background thread
193
+ page_size = page_size ,
194
+ max_results = max_results ,
195
+ retry = retry ,
196
+ timeout = wait_timeout ,
197
+ )
198
+ )
201
199
except Exception :
202
200
# Attempt to cancel the job since we can't return the results.
203
201
try :
204
202
job .cancel (retry = retry , timeout = api_timeout )
205
203
except Exception :
206
204
# Don't eat the original exception if cancel fails.
207
205
pass
208
- raise
206
+ raise
0 commit comments