From 03d5d3d15a57749a66bdb679a3fbee3081c78d74 Mon Sep 17 00:00:00 2001 From: chalmer lowe Date: Tue, 1 Dec 2026 10:24:05 -0500 Subject: [PATCH] feat(sqlalchemy-bigquery): support client_options in BigQueryDialect and create_bigquery_client --- .../sqlalchemy_bigquery/_helpers.py | 11 ++- .../sqlalchemy_bigquery/base.py | 3 + .../tests/unit/test_universe_domain.py | 77 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 packages/sqlalchemy-bigquery/tests/unit/test_universe_domain.py diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py index 2977860d122f..c6bfad494112 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py @@ -8,11 +8,13 @@ import functools import json import re -from typing import Optional +from collections.abc import Mapping +from typing import Any, Optional, Union import google.auth import sqlalchemy from google.api_core import client_info +from google.api_core import client_options as client_options_lib from google.cloud import bigquery from google.oauth2 import service_account @@ -45,6 +47,9 @@ def create_bigquery_client( location: Optional[str] = None, project_id: Optional[str] = None, user_agent: Optional[google.api_core.client_info.ClientInfo] = None, + client_options: Optional[ + Union[client_options_lib.ClientOptions, Mapping[str, Any]] + ] = None, ) -> google.cloud.bigquery.Client: """Construct a BigQuery client object. @@ -64,6 +69,9 @@ def create_bigquery_client( requests. If ``None``, then default info will be used. Generally, you only need to set this if you're developing your own library or partner tool. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, Mapping[str, Any]]]): + Client options (such as ``universe_domain`` or ``api_endpoint``) + forwarded to ``google.cloud.bigquery.Client``. """ default_project = None @@ -97,6 +105,7 @@ def create_bigquery_client( credentials=credentials, location=location, default_query_job_config=default_query_job_config, + client_options=client_options, ) diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py index fa37d3bd0b5c..cfdb99b27241 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py @@ -1357,6 +1357,7 @@ def __init__( credentials_info=None, credentials_base64=None, list_tables_page_size=1000, + client_options=None, *args, **kwargs, ): @@ -1371,6 +1372,7 @@ def __init__( self.identifier_preparer = self.preparer(self) self.dataset_id = None self.list_tables_page_size = list_tables_page_size + self.client_options = client_options @classmethod def dbapi(cls): @@ -1445,6 +1447,7 @@ def create_connect_args(self, url): project_id=self.billing_project_id, location=self.location, default_query_job_config=default_query_job_config, + client_options=self.client_options, ) # If the user specified `bigquery://` we need to set the project_id # from the client diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_universe_domain.py b/packages/sqlalchemy-bigquery/tests/unit/test_universe_domain.py new file mode 100644 index 000000000000..0a1a43241799 --- /dev/null +++ b/packages/sqlalchemy-bigquery/tests/unit/test_universe_domain.py @@ -0,0 +1,77 @@ +# Copyright 2026 The sqlalchemy-bigquery Authors +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. + +"""Unit tests for client_options and universe_domain forwarding.""" + +from types import MappingProxyType +from unittest import mock + +import pytest +import sqlalchemy +from google.api_core import client_options as client_options_lib +from google.auth import credentials as auth_credentials + +from sqlalchemy_bigquery import _helpers +from sqlalchemy_bigquery.base import BigQueryDialect + +CUSTOM_UNIVERSE = "apis-tpclp.goog" + + +@pytest.mark.parametrize( + "opts", + [ + pytest.param( + client_options_lib.ClientOptions(universe_domain=CUSTOM_UNIVERSE), + id="client_options_instance", + ), + pytest.param( + {"universe_domain": CUSTOM_UNIVERSE}, + id="dict", + ), + pytest.param( + MappingProxyType({"universe_domain": CUSTOM_UNIVERSE}), + id="mapping_proxy", + ), + pytest.param(None, id="none"), + ], +) +def test_create_bigquery_client_forwards_client_options(opts): + fake_creds = auth_credentials.AnonymousCredentials() + with ( + mock.patch.object( + _helpers.google.auth, "default", return_value=(fake_creds, "test-proj") + ), + mock.patch.object(_helpers.bigquery, "Client") as mock_client_cls, + ): + _helpers.create_bigquery_client(client_options=opts) + + mock_client_cls.assert_called_once() + assert mock_client_cls.call_args.kwargs["client_options"] is opts + + +def test_dialect_forwards_client_options_to_create_bigquery_client(): + opts = client_options_lib.ClientOptions( + universe_domain=CUSTOM_UNIVERSE, + api_endpoint=f"https://bigquery.{CUSTOM_UNIVERSE}", + ) + dialect = BigQueryDialect(client_options=opts) + url = sqlalchemy.engine.url.make_url("bigquery://test-proj/test_ds") + + with mock.patch.object(_helpers, "create_bigquery_client") as mock_create: + mock_create.return_value = mock.Mock(project="test-proj") + dialect.create_connect_args(url) + + mock_create.assert_called_once() + assert mock_create.call_args.kwargs["client_options"] is opts + + +def test_create_engine_accepts_client_options(): + opts = {"universe_domain": CUSTOM_UNIVERSE} + engine = sqlalchemy.create_engine( + "bigquery://test-proj", + client_options=opts, + ) + assert engine.dialect.client_options == opts