Skip to content

Commit 5d0b7d6

Browse files
committed
Merge branch 'add-pangea-classes' into pangea-v1alpha
Creates a development branch focused on pangea features.
2 parents 1061611 + 116de78 commit 5d0b7d6

File tree

12 files changed

+1259
-11
lines changed

12 files changed

+1259
-11
lines changed

google/cloud/bigquery/_helpers.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
"""Shared helper functions for BigQuery API classes."""
1616

1717
import base64
18+
import copy
1819
import datetime
1920
import decimal
2021
import json
2122
import math
2223
import re
2324
import os
2425
import warnings
25-
from typing import Optional, Union
26+
from typing import Optional, Union, Any, Tuple, Type
2627

2728
from dateutil import relativedelta
2829
from google.cloud._helpers import UTC # type: ignore
@@ -1004,3 +1005,52 @@ def _verify_job_config_type(job_config, expected_type, param_name="job_config"):
10041005
job_config=job_config,
10051006
)
10061007
)
1008+
1009+
1010+
def _isinstance_or_raise(
1011+
value: Any,
1012+
dtype: Union[Type, Tuple[Type, ...]],
1013+
none_allowed: Optional[bool] = False,
1014+
) -> Any:
1015+
"""Determine whether a value type matches a given datatype or None.
1016+
1017+
Args:
1018+
value (Any): Value to be checked.
1019+
dtype (type): Expected data type or tuple of data types.
1020+
none_allowed Optional(bool): whether value is allowed to be None. Default
1021+
is False.
1022+
1023+
Returns:
1024+
Any: Returns the input value if the type check is successful.
1025+
1026+
Raises:
1027+
TypeError: If the input value's type does not match the expected data type(s).
1028+
"""
1029+
if none_allowed and value is None:
1030+
return value
1031+
1032+
if isinstance(value, dtype):
1033+
return value
1034+
1035+
or_none = ""
1036+
if none_allowed:
1037+
or_none = " (or None)"
1038+
1039+
msg = f"Pass {value} as a '{dtype}'{or_none}. Got {type(value)}."
1040+
raise TypeError(msg)
1041+
1042+
1043+
def _from_api_repr(cls, resource: dict):
1044+
"""Factory: constructs an instance of the class (cls)
1045+
given its API representation.
1046+
1047+
Args:
1048+
resource (Dict[str, Any]):
1049+
API representation of the object to be instantiated.
1050+
1051+
Returns:
1052+
An instance of the class initialized with data from 'resource'.
1053+
"""
1054+
config = cls
1055+
config._properties = copy.deepcopy(resource)
1056+
return config

google/cloud/bigquery/dataset.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import google.cloud._helpers # type: ignore
2424

2525
from google.cloud.bigquery import _helpers
26+
from google.cloud.bigquery._helpers import _isinstance_or_raise
2627
from google.cloud.bigquery.model import ModelReference
2728
from google.cloud.bigquery.routine import Routine, RoutineReference
2829
from google.cloud.bigquery.table import Table, TableReference
2930
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
31+
from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions
32+
3033

3134
from typing import Optional, List, Dict, Any, Union
3235

@@ -530,6 +533,7 @@ class Dataset(object):
530533
"storage_billing_model": "storageBillingModel",
531534
"max_time_travel_hours": "maxTimeTravelHours",
532535
"default_rounding_mode": "defaultRoundingMode",
536+
"external_catalog_dataset_options": "externalCatalogDatasetOptions",
533537
}
534538

535539
def __init__(self, dataset_ref) -> None:
@@ -937,10 +941,31 @@ def _build_resource(self, filter_fields):
937941
"""Generate a resource for ``update``."""
938942
return _helpers._build_resource_from_properties(self, filter_fields)
939943

940-
table = _get_table_reference
944+
@property
945+
def external_catalog_dataset_options(self):
946+
"""Options defining open source compatible datasets living in the
947+
BigQuery catalog. Contains metadata of open source database, schema
948+
or namespace represented by the current dataset."""
941949

942-
model = _get_model_reference
950+
prop = _helpers._get_sub_prop(
951+
self._properties, ["externalCatalogDatasetOptions"]
952+
)
943953

954+
if prop is not None:
955+
prop = ExternalCatalogDatasetOptions().from_api_repr(prop)
956+
return prop
957+
958+
@external_catalog_dataset_options.setter
959+
def external_catalog_dataset_options(self, value):
960+
value = _isinstance_or_raise(
961+
value, ExternalCatalogDatasetOptions, none_allowed=True
962+
)
963+
self._properties[
964+
self._PROPERTY_TO_API_FIELD["external_catalog_dataset_options"]
965+
] = value.to_api_repr()
966+
967+
table = _get_table_reference
968+
model = _get_model_reference
944969
routine = _get_routine_reference
945970

946971
def __repr__(self):

google/cloud/bigquery/external_config.py

Lines changed: 189 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
Job.configuration.query.tableDefinitions.
1919
"""
2020

21-
from __future__ import absolute_import
21+
from __future__ import absolute_import, annotations
2222

2323
import base64
2424
import copy
2525
from typing import Any, Dict, FrozenSet, Iterable, Optional, Union
2626

27-
from google.cloud.bigquery._helpers import _to_bytes
28-
from google.cloud.bigquery._helpers import _bytes_to_json
29-
from google.cloud.bigquery._helpers import _int_or_none
30-
from google.cloud.bigquery._helpers import _str_or_none
27+
from google.cloud.bigquery._helpers import (
28+
_to_bytes,
29+
_bytes_to_json,
30+
_int_or_none,
31+
_str_or_none,
32+
_isinstance_or_raise,
33+
_get_sub_prop,
34+
)
3135
from google.cloud.bigquery.format_options import AvroOptions, ParquetOptions
32-
from google.cloud.bigquery.schema import SchemaField
36+
from google.cloud.bigquery.schema import SchemaField, StorageDescriptor
3337

3438

3539
class ExternalSourceFormat(object):
@@ -1003,3 +1007,182 @@ def from_api_repr(cls, resource: dict) -> "ExternalConfig":
10031007
config = cls(resource["sourceFormat"])
10041008
config._properties = copy.deepcopy(resource)
10051009
return config
1010+
1011+
1012+
class ExternalCatalogDatasetOptions:
1013+
"""Options defining open source compatible datasets living in the BigQuery catalog.
1014+
Contains metadata of open source database, schema or namespace represented
1015+
by the current dataset.
1016+
1017+
Args:
1018+
default_storage_location_uri (Optional[str]): The storage location URI for all
1019+
tables in the dataset. Equivalent to hive metastore's database
1020+
locationUri. Maximum length of 1024 characters. (str)
1021+
parameters (Optional[dict[str, Any]]): A map of key value pairs defining the parameters
1022+
and properties of the open source schema. Maximum size of 2Mib.
1023+
"""
1024+
1025+
def __init__(
1026+
self,
1027+
default_storage_location_uri: Optional[str] = None,
1028+
parameters: Optional[Dict[str, Any]] = None,
1029+
):
1030+
self._properties = {}
1031+
self.default_storage_location_uri = default_storage_location_uri
1032+
self.parameters = parameters
1033+
1034+
@property
1035+
def default_storage_location_uri(self) -> Any:
1036+
"""Optional. The storage location URI for all tables in the dataset.
1037+
Equivalent to hive metastore's database locationUri. Maximum length of
1038+
1024 characters."""
1039+
1040+
return self._properties.get("defaultStorageLocationUri")
1041+
1042+
@default_storage_location_uri.setter
1043+
def default_storage_location_uri(self, value: str):
1044+
value = _isinstance_or_raise(value, str, none_allowed=True)
1045+
self._properties["defaultStorageLocationUri"] = value
1046+
1047+
@property
1048+
def parameters(self) -> Any:
1049+
"""Optional. A map of key value pairs defining the parameters and
1050+
properties of the open source schema. Maximum size of 2Mib."""
1051+
1052+
return self._properties.get("parameters")
1053+
1054+
@parameters.setter
1055+
def parameters(self, value: dict[str, Any]):
1056+
value = _isinstance_or_raise(value, dict, none_allowed=True)
1057+
self._properties["parameters"] = value
1058+
1059+
def to_api_repr(self) -> dict:
1060+
"""Build an API representation of this object.
1061+
1062+
Returns:
1063+
Dict[str, Any]:
1064+
A dictionary in the format used by the BigQuery API.
1065+
"""
1066+
config = copy.deepcopy(self._properties)
1067+
return config
1068+
1069+
@classmethod
1070+
def from_api_repr(cls, resource: dict) -> ExternalCatalogDatasetOptions:
1071+
"""Factory: constructs an instance of the class (cls)
1072+
given its API representation.
1073+
1074+
Args:
1075+
resource (Dict[str, Any]):
1076+
API representation of the object to be instantiated.
1077+
1078+
Returns:
1079+
An instance of the class initialized with data from 'resource'.
1080+
"""
1081+
config = cls()
1082+
config._properties = copy.deepcopy(resource)
1083+
return config
1084+
1085+
1086+
class ExternalCatalogTableOptions:
1087+
"""Metadata about open source compatible table. The fields contained in these
1088+
options correspond to hive metastore's table level properties.
1089+
1090+
Args:
1091+
connection_id (Optional[str]): The connection specifying the credentials to be
1092+
used to read external storage, such as Azure Blob, Cloud Storage, or
1093+
S3. The connection is needed to read the open source table from
1094+
BigQuery Engine. The connection_id can have the form `..` or
1095+
`projects//locations//connections/`.
1096+
parameters (Union[Dict[str, Any], None]): A map of key value pairs defining the parameters
1097+
and properties of the open source table. Corresponds with hive meta
1098+
store table parameters. Maximum size of 4Mib.
1099+
storage_descriptor (Optional[StorageDescriptor]): A storage descriptor containing information
1100+
about the physical storage of this table.
1101+
"""
1102+
1103+
def __init__(
1104+
self,
1105+
connection_id: Optional[str] = None,
1106+
parameters: Union[Dict[str, Any], None] = None,
1107+
storage_descriptor: Optional[
1108+
StorageDescriptor
1109+
] = None, # TODO implement StorageDescriptor, then correct this type hint
1110+
):
1111+
self._properties = {} # type: Dict[str, Any]
1112+
self.connection_id = connection_id
1113+
self.parameters = parameters
1114+
self.storage_descriptor = storage_descriptor
1115+
1116+
@property
1117+
def connection_id(self):
1118+
"""Optional. The connection specifying the credentials to be
1119+
used to read external storage, such as Azure Blob, Cloud Storage, or
1120+
S3. The connection is needed to read the open source table from
1121+
BigQuery Engine. The connection_id can have the form `..` or
1122+
`projects//locations//connections/`. (str)
1123+
"""
1124+
return self._properties.get("connectionId")
1125+
1126+
@connection_id.setter
1127+
def connection_id(self, value: Optional[str]):
1128+
value = _isinstance_or_raise(value, str, none_allowed=True)
1129+
self._properties["connectionId"] = value
1130+
1131+
@property
1132+
def parameters(self) -> Any:
1133+
"""Optional. A map of key value pairs defining the parameters and
1134+
properties of the open source table. Corresponds with hive meta
1135+
store table parameters. Maximum size of 4Mib.
1136+
"""
1137+
1138+
return self._properties.get("parameters")
1139+
1140+
@parameters.setter
1141+
def parameters(self, value: Union[Dict[str, Any], None]):
1142+
value = _isinstance_or_raise(value, dict, none_allowed=True)
1143+
self._properties["parameters"] = value
1144+
1145+
@property
1146+
def storage_descriptor(self) -> Any:
1147+
"""Optional. A storage descriptor containing information about the
1148+
physical storage of this table."""
1149+
1150+
prop = _get_sub_prop(self._properties, ["storageDescriptor"])
1151+
1152+
if prop is not None:
1153+
prop = StorageDescriptor().from_api_repr(prop)
1154+
return prop
1155+
1156+
@storage_descriptor.setter
1157+
def storage_descriptor(self, value):
1158+
value = _isinstance_or_raise(value, StorageDescriptor, none_allowed=True)
1159+
if value is not None:
1160+
self._properties["storageDescriptor"] = value.to_api_repr()
1161+
else:
1162+
self._properties["storageDescriptor"] = value
1163+
1164+
def to_api_repr(self) -> dict:
1165+
"""Build an API representation of this object.
1166+
1167+
Returns:
1168+
Dict[str, Any]:
1169+
A dictionary in the format used by the BigQuery API.
1170+
"""
1171+
config = copy.deepcopy(self._properties)
1172+
return config
1173+
1174+
@classmethod
1175+
def from_api_repr(cls, resource: dict) -> ExternalCatalogTableOptions:
1176+
"""Factory: constructs an instance of the class (cls)
1177+
given its API representation.
1178+
1179+
Args:
1180+
resource (Dict[str, Any]):
1181+
API representation of the object to be instantiated.
1182+
1183+
Returns:
1184+
An instance of the class initialized with data from 'resource'.
1185+
"""
1186+
config = cls()
1187+
config._properties = copy.deepcopy(resource)
1188+
return config

google/cloud/bigquery/query.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,6 @@ def __init__(
10031003
):
10041004
self.name = name
10051005
self.range_element_type = self._parse_range_element_type(range_element_type)
1006-
print(self.range_element_type.type_._type)
10071006
self.start = start
10081007
self.end = end
10091008

0 commit comments

Comments
 (0)