61
61
ReadPickleBuffer ,
62
62
StorageOptions ,
63
63
)
64
- import pydata_google_auth
65
64
66
65
import bigframes ._config .bigquery_options as bigquery_options
67
66
import bigframes .constants as constants
75
74
import bigframes .formatting_helpers as formatting_helpers
76
75
from bigframes .remote_function import read_gbq_function as bigframes_rgf
77
76
from bigframes .remote_function import remote_function as bigframes_rf
77
+ import bigframes .session .clients
78
78
import bigframes .version
79
79
80
80
# Even though the ibis.backends.bigquery.registry import is unused, it's needed
85
85
import third_party .bigframes_vendored .pandas .io .parsers .readers as third_party_pandas_readers
86
86
import third_party .bigframes_vendored .pandas .io .pickle as third_party_pandas_pickle
87
87
88
- _ENV_DEFAULT_PROJECT = "GOOGLE_CLOUD_PROJECT"
89
- _APPLICATION_NAME = f"bigframes/{ bigframes .version .__version__ } "
90
- _SCOPES = ["https://www.googleapis.com/auth/cloud-platform" ]
91
-
92
- # BigQuery is a REST API, which requires the protocol as part of the URL.
93
- _BIGQUERY_REGIONAL_ENDPOINT = "https://{location}-bigquery.googleapis.com"
94
-
95
- # BigQuery Connection and Storage are gRPC APIs, which don't support the
96
- # https:// protocol in the API endpoint URL.
97
- _BIGQUERYCONNECTION_REGIONAL_ENDPOINT = "{location}-bigqueryconnection.googleapis.com"
98
- _BIGQUERYSTORAGE_REGIONAL_ENDPOINT = "{location}-bigquerystorage.googleapis.com"
99
-
100
88
_BIGFRAMES_DEFAULT_CONNECTION_ID = "bigframes-default-connection"
101
89
102
90
_MAX_CLUSTER_COLUMNS = 4
@@ -122,149 +110,6 @@ def _is_query(query_or_table: str) -> bool:
122
110
return re .search (r"\s" , query_or_table .strip (), re .MULTILINE ) is not None
123
111
124
112
125
- def _get_default_credentials_with_project ():
126
- return pydata_google_auth .default (scopes = _SCOPES , use_local_webserver = False )
127
-
128
-
129
- class ClientsProvider :
130
- """Provides client instances necessary to perform cloud operations."""
131
-
132
- def __init__ (
133
- self ,
134
- project : Optional [str ],
135
- location : Optional [str ],
136
- use_regional_endpoints : Optional [bool ],
137
- credentials : Optional [google .auth .credentials .Credentials ],
138
- ):
139
- credentials_project = None
140
- if credentials is None :
141
- credentials , credentials_project = _get_default_credentials_with_project ()
142
-
143
- # Prefer the project in this order:
144
- # 1. Project explicitly specified by the user
145
- # 2. Project set in the environment
146
- # 3. Project associated with the default credentials
147
- project = (
148
- project
149
- or os .getenv (_ENV_DEFAULT_PROJECT )
150
- or typing .cast (Optional [str ], credentials_project )
151
- )
152
-
153
- if not project :
154
- raise ValueError (
155
- "Project must be set to initialize BigQuery client. "
156
- "Try setting `bigframes.options.bigquery.project` first."
157
- )
158
-
159
- self ._project = project
160
- self ._location = location
161
- self ._use_regional_endpoints = use_regional_endpoints
162
- self ._credentials = credentials
163
-
164
- # cloud clients initialized for lazy load
165
- self ._bqclient = None
166
- self ._bqconnectionclient = None
167
- self ._bqstorageclient = None
168
- self ._cloudfunctionsclient = None
169
- self ._resourcemanagerclient = None
170
-
171
- @property
172
- def bqclient (self ):
173
- if not self ._bqclient :
174
- bq_options = None
175
- if self ._use_regional_endpoints :
176
- bq_options = google .api_core .client_options .ClientOptions (
177
- api_endpoint = _BIGQUERY_REGIONAL_ENDPOINT .format (
178
- location = self ._location
179
- ),
180
- )
181
- bq_info = google .api_core .client_info .ClientInfo (
182
- user_agent = _APPLICATION_NAME
183
- )
184
- self ._bqclient = bigquery .Client (
185
- client_info = bq_info ,
186
- client_options = bq_options ,
187
- credentials = self ._credentials ,
188
- project = self ._project ,
189
- location = self ._location ,
190
- )
191
-
192
- return self ._bqclient
193
-
194
- @property
195
- def bqconnectionclient (self ):
196
- if not self ._bqconnectionclient :
197
- bqconnection_options = None
198
- if self ._use_regional_endpoints :
199
- bqconnection_options = google .api_core .client_options .ClientOptions (
200
- api_endpoint = _BIGQUERYCONNECTION_REGIONAL_ENDPOINT .format (
201
- location = self ._location
202
- )
203
- )
204
- bqconnection_info = google .api_core .gapic_v1 .client_info .ClientInfo (
205
- user_agent = _APPLICATION_NAME
206
- )
207
- self ._bqconnectionclient = (
208
- google .cloud .bigquery_connection_v1 .ConnectionServiceClient (
209
- client_info = bqconnection_info ,
210
- client_options = bqconnection_options ,
211
- credentials = self ._credentials ,
212
- )
213
- )
214
-
215
- return self ._bqconnectionclient
216
-
217
- @property
218
- def bqstorageclient (self ):
219
- if not self ._bqstorageclient :
220
- bqstorage_options = None
221
- if self ._use_regional_endpoints :
222
- bqstorage_options = google .api_core .client_options .ClientOptions (
223
- api_endpoint = _BIGQUERYSTORAGE_REGIONAL_ENDPOINT .format (
224
- location = self ._location
225
- )
226
- )
227
- bqstorage_info = google .api_core .gapic_v1 .client_info .ClientInfo (
228
- user_agent = _APPLICATION_NAME
229
- )
230
- self ._bqstorageclient = google .cloud .bigquery_storage_v1 .BigQueryReadClient (
231
- client_info = bqstorage_info ,
232
- client_options = bqstorage_options ,
233
- credentials = self ._credentials ,
234
- )
235
-
236
- return self ._bqstorageclient
237
-
238
- @property
239
- def cloudfunctionsclient (self ):
240
- if not self ._cloudfunctionsclient :
241
- functions_info = google .api_core .gapic_v1 .client_info .ClientInfo (
242
- user_agent = _APPLICATION_NAME
243
- )
244
- self ._cloudfunctionsclient = (
245
- google .cloud .functions_v2 .FunctionServiceClient (
246
- client_info = functions_info ,
247
- credentials = self ._credentials ,
248
- )
249
- )
250
-
251
- return self ._cloudfunctionsclient
252
-
253
- @property
254
- def resourcemanagerclient (self ):
255
- if not self ._resourcemanagerclient :
256
- resourcemanager_info = google .api_core .gapic_v1 .client_info .ClientInfo (
257
- user_agent = _APPLICATION_NAME
258
- )
259
- self ._resourcemanagerclient = (
260
- google .cloud .resourcemanager_v3 .ProjectsClient (
261
- credentials = self ._credentials , client_info = resourcemanager_info
262
- )
263
- )
264
-
265
- return self ._resourcemanagerclient
266
-
267
-
268
113
class Session (
269
114
third_party_pandas_gbq .GBQIOMixin ,
270
115
third_party_pandas_parquet .ParquetIOMixin ,
@@ -279,14 +124,14 @@ class Session(
279
124
Configuration adjusting how to connect to BigQuery and related
280
125
APIs. Note that some options are ignored if ``clients_provider`` is
281
126
set.
282
- clients_provider (bigframes.session.ClientsProvider):
127
+ clients_provider (bigframes.session.bigframes.session.clients. ClientsProvider):
283
128
An object providing client library objects.
284
129
"""
285
130
286
131
def __init__ (
287
132
self ,
288
133
context : Optional [bigquery_options .BigQueryOptions ] = None ,
289
- clients_provider : Optional [ClientsProvider ] = None ,
134
+ clients_provider : Optional [bigframes . session . clients . ClientsProvider ] = None ,
290
135
):
291
136
if context is None :
292
137
context = bigquery_options .BigQueryOptions ()
@@ -306,11 +151,12 @@ def __init__(
306
151
if clients_provider :
307
152
self ._clients_provider = clients_provider
308
153
else :
309
- self ._clients_provider = ClientsProvider (
154
+ self ._clients_provider = bigframes . session . clients . ClientsProvider (
310
155
project = context .project ,
311
156
location = self ._location ,
312
157
use_regional_endpoints = context .use_regional_endpoints ,
313
158
credentials = context .credentials ,
159
+ application_name = context .application_name ,
314
160
)
315
161
316
162
self ._create_and_bind_bq_session ()
@@ -319,7 +165,7 @@ def __init__(
319
165
ibis .bigquery .connect (
320
166
project_id = context .project ,
321
167
client = self .bqclient ,
322
- storage_client = self .bqstorageclient ,
168
+ storage_client = self .bqstoragereadclient ,
323
169
),
324
170
)
325
171
@@ -338,8 +184,8 @@ def bqconnectionclient(self):
338
184
return self ._clients_provider .bqconnectionclient
339
185
340
186
@property
341
- def bqstorageclient (self ):
342
- return self ._clients_provider .bqstorageclient
187
+ def bqstoragereadclient (self ):
188
+ return self ._clients_provider .bqstoragereadclient
343
189
344
190
@property
345
191
def cloudfunctionsclient (self ):
0 commit comments