Repository navigation
fix(pandas-gbq): reuse the provided bigquery_client instead of building new clients - #19153
abhishekacharya200 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request enables the propagation of a user-provided bigquery_client through to_gbq, _Table, and _Dataset to avoid redundant client instantiations, and updates GbqConnector._download_results to use self.client. It also adds corresponding unit tests. The feedback suggests using self.client instead of self._bigquery_client in _Table.create to ensure that the lazily-created client is properly shared and reused when a client is not explicitly provided.
| if not _Dataset( | ||
| self.project_id, | ||
| credentials=self.credentials, | ||
| bigquery_client=self._bigquery_client, | ||
| ).exists(self.dataset_id): | ||
| _Dataset( | ||
| self.project_id, | ||
| credentials=self.credentials, | ||
| location=self.location, | ||
| bigquery_client=self._bigquery_client, | ||
| ).create(self.dataset_id) |
There was a problem hiding this comment.
Using self.client instead of self._bigquery_client ensures that the active BigQuery client is reused. If self._bigquery_client is None (because no client was explicitly provided to the constructor), passing self._bigquery_client would cause _Dataset to instantiate its own new client. Utilizing self.client ensures that the lazily-created client is shared, avoiding redundant client creation.
| if not _Dataset( | |
| self.project_id, | |
| credentials=self.credentials, | |
| bigquery_client=self._bigquery_client, | |
| ).exists(self.dataset_id): | |
| _Dataset( | |
| self.project_id, | |
| credentials=self.credentials, | |
| location=self.location, | |
| bigquery_client=self._bigquery_client, | |
| ).create(self.dataset_id) | |
| if not _Dataset( | |
| self.project_id, | |
| credentials=self.credentials, | |
| bigquery_client=self.client, | |
| ).exists(self.dataset_id): | |
| _Dataset( | |
| self.project_id, | |
| credentials=self.credentials, | |
| location=self.location, | |
| bigquery_client=self.client, | |
| ).create(self.dataset_id) |
References
- Avoid eagerly creating or initializing a client or resource if a downstream process can lazily create it only when needed. Utilizing the lazy-loaded client property ensures that we reuse the same client instance instead of triggering redundant client instantiations.
read_gbqandto_gbqdocumentbigquery_clientas the client that will be used.GbqConnectordoes store it asself.client, but two places still built their own client from the project and credentials, so the caller's client (and anything configured on it, such as client options or an endpoint) was bypassed:GbqConnector._download_resultspassedbqclient=self.get_client(), a freshly constructedbigquery.Client, todownload_results. That client is used for theget_tablecall behind the large-results warning. It now passesself.client, which is the provided client when there is one and an equivalent client otherwise.to_gbqcreates a missing table through_Table(...), and_Table.createcreates a missing dataset through_Dataset(...). Neither acceptedbigquery_client, so both built a new client from credentials. They now accept it andto_gbqforwards it.Added
test_to_gbq_creates_table_and_dataset_with_provided_clientandtest_GbqConnector_download_results_uses_provided_client. Both fail onmainand pass with this change. The package's unit tests pass locally (251 passed, 1 skipped) andruff format --checkreports nothing on the changed files.