-
Notifications
You must be signed in to change notification settings - Fork 321
Feat: Adds foreign_type_info attribute to table class and adds unit tests. #2126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7427b21
ee866b8
de4cfd5
3ee933b
5e6138e
fcd7f7b
269ec23
a0f823e
e5ed4b8
89e3a7d
212c58e
44f0447
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,8 @@ | |
| import functools | ||
| import operator | ||
| import typing | ||
| from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union | ||
| from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union, Sequence | ||
|
|
||
| import warnings | ||
|
|
||
| try: | ||
|
|
@@ -66,6 +67,7 @@ | |
| from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration | ||
| from google.cloud.bigquery.enums import DefaultPandasDTypes | ||
| from google.cloud.bigquery.external_config import ExternalConfig | ||
| from google.cloud.bigquery import schema as _schema | ||
| from google.cloud.bigquery.schema import _build_schema_resource | ||
| from google.cloud.bigquery.schema import _parse_schema_resource | ||
| from google.cloud.bigquery.schema import _to_schema_fields | ||
|
|
@@ -398,7 +400,7 @@ class Table(_TableBase): | |
| "partitioning_type": "timePartitioning", | ||
| "range_partitioning": "rangePartitioning", | ||
| "time_partitioning": "timePartitioning", | ||
| "schema": "schema", | ||
| "schema": ["schema", "fields"], | ||
| "snapshot_definition": "snapshotDefinition", | ||
| "clone_definition": "cloneDefinition", | ||
| "streaming_buffer": "streamingBuffer", | ||
|
|
@@ -411,6 +413,7 @@ class Table(_TableBase): | |
| "max_staleness": "maxStaleness", | ||
| "resource_tags": "resourceTags", | ||
| "external_catalog_table_options": "externalCatalogTableOptions", | ||
| "foreign_type_info": ["schema", "foreignTypeInfo"], | ||
| } | ||
|
|
||
| def __init__(self, table_ref, schema=None) -> None: | ||
|
|
@@ -451,8 +454,20 @@ def schema(self): | |
| If ``schema`` is not a sequence, or if any item in the sequence | ||
| is not a :class:`~google.cloud.bigquery.schema.SchemaField` | ||
| instance or a compatible mapping representation of the field. | ||
|
|
||
| .. Note:: | ||
| If you are referencing a schema for an external catalog table such | ||
| as a Hive table, it will also be necessary to populate the foreign_type_info | ||
| attribute. This is not necessary if defining the schema for a BigQuery table. | ||
|
|
||
| For details, see: | ||
| https://cloud.google.com/bigquery/docs/external-tables | ||
| https://cloud.google.com/bigquery/docs/datasets-intro#external_datasets | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need some logic in the setter for schema to avoid overwriting the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I'm not sure if this format will render well in the docs. We might just move all the contents under
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not yet addressed Tim's comment here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is possible for a user to supply an Due to the nested separation of I don't think any additional checks are required. Also, in |
||
| """ | ||
| prop = self._properties.get(self._PROPERTY_TO_API_FIELD["schema"]) | ||
| prop = _helpers._get_sub_prop( | ||
| self._properties, self._PROPERTY_TO_API_FIELD["schema"] | ||
| ) | ||
| if not prop: | ||
| return [] | ||
| else: | ||
|
|
@@ -463,10 +478,21 @@ def schema(self, value): | |
| api_field = self._PROPERTY_TO_API_FIELD["schema"] | ||
|
|
||
| if value is None: | ||
| self._properties[api_field] = None | ||
| else: | ||
| _helpers._set_sub_prop( | ||
| self._properties, | ||
| api_field, | ||
| None, | ||
| ) | ||
| elif isinstance(value, Sequence): | ||
| value = _to_schema_fields(value) | ||
| self._properties[api_field] = {"fields": _build_schema_resource(value)} | ||
| value = _build_schema_resource(value) | ||
| _helpers._set_sub_prop( | ||
| self._properties, | ||
| api_field, | ||
| value, | ||
| ) | ||
| else: | ||
| raise TypeError("Schema must be a Sequence (e.g. a list) or None.") | ||
|
|
||
| @property | ||
| def labels(self): | ||
|
|
@@ -1075,6 +1101,43 @@ def external_catalog_table_options( | |
| self._PROPERTY_TO_API_FIELD["external_catalog_table_options"] | ||
| ] = value | ||
|
|
||
| @property | ||
| def foreign_type_info(self) -> Optional[_schema.ForeignTypeInfo]: | ||
| """Optional. Specifies metadata of the foreign data type definition in | ||
| field schema (TableFieldSchema.foreign_type_definition). | ||
|
|
||
| Returns: | ||
| Optional[schema.ForeignTypeInfo]: | ||
| Foreign type information, or :data:`None` if not set. | ||
|
|
||
| .. Note:: | ||
| foreign_type_info is only required if you are referencing an | ||
| external catalog such as a Hive table. | ||
| For details, see: | ||
| https://cloud.google.com/bigquery/docs/external-tables | ||
| https://cloud.google.com/bigquery/docs/datasets-intro#external_datasets | ||
| """ | ||
|
|
||
| prop = _helpers._get_sub_prop( | ||
| self._properties, self._PROPERTY_TO_API_FIELD["foreign_type_info"] | ||
| ) | ||
| if prop is not None: | ||
| return _schema.ForeignTypeInfo.from_api_repr(prop) | ||
| return None | ||
|
|
||
| @foreign_type_info.setter | ||
| def foreign_type_info(self, value: Union[_schema.ForeignTypeInfo, dict, None]): | ||
| value = _helpers._isinstance_or_raise( | ||
| value, | ||
| (_schema.ForeignTypeInfo, dict), | ||
| none_allowed=True, | ||
| ) | ||
| if isinstance(value, _schema.ForeignTypeInfo): | ||
| value = value.to_api_repr() | ||
| _helpers._set_sub_prop( | ||
| self._properties, self._PROPERTY_TO_API_FIELD["foreign_type_info"], value | ||
| ) | ||
|
|
||
| @classmethod | ||
| def from_string(cls, full_table_id: str) -> "Table": | ||
| """Construct a table from fully-qualified table ID. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.