Skip to content

Commit 80641f4

Browse files
authored
BigQuery: Add snippet to run a dry run query. (googleapis#5119)
* BigQuery: Add snippet to run a dry run query. * Fix region tags. Make sample more readable. * Add commented imports and client creation. * Move query text to query call. * Add print statement for query bytes processing.
1 parent 576cb65 commit 80641f4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docs/bigquery/snippets.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,35 @@ def test_client_query_w_param(client):
17601760
# [END client_query_w_param]
17611761

17621762

1763+
def test_client_query_dry_run(client):
1764+
"""Run a dry run query"""
1765+
1766+
# [START bigquery_query_dry_run]
1767+
# from google.cloud import bigquery
1768+
# client = bigquery.Client()
1769+
job_config = bigquery.QueryJobConfig()
1770+
job_config.dry_run = True
1771+
job_config.use_query_cache = False
1772+
query_job = client.query(
1773+
('SELECT name, COUNT(*) as name_count '
1774+
'FROM `bigquery-public-data.usa_names.usa_1910_2013` '
1775+
"WHERE state = 'WA' "
1776+
'GROUP BY name'),
1777+
# Location must match that of the dataset(s) referenced in the query.
1778+
location='US',
1779+
job_config=job_config) # API request
1780+
1781+
# A dry run query completes immediately.
1782+
assert query_job.state == 'DONE'
1783+
assert query_job.dry_run
1784+
1785+
print("This query will process {} bytes.".format(
1786+
query_job.total_bytes_processed))
1787+
# [END bigquery_query_dry_run]
1788+
1789+
assert query_job.total_bytes_processed > 0
1790+
1791+
17631792
def test_client_list_jobs(client):
17641793
"""List jobs for a project."""
17651794

docs/bigquery/usage.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ Querying data
346346
- Use of the ``timeout`` parameter is optional. The query will continue to
347347
run in the background even if it takes longer the timeout allowed.
348348

349+
Run a dry run query
350+
~~~~~~~~~~~~~~~~~~~
351+
352+
.. literalinclude:: snippets.py
353+
:start-after: [START bigquery_query_dry_run]
354+
:end-before: [END bigquery_query_dry_run]
349355

350356
Writing query results to a destination table
351357
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)