-
Notifications
You must be signed in to change notification settings - Fork 322
feat: Adds attributes to SchemaField #2077
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
Merged
chalmerlowe
merged 6 commits into
pangea-v1alpha
from
feat-b358215039-add-attrs-to-schemafield
Nov 26, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
808a925
Updates SchemaField with new fields and adds tests
chalmerlowe 83c2ef5
Merge branch 'pangea-v1alpha' into feat-b358215039-add-attrs-to-schem…
chalmerlowe 6a340ff
Adds additional reference to Foreign
chalmerlowe a24dfbc
adds test for type vs ftd and test for enums
chalmerlowe 907b77c
Updates enums, schema, and tests, plus linting
chalmerlowe e6eecbc
Updates some formatting of a comment to accomodate black
chalmerlowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,14 +26,14 @@ | |
| _isinstance_or_raise, | ||
| _get_sub_prop, | ||
| ) | ||
| from google.cloud.bigquery.enums import StandardSqlTypeNames | ||
| from google.cloud.bigquery.enums import StandardSqlTypeNames, RoundingMode | ||
|
|
||
|
|
||
| _STRUCT_TYPES = ("RECORD", "STRUCT") | ||
|
|
||
| # SQL types reference: | ||
| # https://cloud.google.com/bigquery/data-types#legacy_sql_data_types | ||
| # https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types | ||
| # LEGACY SQL: https://cloud.google.com/bigquery/data-types#legacy_sql_data_types | ||
| # GoogleSQL: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types | ||
| LEGACY_TO_STANDARD_TYPES = { | ||
| "STRING": StandardSqlTypeNames.STRING, | ||
| "BYTES": StandardSqlTypeNames.BYTES, | ||
|
|
@@ -52,6 +52,7 @@ | |
| "DATE": StandardSqlTypeNames.DATE, | ||
| "TIME": StandardSqlTypeNames.TIME, | ||
| "DATETIME": StandardSqlTypeNames.DATETIME, | ||
| "FOREIGN": StandardSqlTypeNames.FOREIGN, | ||
| # no direct conversion from ARRAY, the latter is represented by mode="REPEATED" | ||
| } | ||
| """String names of the legacy SQL types to integer codes of Standard SQL standard_sql.""" | ||
|
|
@@ -170,6 +171,34 @@ class SchemaField(object): | |
| the type is RANGE, this field is required. Possible values for the | ||
| field element type of a RANGE include `DATE`, `DATETIME` and | ||
| `TIMESTAMP`. | ||
|
|
||
| rounding_mode: Union[RoundingMode, str, None] | ||
| Specifies the rounding mode to be used when storing values of | ||
| NUMERIC and BIGNUMERIC type. | ||
|
|
||
| Unspecified will default to using ROUND_HALF_AWAY_FROM_ZERO. | ||
|
|
||
| ROUND_HALF_AWAY_FROM_ZERO rounds half values away from zero | ||
| when applying precision and scale upon writing of NUMERIC and BIGNUMERIC | ||
| values. | ||
| For Scale: 0 | ||
| 1.1, 1.2, 1.3, 1.4 => 1 | ||
| 1.5, 1.6, 1.7, 1.8, 1.9 => 2 | ||
|
|
||
| ROUND_HALF_EVEN rounds half values to the nearest even value | ||
| when applying precision and scale upon writing of NUMERIC and BIGNUMERIC | ||
| values. | ||
| For Scale: 0 | ||
| 1.1, 1.2, 1.3, 1.4 => 1 | ||
| 1.5 => 2 | ||
| 1.6, 1.7, 1.8, 1.9 => 2 | ||
| 2.5 => 2 | ||
|
|
||
| foreign_type_definition: Optional[str] | ||
| Definition of the foreign data type. | ||
|
|
||
| Only valid for top-level schema fields (not nested fields). | ||
| If the type is FOREIGN, this field is required. | ||
| """ | ||
|
|
||
| def __init__( | ||
|
|
@@ -185,11 +214,12 @@ def __init__( | |
| scale: Union[int, _DefaultSentinel] = _DEFAULT_VALUE, | ||
| max_length: Union[int, _DefaultSentinel] = _DEFAULT_VALUE, | ||
| range_element_type: Union[FieldElementType, str, None] = None, | ||
| rounding_mode: Union[RoundingMode, str, None] = None, | ||
| foreign_type_definition: Optional[str] = None, | ||
| ): | ||
| self._properties: Dict[str, Any] = { | ||
| "name": name, | ||
| "type": field_type, | ||
| } | ||
| self._properties: Dict[str, Any] = {} | ||
|
|
||
| self._properties["name"] = name | ||
| if mode is not None: | ||
| self._properties["mode"] = mode.upper() | ||
| if description is not _DEFAULT_VALUE: | ||
|
|
@@ -210,6 +240,23 @@ def __init__( | |
| self._properties["rangeElementType"] = {"type": range_element_type} | ||
| if isinstance(range_element_type, FieldElementType): | ||
| self._properties["rangeElementType"] = range_element_type.to_api_repr() | ||
| if isinstance(rounding_mode, RoundingMode): | ||
| self._properties["roundingMode"] = rounding_mode.name | ||
| if isinstance(rounding_mode, str): | ||
| self._properties["roundingMode"] = rounding_mode | ||
| if isinstance(foreign_type_definition, str): | ||
| self._properties["foreignTypeDefinition"] = foreign_type_definition | ||
|
|
||
| # The order of operations is important: | ||
| # If field_type is FOREIGN, then foreign_type_definition must be set. | ||
| if field_type != "FOREIGN": | ||
| self._properties["type"] = field_type | ||
| else: | ||
| if self._properties.get("foreignTypeDefinition") is None: | ||
| raise ValueError( | ||
| "If the 'field_type' is 'FOREIGN', then 'foreign_type_definition' is required." | ||
| ) | ||
| self._properties["type"] = field_type | ||
|
|
||
| self._fields = tuple(fields) | ||
|
|
||
|
|
@@ -251,6 +298,9 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField": | |
| else: | ||
| element_type = None | ||
|
|
||
| rounding_mode = api_repr.get("roundingMode") | ||
| foreign_type_definition = api_repr.get("foreignTypeDefinition") | ||
|
|
||
| return cls( | ||
| field_type=field_type, | ||
| fields=[cls.from_api_repr(f) for f in fields], | ||
|
|
@@ -263,6 +313,8 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField": | |
| scale=cls.__get_int(api_repr, "scale"), | ||
| max_length=cls.__get_int(api_repr, "maxLength"), | ||
| range_element_type=element_type, | ||
| rounding_mode=rounding_mode, | ||
| foreign_type_definition=foreign_type_definition, | ||
| ) | ||
|
|
||
| @property | ||
|
|
@@ -330,6 +382,22 @@ def range_element_type(self): | |
| ret = self._properties.get("rangeElementType") | ||
| return FieldElementType.from_api_repr(ret) | ||
|
|
||
| @property | ||
| def rounding_mode(self): | ||
| """Enum that specifies the rounding mode to be used when storing values of | ||
| NUMERIC and BIGNUMERIC type. | ||
| """ | ||
| return self._properties.get("roundingMode") | ||
|
|
||
| @property | ||
| def foreign_type_definition(self): | ||
| """Definition of the foreign data type. | ||
|
|
||
| Only valid for top-level schema fields (not nested fields). | ||
| If the type is FOREIGN, this field is required. | ||
|
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. are there any checks for this?
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. There is now. 😺 Starting around line 250. |
||
| """ | ||
| return self._properties.get("foreignTypeDefinition") | ||
|
|
||
| @property | ||
| def fields(self): | ||
| """Optional[tuple]: Subfields contained in this field. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can this be indented to align with the rest of the commend above it?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting.
When you try to indent the second line,
blackthe Python formatter kicks the starting point back to the left and aligns it under theFinForeign.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved both lines of comment above the object FOREIGN so that they both align together.