Skip to content

Commit 562deea

Browse files
plamuttswast
authored andcommitted
Add bqstorage_client param to QueryJob.to_arrow() (#8693)
* Add method signature compatibility reminders * Add bqstorage_client param to QueryJob.to_arrow() * Add test for to_*() method signature compatibility The method signatures for to_arrow() and to_dataframe() methods in the job.QueryJob and table.RowIterator classes must match to present a consistent API for users. * Skip method signature test in old Pythons inspect.signature() method is only available in older Python versions
1 parent f7faf20 commit 562deea

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

bigquery/google/cloud/bigquery/job.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,7 +2896,9 @@ def result(self, timeout=None, page_size=None, retry=DEFAULT_RETRY):
28962896
rows._preserve_order = _contains_order_by(self.query)
28972897
return rows
28982898

2899-
def to_arrow(self, progress_bar_type=None):
2899+
# If changing the signature of this method, make sure to apply the same
2900+
# changes to table.RowIterator.to_arrow()
2901+
def to_arrow(self, progress_bar_type=None, bqstorage_client=None):
29002902
"""[Beta] Create a class:`pyarrow.Table` by loading all pages of a
29012903
table or query.
29022904
@@ -2919,6 +2921,18 @@ def to_arrow(self, progress_bar_type=None):
29192921
``'tqdm_gui'``
29202922
Use the :func:`tqdm.tqdm_gui` function to display a
29212923
progress bar as a graphical dialog box.
2924+
bqstorage_client ( \
2925+
google.cloud.bigquery_storage_v1beta1.BigQueryStorageClient \
2926+
):
2927+
**Beta Feature** Optional. A BigQuery Storage API client. If
2928+
supplied, use the faster BigQuery Storage API to fetch rows
2929+
from BigQuery. This API is a billable API.
2930+
2931+
This method requires the ``pyarrow`` and
2932+
``google-cloud-bigquery-storage`` libraries.
2933+
2934+
Reading from a specific partition or snapshot is not
2935+
currently supported by this method.
29222936
29232937
Returns:
29242938
pyarrow.Table
@@ -2932,8 +2946,12 @@ def to_arrow(self, progress_bar_type=None):
29322946
29332947
..versionadded:: 1.17.0
29342948
"""
2935-
return self.result().to_arrow(progress_bar_type=progress_bar_type)
2949+
return self.result().to_arrow(
2950+
progress_bar_type=progress_bar_type, bqstorage_client=bqstorage_client
2951+
)
29362952

2953+
# If changing the signature of this method, make sure to apply the same
2954+
# changes to table.RowIterator.to_dataframe()
29372955
def to_dataframe(self, bqstorage_client=None, dtypes=None, progress_bar_type=None):
29382956
"""Return a pandas DataFrame from a QueryJob
29392957

bigquery/google/cloud/bigquery/table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,8 @@ def _to_arrow_iterable(self, bqstorage_client=None):
14491449
bqstorage_client=bqstorage_client,
14501450
)
14511451

1452+
# If changing the signature of this method, make sure to apply the same
1453+
# changes to job.QueryJob.to_arrow()
14521454
def to_arrow(self, progress_bar_type=None, bqstorage_client=None):
14531455
"""[Beta] Create a class:`pyarrow.Table` by loading all pages of a
14541456
table or query.
@@ -1552,6 +1554,8 @@ def _to_dataframe_iterable(self, bqstorage_client=None, dtypes=None):
15521554
bqstorage_client=bqstorage_client,
15531555
)
15541556

1557+
# If changing the signature of this method, make sure to apply the same
1558+
# changes to job.QueryJob.to_dataframe()
15551559
def to_dataframe(self, bqstorage_client=None, dtypes=None, progress_bar_type=None):
15561560
"""Create a pandas DataFrame by loading all pages of a query.
15571561
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import inspect
16+
17+
import pytest
18+
19+
20+
@pytest.fixture
21+
def query_job_class():
22+
from google.cloud.bigquery.job import QueryJob
23+
24+
return QueryJob
25+
26+
27+
@pytest.fixture
28+
def row_iterator_class():
29+
from google.cloud.bigquery.table import RowIterator
30+
31+
return RowIterator
32+
33+
34+
@pytest.mark.skipif(
35+
not hasattr(inspect, "signature"),
36+
reason="inspect.signature() is not availalbe in older Python versions",
37+
)
38+
def test_to_arrow_method_signatures_match(query_job_class, row_iterator_class):
39+
sig = inspect.signature(query_job_class.to_arrow)
40+
sig2 = inspect.signature(row_iterator_class.to_arrow)
41+
assert sig == sig2
42+
43+
44+
@pytest.mark.skipif(
45+
not hasattr(inspect, "signature"),
46+
reason="inspect.signature() is not availalbe in older Python versions",
47+
)
48+
def test_to_dataframe_method_signatures_match(query_job_class, row_iterator_class):
49+
sig = inspect.signature(query_job_class.to_dataframe)
50+
sig2 = inspect.signature(row_iterator_class.to_dataframe)
51+
assert sig == sig2

0 commit comments

Comments
 (0)