1414
1515"""IPython Magics
1616
17- .. function:: %%bigquery
17+ .. function:: `` %%bigquery`` or ``%%bqsql``
1818
1919 IPython cell magic to run a query and display the result as a DataFrame
2020
2121 .. code-block:: python
2222
23- %%bigquery [<destination_var>] [--project <project>] [--use_legacy_sql]
23+ %%bqsql [<destination_var>] [--project <project>] [--use_legacy_sql]
2424 [--verbose] [--params <params>]
2525 <query>
2626
3030 variable to store the query results. The results are not displayed if
3131 this parameter is used. If an error occurs during the query execution,
3232 the corresponding ``QueryJob`` instance (if available) is stored in
33- the variable instead.
33+ the variable instead. Set ``bigquery_magics.context.default_variable``
34+ to set a destination variable without specifying this argument.
3435 * ``--destination_table`` (Optional[line argument]):
3536 A dataset and table to store the query results. If table does not exists,
3637 it will be created. If table already exists, its data will be overwritten.
7374 fully-qualified table ID, and the latter's data will be fetched.
7475
7576 Returns:
76- A :class:`pandas.DataFrame` with the query results.
77+ A :class:`pandas.DataFrame` or :class:`bigframes.pandas.DataFrame`
78+ with the query results, depending on the ``engine`` chosen.
7779
7880 .. note::
7981 All queries run using this magic will run using the context
80- :attr:`~google.cloud.bigquery.magics .Context.credentials`.
82+ :attr:`~bigquery_magics.config .Context.credentials`.
8183"""
8284
8385from __future__ import print_function
@@ -478,10 +480,7 @@ def _query_with_bigframes(query: str, params: List[Any], args: Any):
478480 configuration = _create_job_config (args , params ).to_api_repr (),
479481 )
480482
481- if args .destination_var :
482- get_ipython ().push ({args .destination_var : result })
483- else :
484- return result
483+ return _handle_result (result , args )
485484
486485
487486def _query_with_pandas (query : str , params : List [Any ], args : Any ):
@@ -545,6 +544,28 @@ def _create_clients(args: Any) -> Tuple[bigquery.Client, Any]:
545544 return bq_client , bqstorage_client
546545
547546
547+ def _handle_result (result , args ):
548+ """Determine the output of the cell, depending on options set.
549+
550+ If an explicit destination is set, that takes precedence. Write to that
551+ variable and skip showing any results.
552+
553+ Otherwise, if there is a default variable set (such as if this module is
554+ initialized by bigframes), then set that but also show the output.
555+
556+ Finally, there is no variable to save to, so just show the output.
557+ """
558+ if args .destination_var :
559+ get_ipython ().push ({args .destination_var : result })
560+ return None
561+
562+ if context .default_variable :
563+ # If a default variable is set, save the result _and_ show the results.
564+ get_ipython ().push ({context .default_variable : result })
565+
566+ return result
567+
568+
548569def _make_bq_query (
549570 query : str ,
550571 args : Any ,
@@ -567,11 +588,7 @@ def _make_bq_query(
567588 bqstorage_client = bqstorage_client ,
568589 create_bqstorage_client = False ,
569590 )
570- if args .destination_var :
571- get_ipython ().push ({args .destination_var : result })
572- return
573- else :
574- return result
591+ return _handle_result (result , args )
575592
576593 job_config = _create_job_config (args , params )
577594 if args .destination_table :
@@ -599,6 +616,9 @@ def _make_bq_query(
599616 display .clear_output ()
600617
601618 if args .dry_run :
619+ # TODO(tswast): Use _handle_result() here, too, but perhaps change the
620+ # format to match the dry run schema from bigframes and pandas-gbq.
621+ # See: https://github.com/googleapis/python-bigquery-pandas/issues/585
602622 if args .destination_var :
603623 get_ipython ().push ({args .destination_var : query_job })
604624 return
@@ -625,10 +645,7 @@ def _make_bq_query(
625645 progress_bar_type = progress_bar ,
626646 )
627647
628- if args .destination_var :
629- get_ipython ().push ({args .destination_var : result })
630- else :
631- return result
648+ return _handle_result (result , args )
632649
633650
634651def _validate_and_resolve_query (query : str , args : Any ) -> str :
0 commit comments