@@ -188,39 +188,50 @@ def __init__(
188188 if context is None :
189189 context = bigquery_options .BigQueryOptions ()
190190
191- if context .location is None :
192- self ._location = "US"
193- msg = bfe .format_message (
194- f"No explicit location is set, so using location { self ._location } for the session."
195- )
196- # User's code
197- # -> get_global_session()
198- # -> connect()
199- # -> Session()
200- #
201- # Note: We could also have:
202- # User's code
203- # -> read_gbq()
204- # -> with_default_session()
205- # -> get_global_session()
206- # -> connect()
207- # -> Session()
208- # but we currently have no way to disambiguate these
209- # situations.
210- warnings .warn (msg , stacklevel = 4 , category = bfe .DefaultLocationWarning )
211- else :
212- self ._location = context .location
213-
214191 self ._bq_kms_key_name = context .kms_key_name
215192
216193 # Instantiate a clients provider to help with cloud clients that will be
217194 # used in the future operations in the session
218195 if clients_provider :
196+ # this path is only for unit testing. Not meant to be used by end users.
219197 self ._clients_provider = clients_provider
198+ self ._location = context .location or "US"
220199 else :
221200 credentials , project = (
222201 bigframes ._config .auth .resolve_credentials_and_project (context )
223202 )
203+ if context .location is None :
204+ with bigquery .Client (
205+ project = project ,
206+ credentials = credentials ,
207+ ) as temp_client :
208+ row_iter = temp_client .query_and_wait (
209+ "SELECT 1" ,
210+ job_config = bigquery .QueryJobConfig (dry_run = True ),
211+ )
212+ self ._location = row_iter .location or "US"
213+ msg = bfe .format_message (
214+ f"No explicit location is set, so using location { self ._location } for the session."
215+ )
216+ # User's code
217+ # -> get_global_session()
218+ # -> connect()
219+ # -> Session()
220+ #
221+ # Note: We could also have:
222+ # User's code
223+ # -> read_gbq()
224+ # -> with_default_session()
225+ # -> get_global_session()
226+ # -> connect()
227+ # -> Session()
228+ # but we currently have no way to disambiguate these
229+ # situations.
230+ warnings .warn (
231+ msg , stacklevel = 4 , category = bfe .DefaultLocationWarning
232+ )
233+ else :
234+ self ._location = context .location
224235
225236 self ._clients_provider = clients .ClientsProvider (
226237 project = project ,
0 commit comments