Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions google/cloud/bigquery/magics/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ def _create_dataset_if_necessary(client, dataset_id):
"Defaults to use tqdm_notebook. Install the ``tqdm`` package to use this feature."
),
)
@magic_arguments.argument(
"--location",
type=str,
default=None,
help=(
"Set the location to execute query."
"Defaults to location set in query setting in console."
),
)
def _cell_magic(line, query):
"""Underlying function for bigquery cell magic

Expand Down Expand Up @@ -551,6 +560,7 @@ def _cell_magic(line, query):
category=DeprecationWarning,
)
use_bqstorage_api = not args.use_rest_api
location = args.location

params = []
if params_option_value:
Expand Down Expand Up @@ -579,6 +589,7 @@ def _cell_magic(line, query):
default_query_job_config=context.default_query_job_config,
client_info=client_info.ClientInfo(user_agent=IPYTHON_USER_AGENT),
client_options=bigquery_client_options,
location=location,
)
if context._connection:
client._connection = context._connection
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2053,3 +2053,21 @@ def test_bigquery_magic_create_dataset_fails():
)

assert close_transports.called


@pytest.mark.usefixtures("ipython_interactive")
def test_bigquery_magic_with_location():
ip = IPython.get_ipython()
ip.extension_manager.load_extension("google.cloud.bigquery")
magics.context.credentials = mock.create_autospec(
google.auth.credentials.Credentials, instance=True
)

run_query_patch = mock.patch(
"google.cloud.bigquery.magics.magics._run_query", autospec=True
)
with run_query_patch as run_query_mock:
ip.run_cell_magic("bigquery", "--location=us-east1", "SELECT 17 AS num")

client_options_used = run_query_mock.call_args_list[0][0][0]
assert client_options_used.location == "us-east1"