Skip to content

Commit 0ed87db

Browse files
authored
Merge branch 'main' into owl-bot-update-lock-8e3e7e18255c22d1489258d0374c901c01f9c4fd77a12088670cd73d580aa737
2 parents 038d709 + 60e40c1 commit 0ed87db

File tree

18 files changed

+216
-42
lines changed

18 files changed

+216
-42
lines changed

.github/sync-repo-settings.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ branchProtectionRules:
1616
- 'docs'
1717
- 'unit (3.7)'
1818
- 'unit (3.8)'
19-
- 'unit (3.11)'
2019
- 'unit (3.12)'
20+
- 'unit (3.13)'
2121
- 'cover'
2222
- 'Samples - Lint'
2323
- 'Samples - Python 3.8'
@@ -37,4 +37,4 @@ permissionRules:
3737
- team: python-samples-owners
3838
permission: push
3939
- team: python-samples-reviewers
40-
permission: push
40+
permission: push

.github/workflows/unittest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python: ['3.7', '3.8', '3.11', '3.12']
11+
python: ['3.7', '3.8', '3.11', '3.12', '3.13']
1212
steps:
1313
- name: Checkout
1414
uses: actions/checkout@v4
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Only run this nox session.
4+
env_vars: {
5+
key: "NOX_SESSION"
6+
value: "prerelease_deps-3.13"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Only run this nox session.
4+
env_vars: {
5+
key: "NOX_SESSION"
6+
value: "prerelease_deps-3.13"
7+
}

.kokoro/presubmit/system-3.13.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Only run this nox session.
4+
env_vars: {
5+
key: "NOX_SESSION"
6+
value: "system-3.13"
7+
}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [0.5.0](https://github.com/googleapis/python-bigquery-magics/compare/v0.4.0...v0.5.0) (2024-12-17)
4+
5+
6+
### Features
7+
8+
* Add `%%bqsql` as an alias to the `%%bigquery` magic ([#72](https://github.com/googleapis/python-bigquery-magics/issues/72)) ([03fe1d5](https://github.com/googleapis/python-bigquery-magics/commit/03fe1d544ef22865c07c680873f980c64bbc7abc))
9+
* Add `bigquery_magics.context.default_variable` option ([#70](https://github.com/googleapis/python-bigquery-magics/issues/70)) ([72ed882](https://github.com/googleapis/python-bigquery-magics/commit/72ed882c9359718a702bab2bca76933548650064))
10+
311
## [0.4.0](https://github.com/googleapis/python-bigquery-magics/compare/v0.3.0...v0.4.0) (2024-09-24)
412

513

CONTRIBUTING.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In order to add a feature:
2222
documentation.
2323

2424
- The feature must work fully on the following CPython versions:
25-
3.7, 3.8, 3.9, 3.11 and 3.12 on both UNIX and Windows.
25+
3.7, 3.8, 3.9, 3.11, 3.12 and 3.13 on both UNIX and Windows.
2626

2727
- The feature must not add unnecessary dependencies (where
2828
"unnecessary" is of course subjective, but new dependencies should
@@ -72,7 +72,7 @@ We use `nox <https://nox.readthedocs.io/en/latest/>`__ to instrument our tests.
7272

7373
- To run a single unit test::
7474

75-
$ nox -s unit-3.12 -- -k <name of test>
75+
$ nox -s unit-3.13 -- -k <name of test>
7676

7777

7878
.. note::
@@ -143,12 +143,12 @@ Running System Tests
143143
$ nox -s system
144144

145145
# Run a single system test
146-
$ nox -s system-3.12 -- -k <name of test>
146+
$ nox -s system-3.13 -- -k <name of test>
147147

148148

149149
.. note::
150150

151-
System tests are only configured to run under Python 3.8, 3.9, 3.11 and 3.12.
151+
System tests are only configured to run under Python 3.8, 3.11, 3.12 and 3.13.
152152
For expediency, we do not run them in older versions of Python 3.
153153

154154
This alone will not run the tests. You'll need to change some local
@@ -226,12 +226,14 @@ We support:
226226
- `Python 3.9`_
227227
- `Python 3.11`_
228228
- `Python 3.12`_
229+
- `Python 3.13`_
229230

230231
.. _Python 3.7: https://docs.python.org/3.7/
231232
.. _Python 3.8: https://docs.python.org/3.8/
232233
.. _Python 3.9: https://docs.python.org/3.9/
233234
.. _Python 3.11: https://docs.python.org/3.11/
234235
.. _Python 3.12: https://docs.python.org/3.12/
236+
.. _Python 3.13: https://docs.python.org/3.13/
235237

236238

237239
Supported versions can be found in our ``noxfile.py`` `config`_.

README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ Perform a query
101101
GROUP BY name
102102
ORDER BY count DESC
103103
LIMIT 3
104+
105+
Since BigQuery supports Python via BigQuery DataFrames, `%%bqsql` is offered as
106+
an alias to clarify the language of these cells.
107+
108+
.. code:: python
109+
110+
%%bqsql
111+
SELECT name, SUM(number) as count
112+
FROM 'bigquery-public-data.usa_names.usa_1910_current'
113+
GROUP BY name
114+
ORDER BY count DESC
115+
LIMIT 3

bigquery_magics/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def load_ipython_extension(ipython):
2727
ipython.register_magic_function(
2828
_cell_magic, magic_kind="cell", magic_name="bigquery"
2929
)
30+
ipython.register_magic_function(_cell_magic, magic_kind="cell", magic_name="bqsql")
3031

3132

3233
__all__ = (

bigquery_magics/bigquery.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
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
@@ -30,7 +30,8 @@
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.
@@ -73,11 +74,12 @@
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

8385
from __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

487486
def _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+
548569
def _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

634651
def _validate_and_resolve_query(query: str, args: Any) -> str:

0 commit comments

Comments
 (0)