|
| 1 | +# Copyright 2024 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 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +import google.auth.credentials |
| 18 | +import google.cloud.bigquery as bq |
| 19 | +import pydata_google_auth |
| 20 | +from pydata_google_auth import cache |
| 21 | + |
| 22 | +from ibis.backends.bigquery import ( |
| 23 | + Backend as BigQueryBackend, |
| 24 | + _create_client_info, |
| 25 | + parse_project_and_dataset, |
| 26 | + CLIENT_ID, |
| 27 | + CLIENT_SECRET, |
| 28 | + EXTERNAL_DATA_SCOPES, |
| 29 | + SCOPES, |
| 30 | +) |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + import google.cloud.bigquery_storage_v1 |
| 34 | + |
| 35 | + |
| 36 | +class Backend(BigQueryBackend): |
| 37 | + def __init__(self): |
| 38 | + super().__init__() |
| 39 | + self.storage_client = None |
| 40 | + |
| 41 | + def do_connect( |
| 42 | + self, |
| 43 | + project_id: str = None, |
| 44 | + dataset_id: str = "", |
| 45 | + credentials: google.auth.credentials.Credentials = None, |
| 46 | + application_name: str = None, |
| 47 | + auth_local_webserver: bool = True, |
| 48 | + auth_external_data: bool = False, |
| 49 | + auth_cache: str = "default", |
| 50 | + partition_column: str = "PARTITIONTIME", |
| 51 | + # Custom DVT arguments: |
| 52 | + bigquery_client: bq.Client = None, |
| 53 | + bqstorage_client: "google.cloud.bigquery_storage_v1.BigQueryReadClient" = None, |
| 54 | + ): |
| 55 | + """Copy of Ibis v5 BigQuery do_connect() customized for DVT, see original method for docs.""" |
| 56 | + default_project_id = "" |
| 57 | + |
| 58 | + if credentials is None: |
| 59 | + scopes = SCOPES |
| 60 | + if auth_external_data: |
| 61 | + scopes = EXTERNAL_DATA_SCOPES |
| 62 | + |
| 63 | + if auth_cache == "default": |
| 64 | + credentials_cache = cache.ReadWriteCredentialsCache( |
| 65 | + filename="ibis.json" |
| 66 | + ) |
| 67 | + elif auth_cache == "reauth": |
| 68 | + credentials_cache = cache.WriteOnlyCredentialsCache( |
| 69 | + filename="ibis.json" |
| 70 | + ) |
| 71 | + elif auth_cache == "none": |
| 72 | + credentials_cache = cache.NOOP |
| 73 | + else: |
| 74 | + raise ValueError( |
| 75 | + f"Got unexpected value for auth_cache = '{auth_cache}'. " |
| 76 | + "Expected one of 'default', 'reauth', or 'none'." |
| 77 | + ) |
| 78 | + |
| 79 | + credentials, default_project_id = pydata_google_auth.default( |
| 80 | + scopes, |
| 81 | + client_id=CLIENT_ID, |
| 82 | + client_secret=CLIENT_SECRET, |
| 83 | + credentials_cache=credentials_cache, |
| 84 | + use_local_webserver=auth_local_webserver, |
| 85 | + ) |
| 86 | + |
| 87 | + project_id = project_id or default_project_id |
| 88 | + |
| 89 | + ( |
| 90 | + self.data_project, |
| 91 | + self.billing_project, |
| 92 | + self.dataset, |
| 93 | + ) = parse_project_and_dataset(project_id, dataset_id) |
| 94 | + |
| 95 | + if bigquery_client is None: |
| 96 | + self.client = bq.Client( |
| 97 | + project=self.billing_project, |
| 98 | + credentials=credentials, |
| 99 | + client_info=_create_client_info(application_name), |
| 100 | + ) |
| 101 | + else: |
| 102 | + self.client = bigquery_client |
| 103 | + self.partition_column = partition_column |
| 104 | + self.storage_client = bqstorage_client |
| 105 | + |
| 106 | + def _cursor_to_arrow( |
| 107 | + self, |
| 108 | + cursor, |
| 109 | + *, |
| 110 | + method=None, |
| 111 | + chunk_size: int = None, |
| 112 | + ): |
| 113 | + """Copy of Ibis v5 BigQuery _cursor_to_arrow() except can use custom DVT storage client""" |
| 114 | + if method is None: |
| 115 | + |
| 116 | + def method(result, storage_client=self.storage_client): |
| 117 | + return result.to_arrow( |
| 118 | + progress_bar_type=None, |
| 119 | + # Include DVT specific storage client. |
| 120 | + bqstorage_client=storage_client, |
| 121 | + create_bqstorage_client=bool(not self.storage_client), |
| 122 | + ) |
| 123 | + |
| 124 | + query = cursor.query |
| 125 | + query_result = query.result(page_size=chunk_size) |
| 126 | + # workaround potentially not having the ability to create read sessions |
| 127 | + # in the dataset project |
| 128 | + orig_project = query_result._project |
| 129 | + query_result._project = self.billing_project |
| 130 | + try: |
| 131 | + arrow_obj = method(query_result) |
| 132 | + finally: |
| 133 | + query_result._project = orig_project |
| 134 | + return arrow_obj |
| 135 | + |
| 136 | + def list_primary_key_columns(self, database: str, table: str) -> list: |
| 137 | + """Return a list of primary key column names.""" |
| 138 | + # TODO: Related to issue-1253, it's not clear if this is possible, we should revisit if it becomes a requirement. |
| 139 | + return None |
| 140 | + |
| 141 | + def dvt_list_tables(self, like=None, database=None): |
| 142 | + return self.list_tables(like=like, database=database) |
0 commit comments