Skip to content

Commit 3110f8d

Browse files
committed
Detect google-cloud-bigquery version for backwards compatibility
1 parent 3536cf4 commit 3110f8d

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

ci/requirements-3.5-0.18.1.pip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google-auth==1.4.1
22
google-auth-oauthlib==0.0.1
33
mock
4-
google-cloud-bigquery==0.32.0
4+
google-cloud-bigquery==0.29.0

pandas_gbq/_query.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import pkg_resources
3+
from google.cloud import bigquery
4+
5+
6+
# Version with query config breaking change.
7+
BIGQUERY_CONFIG_VERSION = pkg_resources.parse_version('0.32.0.dev1')
8+
9+
10+
def query_config(resource, installed_version):
11+
if installed_version < BIGQUERY_CONFIG_VERSION:
12+
return bigquery.QueryJobConfig.from_api_repr(resource.get('query', {}))
13+
return bigquery.QueryJobConfig.from_api_repr(resource)

pandas_gbq/gbq.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16+
BIGQUERY_INSTALLED_VERSION = None
17+
18+
1619
def _check_google_client_version():
20+
global BIGQUERY_INSTALLED_VERSION
1721

1822
try:
1923
import pkg_resources
@@ -22,15 +26,15 @@ def _check_google_client_version():
2226
raise ImportError('Could not import pkg_resources (setuptools).')
2327

2428
# https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/bigquery/CHANGELOG.md
25-
bigquery_minimum_version = pkg_resources.parse_version('0.32.0.dev1')
26-
bigquery_installed_version = pkg_resources.get_distribution(
29+
bigquery_minimum_version = pkg_resources.parse_version('0.29.0')
30+
BIGQUERY_INSTALLED_VERSION = pkg_resources.get_distribution(
2731
'google-cloud-bigquery').parsed_version
2832

29-
if bigquery_installed_version < bigquery_minimum_version:
33+
if BIGQUERY_INSTALLED_VERSION < bigquery_minimum_version:
3034
raise ImportError(
3135
'pandas-gbq requires google-cloud-bigquery >= {0}, '
3236
'current version {1}'.format(
33-
bigquery_minimum_version, bigquery_installed_version))
37+
bigquery_minimum_version, BIGQUERY_INSTALLED_VERSION))
3438

3539

3640
def _test_google_api_imports():
@@ -444,8 +448,8 @@ def process_http_error(ex):
444448

445449
def run_query(self, query, **kwargs):
446450
from google.auth.exceptions import RefreshError
447-
from google.cloud.bigquery import QueryJobConfig
448451
from concurrent.futures import TimeoutError
452+
from pandas_gbq import _query
449453

450454
job_config = {
451455
'query': {
@@ -467,12 +471,11 @@ def run_query(self, query, **kwargs):
467471
del config['query']['query']
468472

469473
self._start_timer()
470-
try:
471474

475+
try:
472476
logger.info('Requesting query... ')
473477
query_reply = self.client.query(
474-
query,
475-
job_config=QueryJobConfig.from_api_repr(job_config))
478+
query, job_config=_query.query_config(job_config))
476479
logger.info('ok.\nQuery running...')
477480
except (RefreshError, ValueError):
478481
if self.private_key:

pandas_gbq/tests/test__query.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
import pkg_resources
3+
4+
import mock
5+
6+
7+
@mock.patch('google.cloud.bigquery.QueryJobConfig')
8+
def test_query_config_w_old_bq_version(mock_config):
9+
from pandas_gbq._query import query_config
10+
11+
old_version = pkg_resources.parse_version('0.29.0')
12+
query_config({'query': {'useLegacySql': False}}, old_version)
13+
mock_config.from_api_repr.assert_called_once_with({'useLegacySql': False})
14+
15+
16+
@mock.patch('google.cloud.bigquery.QueryJobConfig')
17+
def test_query_config_w_dev_bq_version(mock_config):
18+
from pandas_gbq._query import query_config
19+
20+
dev_version = pkg_resources.parse_version('0.32.0.dev1')
21+
query_config(
22+
{
23+
'query': {
24+
'useLegacySql': False,
25+
},
26+
'labels': {'key': 'value'},
27+
},
28+
dev_version)
29+
mock_config.from_api_repr.assert_called_once_with(
30+
{
31+
'query': {
32+
'useLegacySql': False,
33+
},
34+
'labels': {'key': 'value'},
35+
})
36+
37+
38+
@mock.patch('google.cloud.bigquery.QueryJobConfig')
39+
def test_query_config_w_new_bq_version(mock_config):
40+
from pandas_gbq._query import query_config
41+
42+
dev_version = pkg_resources.parse_version('1.0.0')
43+
query_config(
44+
{
45+
'query': {
46+
'useLegacySql': False,
47+
},
48+
'labels': {'key': 'value'},
49+
},
50+
dev_version)
51+
mock_config.from_api_repr.assert_called_once_with(
52+
{
53+
'query': {
54+
'useLegacySql': False,
55+
},
56+
'labels': {'key': 'value'},
57+
})

0 commit comments

Comments
 (0)