Skip to content

Commit 7dd30af

Browse files
authored
feat: add --no_query_cache option to %%bigquery magics to disable query cache (#1141)
1 parent f5daa9b commit 7dd30af

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

google/cloud/bigquery/magics/magics.py

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
A dataset and table to store the query results. If table does not exists,
3636
it will be created. If table already exists, its data will be overwritten.
3737
Variable should be in a format <dataset_id>.<table_id>.
38+
* ``--no_query_cache`` (Optional[line argument]):
39+
Do not use cached query results.
3840
* ``--project <project>`` (Optional[line argument]):
3941
Project to use for running the query. Defaults to the context
4042
:attr:`~google.cloud.bigquery.magics.Context.project`.
@@ -442,6 +444,12 @@ def _create_dataset_if_necessary(client, dataset_id):
442444
"this option's value in the context bqstorage_client_options."
443445
),
444446
)
447+
@magic_arguments.argument(
448+
"--no_query_cache",
449+
action="store_true",
450+
default=False,
451+
help=("Do not use cached query results."),
452+
)
445453
@magic_arguments.argument(
446454
"--use_bqstorage_api",
447455
action="store_true",
@@ -642,6 +650,10 @@ def _cell_magic(line, query):
642650
job_config.use_legacy_sql = args.use_legacy_sql
643651
job_config.dry_run = args.dry_run
644652

653+
# Don't override context job config unless --no_query_cache is explicitly set.
654+
if args.no_query_cache:
655+
job_config.use_query_cache = False
656+
645657
if args.destination_table:
646658
split = args.destination_table.split(".")
647659
if len(split) != 2:

tests/unit/test_magics.py

+58
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,64 @@ def test_bigquery_magic_w_maximum_bytes_billed_w_context_setter():
12171217
assert sent_config["maximumBytesBilled"] == "10203"
12181218

12191219

1220+
@pytest.mark.usefixtures("ipython_interactive")
1221+
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
1222+
def test_bigquery_magic_with_no_query_cache(monkeypatch):
1223+
ip = IPython.get_ipython()
1224+
ip.extension_manager.load_extension("google.cloud.bigquery")
1225+
conn = make_connection()
1226+
monkeypatch.setattr(magics.context, "_connection", conn)
1227+
monkeypatch.setattr(magics.context, "project", "project-from-context")
1228+
1229+
# --no_query_cache option should override context.
1230+
monkeypatch.setattr(
1231+
magics.context.default_query_job_config, "use_query_cache", True
1232+
)
1233+
1234+
ip.run_cell_magic("bigquery", "--no_query_cache", QUERY_STRING)
1235+
1236+
conn.api_request.assert_called_with(
1237+
method="POST",
1238+
path="/projects/project-from-context/jobs",
1239+
data=mock.ANY,
1240+
timeout=DEFAULT_TIMEOUT,
1241+
)
1242+
jobs_insert_call = [
1243+
call
1244+
for call in conn.api_request.call_args_list
1245+
if call[1]["path"] == "/projects/project-from-context/jobs"
1246+
][0]
1247+
assert not jobs_insert_call[1]["data"]["configuration"]["query"]["useQueryCache"]
1248+
1249+
1250+
@pytest.mark.usefixtures("ipython_interactive")
1251+
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
1252+
def test_context_with_no_query_cache_from_context(monkeypatch):
1253+
ip = IPython.get_ipython()
1254+
ip.extension_manager.load_extension("google.cloud.bigquery")
1255+
conn = make_connection()
1256+
monkeypatch.setattr(magics.context, "_connection", conn)
1257+
monkeypatch.setattr(magics.context, "project", "project-from-context")
1258+
monkeypatch.setattr(
1259+
magics.context.default_query_job_config, "use_query_cache", False
1260+
)
1261+
1262+
ip.run_cell_magic("bigquery", "", QUERY_STRING)
1263+
1264+
conn.api_request.assert_called_with(
1265+
method="POST",
1266+
path="/projects/project-from-context/jobs",
1267+
data=mock.ANY,
1268+
timeout=DEFAULT_TIMEOUT,
1269+
)
1270+
jobs_insert_call = [
1271+
call
1272+
for call in conn.api_request.call_args_list
1273+
if call[1]["path"] == "/projects/project-from-context/jobs"
1274+
][0]
1275+
assert not jobs_insert_call[1]["data"]["configuration"]["query"]["useQueryCache"]
1276+
1277+
12201278
@pytest.mark.usefixtures("ipython_interactive")
12211279
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
12221280
def test_bigquery_magic_w_progress_bar_type_w_context_setter(monkeypatch):

0 commit comments

Comments
 (0)