Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
from __future__ import annotations

import base64
from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Dict, Iterator, Optional, Sequence, Tuple

from google.cloud.bigquery import _versions_helpers
from google.cloud.bigquery import enums
from google.cloud.bigquery import exceptions
from google.cloud.bigquery import _versions_helpers, enums, exceptions

try:
import pyarrow # type: ignore
Expand Down Expand Up @@ -303,6 +301,12 @@ def download_arrow_from_job(
raise ValueError("RowIterator client is None.")
bqstorage_client = row_iterator.client._get_shared_bqstorage_client()
if bqstorage_client is None:
if getattr(row_iterator.client, "_has_custom_api_endpoint", False):
raise ValueError(
"Cannot read multi-page Arrow results with a custom BigQuery "
"api_endpoint or BIGQUERY_EMULATOR_HOST without an explicit "
"bqstorage_client."
)
raise ValueError(
"The google-cloud-bigquery-storage library is required to read Arrow results."
)
Expand Down
118 changes: 92 additions & 26 deletions packages/google-cloud-bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,43 @@
"""Shared helper functions for BigQuery API classes."""

import base64
import copy
import datetime
import decimal
import json
import math
import re
import os
import re
import textwrap
import warnings
from collections.abc import Mapping
from typing import Any, Optional, Tuple, Type, Union

from dateutil import relativedelta
from google.cloud._helpers import UTC # type: ignore
from google.cloud._helpers import _date_from_iso8601_date
from google.cloud._helpers import _datetime_from_microseconds
from google.cloud._helpers import _RFC3339_MICROS
from google.cloud._helpers import _RFC3339_NO_FRACTION
from google.cloud._helpers import _to_bytes
from google.api_core import client_options as client_options_lib
from google.api_core.universe import UniverseMismatchError
from google.auth import compute_engine
from google.auth import credentials as ga_credentials # type: ignore
from google.cloud._helpers import (
_RFC3339_MICROS,
_RFC3339_NO_FRACTION,
UTC, # type: ignore
_date_from_iso8601_date,
_datetime_from_microseconds,
_to_bytes,
)

from google.cloud.bigquery import enums

from google.auth import credentials as ga_credentials # type: ignore
from google.api_core import client_options as client_options_lib
try:
from google.auth.aio import credentials as _aio_credentials

_ANONYMOUS_CREDENTIAL_TYPES: tuple[type[Any], ...] = (
ga_credentials.AnonymousCredentials,
_aio_credentials.AnonymousCredentials,
)
except ImportError: # pragma: NO COVER
_ANONYMOUS_CREDENTIAL_TYPES = (ga_credentials.AnonymousCredentials,)

TimeoutType = Union[float, None]

Expand Down Expand Up @@ -77,7 +93,7 @@


def _get_client_universe(
client_options: Optional[Union[client_options_lib.ClientOptions, dict]]
client_options: client_options_lib.ClientOptions | Mapping[str, Any] | None,
) -> str:
"""Retrieves the specified universe setting.

Expand All @@ -87,7 +103,7 @@ def _get_client_universe(
str: resolved universe setting.

"""
if isinstance(client_options, dict):
if isinstance(client_options, Mapping):
client_options = client_options_lib.from_dict(client_options)
universe = _DEFAULT_UNIVERSE
options_universe = getattr(client_options, "universe_domain", None)
Expand All @@ -104,27 +120,77 @@ def _get_client_universe(
return universe


def _validate_universe(client_universe: str, credentials: ga_credentials.Credentials):
# TODO(universe-domain): Replace with google.api_core.universe.validate_credentials_universe
# once minimum allowed google-api-core version exceeds 2.44.0.
def _validate_universe(
client_universe: str, credentials: ga_credentials.Credentials | None
) -> None:
"""Validates that client provided universe and universe embedded in credentials match.

Args:
client_universe (str): The universe domain configured via the client options.
credentials (ga_credentials.Credentials): The credentials being used in the client.
credentials (ga_credentials.Credentials | None): The credentials being
used in the client. Validation is skipped when ``None``,
``AnonymousCredentials`` (sync or async), or uncached
``compute_engine.Credentials``.

Raises:
ValueError: when client_universe does not match the universe in credentials.
google.api_core.universe.UniverseMismatchError: (subclass of ``ValueError``)
when ``client_universe`` does not match the universe in ``credentials``.
"""
if hasattr(credentials, "universe_domain"):
cred_universe = getattr(credentials, "universe_domain")
if isinstance(cred_universe, str):
if client_universe != cred_universe:
raise ValueError(
"The configured universe domain "
f"({client_universe}) does not match the universe domain "
f"found in the credentials ({cred_universe}). "
"If you haven't configured the universe domain explicitly, "
f"`{_DEFAULT_UNIVERSE}` is the default."
)
if credentials is None or isinstance(credentials, _ANONYMOUS_CREDENTIAL_TYPES):
return
if isinstance(credentials, compute_engine.Credentials) and not getattr(
credentials, "_universe_domain_cached", False
):
return
cred_universe = getattr(credentials, "universe_domain", None)
if isinstance(cred_universe, str) and client_universe != cred_universe:
raise UniverseMismatchError(client_universe, cred_universe)


def _inherit_universe_in_client_options(
parent_universe: str,
client_options: client_options_lib.ClientOptions | Mapping[str, Any] | None = None,
) -> client_options_lib.ClientOptions | None:
"""Inherit parent universe domain when child client_options leaves it unset.

Caller-supplied mappings and ``ClientOptions`` instances are not mutated.

Args:
parent_universe: The resolved universe domain of the parent client.
client_options: Optional child client options mapping or object.
When ``None``, a new ``ClientOptions`` is created only if
inheritance is needed.

Returns:
A ``ClientOptions`` instance with ``universe_domain`` populated when
needed, or ``None`` when ``client_options`` is ``None`` and the
parent uses the default universe with no overriding env var.
"""
should_inherit = (
parent_universe != _DEFAULT_UNIVERSE
or _get_client_universe(None) != parent_universe
)
if client_options is None:
if should_inherit:
return client_options_lib.ClientOptions(universe_domain=parent_universe)
return None
if isinstance(client_options, Mapping):
opts = client_options_lib.from_dict(client_options)
if not opts.universe_domain and (should_inherit or opts.universe_domain == ""):
opts.universe_domain = parent_universe
return opts
if isinstance(client_options, client_options_lib.ClientOptions):
if not client_options.universe_domain and (
should_inherit or client_options.universe_domain == ""
):
opts = copy.copy(client_options)
opts.universe_domain = parent_universe
return opts
return client_options
# Preserve non-ClientOptions objects (such as unittest.mock sentinels in caller tests).
return client_options


def _get_bigquery_host():
Expand Down Expand Up @@ -648,7 +714,7 @@ def _range_field_to_json(range_element_type, value):
end = value.get("end")
else:
raise ValueError(
f"Unsupported type of RANGE value {value}, must be " "string or dict"
f"Unsupported type of RANGE value {value}, must be string or dict"
)

start = _range_element_to_json(start, range_element_type)
Expand Down
Loading
Loading