Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -97,6 +105,7 @@ def create_bigquery_client(
credentials=credentials,
location=location,
default_query_job_config=default_query_job_config,
client_options=client_options,
)


Expand Down
3 changes: 3 additions & 0 deletions packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ def __init__(
credentials_info=None,
credentials_base64=None,
list_tables_page_size=1000,
client_options=None,
*args,
**kwargs,
):
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions packages/sqlalchemy-bigquery/tests/unit/test_universe_domain.py
Original file line number Diff line number Diff line change
@@ -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
Loading