Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbfdeb3f

Browse files
authored
feat: add prefer_bqstorage_client option for Connection (#1945)
1 parentcc7b399 commitbfdeb3f

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

‎google/cloud/bigquery/dbapi/connection.py‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ class Connection(object):
3535
A client that uses the faster BigQuery Storage API to fetch rows from
3636
BigQuery. If not passed, it is created using the same credentials
3737
as ``client`` (provided that BigQuery Storage dependencies are installed).
38-
39-
If both clients are available, ``bqstorage_client`` is used for
40-
fetching query results.
38+
prefer_bqstorage_client (Optional[bool]):
39+
Prefer the BigQuery Storage client over the REST client. If Storage
40+
client isn't available, fall back to the REST client. Defaults to
41+
``True``.
4142
"""
4243

43-
def__init__(self,client=None,bqstorage_client=None):
44+
def__init__(
45+
self,
46+
client=None,
47+
bqstorage_client=None,
48+
prefer_bqstorage_client=True,
49+
):
4450
ifclientisNone:
4551
client=bigquery.Client()
4652
self._owns_client=True
@@ -49,7 +55,10 @@ def __init__(self, client=None, bqstorage_client=None):
4955

5056
# A warning is already raised by the BQ Storage client factory factory if
5157
# instantiation fails, or if the given BQ Storage client instance is outdated.
52-
ifbqstorage_clientisNone:
58+
ifnotprefer_bqstorage_client:
59+
bqstorage_client=None
60+
self._owns_bqstorage_client=False
61+
elifbqstorage_clientisNone:
5362
bqstorage_client=client._ensure_bqstorage_client()
5463
self._owns_bqstorage_client=bqstorage_clientisnotNone
5564
else:
@@ -95,7 +104,7 @@ def cursor(self):
95104
returnnew_cursor
96105

97106

98-
defconnect(client=None,bqstorage_client=None):
107+
defconnect(client=None,bqstorage_client=None,prefer_bqstorage_client=True):
99108
"""Construct a DB-API connection to Google BigQuery.
100109
101110
Args:
@@ -108,11 +117,12 @@ def connect(client=None, bqstorage_client=None):
108117
A client that uses the faster BigQuery Storage API to fetch rows from
109118
BigQuery. If not passed, it is created using the same credentials
110119
as ``client`` (provided that BigQuery Storage dependencies are installed).
111-
112-
If both clients are available, ``bqstorage_client`` is used for
113-
fetching query results.
120+
prefer_bqstorage_client (Optional[bool]):
121+
Prefer the BigQuery Storage client over the REST client. If Storage
122+
client isn't available, fall back to the REST client. Defaults to
123+
``True``.
114124
115125
Returns:
116126
google.cloud.bigquery.dbapi.Connection: A new DB-API connection to BigQuery.
117127
"""
118-
returnConnection(client,bqstorage_client)
128+
returnConnection(client,bqstorage_client,prefer_bqstorage_client)

‎tests/unit/test_dbapi_connection.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ def test_connect_w_both_clients(self):
122122
self.assertIs(connection._client,mock_client)
123123
self.assertIs(connection._bqstorage_client,mock_bqstorage_client)
124124

125+
deftest_connect_prefer_bqstorage_client_false(self):
126+
pytest.importorskip("google.cloud.bigquery_storage")
127+
fromgoogle.cloud.bigquery.dbapiimportconnect
128+
fromgoogle.cloud.bigquery.dbapiimportConnection
129+
130+
mock_client=self._mock_client()
131+
mock_bqstorage_client=self._mock_bqstorage_client()
132+
mock_client._ensure_bqstorage_client.return_value=mock_bqstorage_client
133+
134+
connection=connect(
135+
client=mock_client,
136+
bqstorage_client=mock_bqstorage_client,
137+
prefer_bqstorage_client=False,
138+
)
139+
140+
mock_client._ensure_bqstorage_client.assert_not_called()
141+
self.assertIsInstance(connection,Connection)
142+
self.assertIs(connection._client,mock_client)
143+
self.assertIs(connection._bqstorage_client,None)
144+
125145
deftest_raises_error_if_closed(self):
126146
fromgoogle.cloud.bigquery.dbapi.exceptionsimportProgrammingError
127147

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp