Skip to content

Commit 6559dde

Browse files
Gaurang033Linchin
andauthored
feature: add query location for bigquery magic (#1771)
Co-authored-by: Lingqing Gan <[email protected]>
1 parent 4ba4342 commit 6559dde

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

google/cloud/bigquery/magics/magics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ def _create_dataset_if_necessary(client, dataset_id):
508508
"Defaults to use tqdm_notebook. Install the ``tqdm`` package to use this feature."
509509
),
510510
)
511+
@magic_arguments.argument(
512+
"--location",
513+
type=str,
514+
default=None,
515+
help=(
516+
"Set the location to execute query."
517+
"Defaults to location set in query setting in console."
518+
),
519+
)
511520
def _cell_magic(line, query):
512521
"""Underlying function for bigquery cell magic
513522
@@ -551,6 +560,7 @@ def _cell_magic(line, query):
551560
category=DeprecationWarning,
552561
)
553562
use_bqstorage_api = not args.use_rest_api
563+
location = args.location
554564

555565
params = []
556566
if params_option_value:
@@ -579,6 +589,7 @@ def _cell_magic(line, query):
579589
default_query_job_config=context.default_query_job_config,
580590
client_info=client_info.ClientInfo(user_agent=IPYTHON_USER_AGENT),
581591
client_options=bigquery_client_options,
592+
location=location,
582593
)
583594
if context._connection:
584595
client._connection = context._connection

tests/unit/test_magics.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,3 +2053,21 @@ def test_bigquery_magic_create_dataset_fails():
20532053
)
20542054

20552055
assert close_transports.called
2056+
2057+
2058+
@pytest.mark.usefixtures("ipython_interactive")
2059+
def test_bigquery_magic_with_location():
2060+
ip = IPython.get_ipython()
2061+
ip.extension_manager.load_extension("google.cloud.bigquery")
2062+
magics.context.credentials = mock.create_autospec(
2063+
google.auth.credentials.Credentials, instance=True
2064+
)
2065+
2066+
run_query_patch = mock.patch(
2067+
"google.cloud.bigquery.magics.magics._run_query", autospec=True
2068+
)
2069+
with run_query_patch as run_query_mock:
2070+
ip.run_cell_magic("bigquery", "--location=us-east1", "SELECT 17 AS num")
2071+
2072+
client_options_used = run_query_mock.call_args_list[0][0][0]
2073+
assert client_options_used.location == "us-east1"

0 commit comments

Comments
 (0)