Skip to content

Commit

Permalink
feature: add query location for bigquery magic (#1771)
Browse files Browse the repository at this point in the history
Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
  • Loading branch information
Gaurang033 and Linchin authored Jan 24, 2024
1 parent 4ba4342 commit 6559dde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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"

0 comments on commit 6559dde

Please sign in to comment.