Skip to content

Commit d268a62

Browse files
feat: add dry run to the read_gbq function
1 parent 23fbebb commit d268a62

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

pandas_gbq/gbq.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def read_gbq(
119119
*,
120120
col_order=None,
121121
bigquery_client=None,
122+
dry_run: bool = False,
122123
):
123124
r"""Read data from Google BigQuery to a pandas DataFrame.
124125
@@ -269,7 +270,8 @@ def read_gbq(
269270
bigquery_client : google.cloud.bigquery.Client, optional
270271
A Google Cloud BigQuery Python Client instance. If provided, it will be used for reading
271272
data, while the project and credentials parameters will be ignored.
272-
273+
dry_run : bool, default False
274+
If True, run a dry run query.
273275
Returns
274276
-------
275277
df: DataFrame
@@ -328,6 +330,7 @@ def read_gbq(
328330
max_results=max_results,
329331
progress_bar_type=progress_bar_type,
330332
dtypes=dtypes,
333+
dry_run=dry_run,
331334
)
332335
else:
333336
final_df = connector.download_table(

pandas_gbq/gbq_connector.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ def download_table(
199199
user_dtypes=dtypes,
200200
)
201201

202-
def run_query(self, query, max_results=None, progress_bar_type=None, **kwargs):
202+
def run_query(
203+
self,
204+
query,
205+
max_results=None,
206+
progress_bar_type=None,
207+
dry_run: bool = False,
208+
**kwargs,
209+
):
203210
from google.cloud import bigquery
204211

205212
job_config_dict = {
@@ -235,6 +242,7 @@ def run_query(self, query, max_results=None, progress_bar_type=None, **kwargs):
235242

236243
self._start_timer()
237244
job_config = bigquery.QueryJobConfig.from_api_repr(job_config_dict)
245+
job_config.dry_run = dry_run
238246

239247
if FEATURES.bigquery_has_query_and_wait:
240248
rows_iter = pandas_gbq.query.query_and_wait_via_client_library(
@@ -260,6 +268,10 @@ def run_query(self, query, max_results=None, progress_bar_type=None, **kwargs):
260268
)
261269

262270
dtypes = kwargs.get("dtypes")
271+
272+
if dry_run:
273+
return rows_iter
274+
263275
return self._download_results(
264276
rows_iter,
265277
max_results=max_results,

tests/system/test_gbq.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,19 @@ def test_columns_and_col_order_raises_error(self, project_id):
656656
dialect="standard",
657657
)
658658

659+
def test_read_gbq_with_dry_run(self, project_id):
660+
query = "SELECT 1"
661+
job = gbq.read_gbq(
662+
query,
663+
project_id=project_id,
664+
credentials=self.credentials,
665+
dialect="standard",
666+
dry_run=True,
667+
)
668+
assert job.dry_run
669+
assert job.state == "DONE"
670+
assert job.total_bytes_processed > 0
671+
659672

660673
class TestToGBQIntegration(object):
661674
@pytest.fixture(autouse=True, scope="function")

tests/unit/test_gbq.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,10 @@ def test_run_query_with_dml_query(mock_bigquery_client, mock_query_job):
937937
type(mock_query_job).destination = mock.PropertyMock(return_value=None)
938938
connector.run_query("UPDATE tablename SET value = '';")
939939
mock_bigquery_client.list_rows.assert_not_called()
940+
941+
942+
def test_read_gbq_with_dry_run(mock_bigquery_client):
943+
gbq.read_gbq("SELECT 1", project_id="my-project", dry_run=True)
944+
_, kwargs = mock_bigquery_client.query.call_args
945+
job_config = kwargs["job_config"]
946+
assert job_config.dry_run is True

0 commit comments

Comments
 (0)