Skip to content

Commit ef9945a

Browse files
fix: BigFrames respects bq default region (#16933)
1 parent e7efd90 commit ef9945a

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

packages/bigframes/bigframes/session/__init__.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

packages/bigframes/bigframes/session/bq_caching_executor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,9 @@ def _cache_most_complex_subtree(self, node: nodes.BigFrameNode) -> bool:
547547
max_complexity=QUERY_COMPLEXITY_LIMIT,
548548
cache=self.cache,
549549
# Heuristic: subtree_compleixty * (copies of subtree)^2
550-
heuristic=lambda complexity, count: math.log(complexity)
551-
+ 2 * math.log(count),
550+
heuristic=lambda complexity, count: (
551+
math.log(complexity) + 2 * math.log(count)
552+
),
552553
)
553554
if selection is None:
554555
# No good subtrees to cache, just return original tree

0 commit comments

Comments
 (0)