Skip to content

fix(bigquery): propagate universe_domain and guard custom endpoints in _ensure_bqstorage_client - #19199

Draft
chalmerlowe wants to merge 2 commits into
mainfrom
fix/bigquery-tpc-bqstorage-universe
Draft

chalmerlowe wants to merge 2 commits into
mainfrom
fix/bigquery-tpc-bqstorage-universe

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Context

When bigquery.Client is configured for a non-default universe such as a Trusted Partner Cloud (TPC) environment, or when a custom api_endpoint or BIGQUERY_EMULATOR_HOST is specified, universe configuration and routing can diverge in two places:

  1. Calling Client._ensure_bqstorage_client() with an empty ClientOptions() object or {} mapping (where universe_domain is unset) bypassed parent universe_domain inheritance, causing the child BigQuery Storage client to fall back to the default environment rather than inheriting self._universe_domain. Similarly, when GOOGLE_CLOUD_UNIVERSE_DOMAIN was set in the environment and bigquery.Client explicitly selected googleapis.com, leaving child universe_domain unset allowed the child client to pick up the environment variable instead of the parent client's universe.
  2. Setting client_options.api_endpoint on bigquery.Client skipped _validate_universe() during initialization, and subsequent calls to _ensure_bqstorage_client() without an explicit BigQuery Storage endpoint still constructed a default BigQueryReadClient instead of falling back to the configured BigQuery REST connection.

Solution

  • Propagate parent universe_domain safely (_helpers.py): Added _propagate_universe_to_client_options() to propagate the parent universe_domain onto None, Mapping, and ClientOptions inputs whenever child universe_domain is unset and either parent_universe is non-default or GOOGLE_CLOUD_UNIVERSE_DOMAIN differs from parent_universe, using a shallow copy so caller-supplied objects and mappings are never mutated.
  • Consistent universe validation (_helpers.py, client.py): Updated Client.__init__ to resolve client_universe unconditionally, expose a read-only Client.universe_domain property, and run _validate_universe() even when client_options.api_endpoint is provided (as well as validating explicit child client_options in _ensure_bqstorage_client()). Updated _validate_universe() to exempt None and AnonymousCredentials and raise google.api_core.universe.UniverseMismatchError (a ValueError subclass).
  • Guard custom BigQuery endpoints in _ensure_bqstorage_client() (client.py): Synchronized self._has_custom_api_endpoint with self._connection.API_BASE_URL across client_options.api_endpoint and BIGQUERY_EMULATOR_HOST. When a custom BigQuery endpoint is active without an explicit BigQuery Storage api_endpoint, _ensure_bqstorage_client() emits a UserWarning and returns None so callers (to_dataframe, to_arrow, DB-API, and magics) fall back cleanly to the BigQuery REST transport.
  • Unit tests (tests/unit/test_tpc_universe.py): Added client universe conformance tests, boundary and environment-precedence tests, and _ensure_bqstorage_client() routing, non-mutation, and REST fallback tests across GDU, TPC, and emulator configurations.

Notes for Reviewers

  • UniverseMismatchError subclasses ValueError, preserving backward compatibility for callers catching ValueError on mismatched credentials and universe settings.
  • Returning None from _ensure_bqstorage_client() matches the existing fallback contract used when google-cloud-bigquery-storage is not installed, allowing RowIterator.to_arrow(), RowIterator.to_dataframe(), and bigquery.dbapi.connect() to read results over self._connection without extra caller changes.

@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 introduces support for propagating and validating universe domains across the BigQuery client and its sub-clients, specifically the BigQuery Storage client. Key changes include the addition of the _propagate_universe_to_client_options helper, raising UniverseMismatchError on mismatched credentials, exposing a universe_domain property on the Client, and implementing a fallback to REST with a warning when custom endpoints are configured without matching Storage endpoints. Additionally, a comprehensive test suite has been added to verify universe-domain conformance and routing behavior. No review comments were provided, so there is no feedback to address.

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