Skip to content

Commit f875673

Browse files
authored
BigQuery: Do not return results of query magic when assigned to variable (googleapis#7010)
1 parent eee06f0 commit f875673

2 files changed

Lines changed: 14 additions & 21 deletions

File tree

bigquery/google/cloud/bigquery/magics.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
Parameters:
2828
2929
* ``<destination_var>`` (optional, line argument):
30-
variable to store the query results.
30+
variable to store the query results. The results are not displayed if
31+
this parameter is used.
3132
* ``--project <project>`` (optional, line argument):
3233
Project to use for running the query. Defaults to the context
3334
:attr:`~google.cloud.bigquery.magics.Context.project`.
@@ -96,12 +97,6 @@
9697
Query executing: 2.61s
9798
Query complete after 2.92s
9899
99-
Out[3]: name count
100-
...: ----------------------
101-
...: 0 Mary 3736239
102-
...: 1 Patricia 1568495
103-
...: 2 Elizabeth 1519946
104-
105100
In [4]: df
106101
107102
Out[4]: name count
@@ -110,7 +105,7 @@
110105
...: 1 Patricia 1568495
111106
...: 2 Elizabeth 1519946
112107
113-
In [5]: %%bigquery df --params {"num": 17}
108+
In [5]: %%bigquery --params {"num": 17}
114109
...: SELECT @num AS num
115110
116111
Out[5]: num
@@ -119,7 +114,7 @@
119114
120115
In [6]: params = {"num": 17}
121116
122-
In [7]: %%bigquery df --params $params
117+
In [7]: %%bigquery --params $params
123118
...: SELECT @num AS num
124119
125120
Out[7]: num
@@ -262,17 +257,13 @@ def _run_query(client, query, job_config=None):
262257
@magic_arguments.argument(
263258
"destination_var",
264259
nargs="?",
265-
help=(
266-
"If provided, save the output to this variable in addition " "to displaying it."
267-
),
260+
help=("If provided, save the output to this variable instead of displaying it."),
268261
)
269262
@magic_arguments.argument(
270263
"--project",
271264
type=str,
272265
default=None,
273-
help=(
274-
"Project to use for executing this query. Defaults to the context " "project."
275-
),
266+
help=("Project to use for executing this query. Defaults to the context project."),
276267
)
277268
@magic_arguments.argument(
278269
"--use_legacy_sql",
@@ -348,4 +339,5 @@ def _cell_magic(line, query):
348339
result = query_job.to_dataframe()
349340
if args.destination_var:
350341
IPython.get_ipython().push({args.destination_var: result})
351-
return result
342+
else:
343+
return result

bigquery/tests/unit/test_magics.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def test_bigquery_magic_without_optional_arguments():
160160
with run_query_patch as run_query_mock:
161161
run_query_mock.return_value = query_job_mock
162162

163-
result = ip.run_cell_magic("bigquery", "", sql)
163+
return_value = ip.run_cell_magic("bigquery", "", sql)
164164

165-
assert isinstance(result, pandas.DataFrame)
166-
assert len(result) == len(result) # verify row count
167-
assert list(result) == list(result) # verify column names
165+
assert isinstance(return_value, pandas.DataFrame)
166+
assert len(return_value) == len(result) # verify row count
167+
assert list(return_value) == list(result) # verify column names
168168

169169

170170
@pytest.mark.usefixtures("ipython_interactive")
@@ -208,8 +208,9 @@ def test_bigquery_magic_with_result_saved_to_variable():
208208
with run_query_patch as run_query_mock:
209209
run_query_mock.return_value = query_job_mock
210210

211-
ip.run_cell_magic("bigquery", "df", sql)
211+
return_value = ip.run_cell_magic("bigquery", "df", sql)
212212

213+
assert return_value is None
213214
assert "df" in ip.user_ns # verify that variable exists
214215
df = ip.user_ns["df"]
215216
assert len(df) == len(result) # verify row count

0 commit comments

Comments
 (0)