Skip to content

Commit bfdeb3f

Browse files
authored
feat: add prefer_bqstorage_client option for Connection (#1945)
1 parent cc7b399 commit bfdeb3f

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

google/cloud/bigquery/dbapi/connection.py

+20-10
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
if client is None:
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-
if bqstorage_client is None:
58+
if not prefer_bqstorage_client:
59+
bqstorage_client = None
60+
self._owns_bqstorage_client = False
61+
elif bqstorage_client is None:
5362
bqstorage_client = client._ensure_bqstorage_client()
5463
self._owns_bqstorage_client = bqstorage_client is not None
5564
else:
@@ -95,7 +104,7 @@ def cursor(self):
95104
return new_cursor
96105

97106

98-
def connect(client=None, bqstorage_client=None):
107+
def connect(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-
return Connection(client, bqstorage_client)
128+
return Connection(client, bqstorage_client, prefer_bqstorage_client)

tests/unit/test_dbapi_connection.py

+20
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+
def test_connect_prefer_bqstorage_client_false(self):
126+
pytest.importorskip("google.cloud.bigquery_storage")
127+
from google.cloud.bigquery.dbapi import connect
128+
from google.cloud.bigquery.dbapi import Connection
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
def test_raises_error_if_closed(self):
126146
from google.cloud.bigquery.dbapi.exceptions import ProgrammingError
127147

0 commit comments

Comments
 (0)