Skip to content

fix(pandas-gbq): reuse the provided bigquery_client instead of building new clients - #19153

Open
abhishekacharya200 wants to merge 1 commit into
googleapis:mainfrom
abhishekacharya200:fix-pandas-gbq-bigquery-client-reuse
Open

abhishekacharya200 wants to merge 1 commit into
googleapis:mainfrom
abhishekacharya200:fix-pandas-gbq-bigquery-client-reuse

Conversation

@abhishekacharya200

Copy link
Copy Markdown
Contributor

read_gbq and to_gbq document bigquery_client as the client that will be used. GbqConnector does store it as self.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_results passed bqclient=self.get_client(), a freshly constructed bigquery.Client, to download_results. That client is used for the get_table call behind the large-results warning. It now passes self.client, which is the provided client when there is one and an equivalent client otherwise.
  • to_gbq creates a missing table through _Table(...), and _Table.create creates a missing dataset through _Dataset(...). Neither accepted bigquery_client, so both built a new client from credentials. They now accept it and to_gbq forwards it.

Added test_to_gbq_creates_table_and_dataset_with_provided_client and test_GbqConnector_download_results_uses_provided_client. Both fail on main and pass with this change. The package's unit tests pass locally (251 passed, 1 skipped) and ruff format --check reports nothing on the changed files.

  • Make sure to open an issue as a bug/issue before writing your code (no issue opened)
  • Ensure the tests and linter pass
  • Code coverage does not decrease (not measured)

@abhishekacharya200
abhishekacharya200 requested review from a team as code owners November 20, 2026 08:46
@abhishekacharya200
abhishekacharya200 requested review from tswast and removed request for a team November 20, 2026 08:46

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +834 to 844
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant