|
18 | 18 | Job.configuration.query.tableDefinitions. |
19 | 19 | """ |
20 | 20 |
|
21 | | -from __future__ import absolute_import |
| 21 | +from __future__ import absolute_import, annotations |
22 | 22 |
|
23 | 23 | import base64 |
24 | 24 | import copy |
25 | 25 | from typing import Any, Dict, FrozenSet, Iterable, Optional, Union |
26 | 26 |
|
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 | +) |
31 | 35 | 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 |
33 | 37 |
|
34 | 38 |
|
35 | 39 | class ExternalSourceFormat(object): |
@@ -1003,3 +1007,182 @@ def from_api_repr(cls, resource: dict) -> "ExternalConfig": |
1003 | 1007 | config = cls(resource["sourceFormat"]) |
1004 | 1008 | config._properties = copy.deepcopy(resource) |
1005 | 1009 | 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 |
0 commit comments