1414 bigquery = None
1515 google_exceptions = None
1616
17- try :
18- # The BigQuery Storage API client is an optional dependency. It is only
19- # required when use_bqstorage_api=True.
20- from google .cloud import bigquery_storage_v1beta1
21- except ImportError : # pragma: NO COVER
22- bigquery_storage_v1beta1 = None
23-
2417from pandas_gbq .exceptions import AccessDenied
18+ from pandas_gbq .exceptions import PerformanceWarning
2519import pandas_gbq .schema
2620import pandas_gbq .timestamp
2721
3024
3125BIGQUERY_INSTALLED_VERSION = None
3226BIGQUERY_CLIENT_INFO_VERSION = "1.12.0"
27+ BIGQUERY_BQSTORAGE_VERSION = "1.24.0"
3328HAS_CLIENT_INFO = False
29+ HAS_BQSTORAGE_SUPPORT = False
3430
3531try :
3632 import tqdm # noqa
3935
4036
4137def _check_google_client_version ():
42- global BIGQUERY_INSTALLED_VERSION , HAS_CLIENT_INFO , SHOW_VERBOSE_DEPRECATION
38+ global BIGQUERY_INSTALLED_VERSION , HAS_CLIENT_INFO , HAS_BQSTORAGE_SUPPORT , SHOW_VERBOSE_DEPRECATION
4339
4440 try :
4541 import pkg_resources
4642
4743 except ImportError :
4844 raise ImportError ("Could not import pkg_resources (setuptools)." )
4945
50- # https://github.com/GoogleCloudPlatform/google-cloud- python/blob/master/bigquery /CHANGELOG.md
46+ # https://github.com/googleapis/ python-bigquery /blob/master/CHANGELOG.md
5147 bigquery_minimum_version = pkg_resources .parse_version ("1.11.0" )
5248 bigquery_client_info_version = pkg_resources .parse_version (
5349 BIGQUERY_CLIENT_INFO_VERSION
5450 )
51+ bigquery_bqstorage_version = pkg_resources .parse_version (
52+ BIGQUERY_BQSTORAGE_VERSION
53+ )
5554 BIGQUERY_INSTALLED_VERSION = pkg_resources .get_distribution (
5655 "google-cloud-bigquery"
5756 ).parsed_version
5857
5958 HAS_CLIENT_INFO = (
6059 BIGQUERY_INSTALLED_VERSION >= bigquery_client_info_version
6160 )
61+ HAS_BQSTORAGE_SUPPORT = (
62+ BIGQUERY_INSTALLED_VERSION >= bigquery_bqstorage_version
63+ )
6264
6365 if BIGQUERY_INSTALLED_VERSION < bigquery_minimum_version :
6466 raise ImportError (
@@ -548,14 +550,30 @@ def _download_results(
548550 if user_dtypes is None :
549551 user_dtypes = {}
550552
551- try :
552- bqstorage_client = None
553- if max_results is None :
554- # Only use the BigQuery Storage API if the full result set is requested.
555- bqstorage_client = _make_bqstorage_client (
556- self .use_bqstorage_api , self .credentials
557- )
553+ if self .use_bqstorage_api and not HAS_BQSTORAGE_SUPPORT :
554+ warnings .warn (
555+ (
556+ "use_bqstorage_api was set, but have google-cloud-bigquery "
557+ "version {}. Requires google-cloud-bigquery version "
558+ "{} or later."
559+ ).format (
560+ BIGQUERY_INSTALLED_VERSION , BIGQUERY_BQSTORAGE_VERSION
561+ ),
562+ PerformanceWarning ,
563+ stacklevel = 4 ,
564+ )
565+
566+ create_bqstorage_client = self .use_bqstorage_api
567+ if max_results is not None :
568+ create_bqstorage_client = False
569+
570+ to_dataframe_kwargs = {}
571+ if HAS_BQSTORAGE_SUPPORT :
572+ to_dataframe_kwargs [
573+ "create_bqstorage_client"
574+ ] = create_bqstorage_client
558575
576+ try :
559577 query_job .result ()
560578 # Get the table schema, so that we can list rows.
561579 destination = self .client .get_table (query_job .destination )
@@ -568,16 +586,11 @@ def _download_results(
568586 conversion_dtypes .update (user_dtypes )
569587 df = rows_iter .to_dataframe (
570588 dtypes = conversion_dtypes ,
571- bqstorage_client = bqstorage_client ,
572589 progress_bar_type = progress_bar_type ,
590+ ** to_dataframe_kwargs
573591 )
574592 except self .http_error as ex :
575593 self .process_http_error (ex )
576- finally :
577- if bqstorage_client :
578- # Clean up open socket resources. See:
579- # https://github.com/pydata/pandas-gbq/issues/294
580- bqstorage_client .transport .channel .close ()
581594
582595 if df .empty :
583596 df = _cast_empty_df_dtypes (schema_fields , df )
@@ -763,27 +776,6 @@ def _cast_empty_df_dtypes(schema_fields, df):
763776 return df
764777
765778
766- def _make_bqstorage_client (use_bqstorage_api , credentials ):
767- if not use_bqstorage_api :
768- return None
769-
770- if bigquery_storage_v1beta1 is None :
771- raise ImportError (
772- "Install the google-cloud-bigquery-storage and fastavro/pyarrow "
773- "packages to use the BigQuery Storage API."
774- )
775-
776- import google .api_core .gapic_v1 .client_info
777- import pandas
778-
779- client_info = google .api_core .gapic_v1 .client_info .ClientInfo (
780- user_agent = "pandas-{}" .format (pandas .__version__ )
781- )
782- return bigquery_storage_v1beta1 .BigQueryStorageClient (
783- credentials = credentials , client_info = client_info
784- )
785-
786-
787779def read_gbq (
788780 query ,
789781 project_id = None ,
0 commit comments