- Notifications
You must be signed in to change notification settings - Fork321
feat: Addtable_constraints field to Table model#1755
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
31 commits Select commitHold shift + click to select a range
cea6e03 feat: add `table_constraints` field to Table model
dimkonko9e34810 Change `raise` to `return` in __eq__ methods
dimkonko5d034f4 Fix __eq__ for ColumnReference
dimkonkoda663b2 Add column_references to ForeignKey __eq__
dimkonko29d1238 Add missing coverage
dimkonko51b3c58 Merge branch 'main' into main
chalmerlowec69d717 Update google/cloud/bigquery/table.py
chalmerlowe8cdbc04 Update google/cloud/bigquery/table.py
chalmerlowe55054bb Update google/cloud/bigquery/table.py
chalmerlowe9d49e50 Update tests/unit/test_table.py
chalmerlowe7bdfad9 Update tests/unit/test_table.py
chalmerlowe5145eb9 Update tests/unit/test_table.py
chalmerlowecd1ee01 Update tests/unit/test_table.py
chalmerlowedd8a16b Update tests/unit/test_table.py
chalmerlowe1fa1cfd Update tests/unit/test_table.py
chalmerlowe43ec134 Update tests/unit/test_table.py
chalmerlowe3b566b8 Update tests/unit/test_table.py
chalmerlowe34df1c6 Merge branch 'main' into main
chalmerloweeec92da Update google/cloud/bigquery/table.py
chalmerlowea84edc4 Update google/cloud/bigquery/table.py
chalmerlowebec1b4a Update google/cloud/bigquery/table.py
chalmerlowe8e07232 Update tests/unit/test_table.py
chalmerlowe60ba10c Update tests/unit/test_table.py
chalmerlowe75f3482 Update tests/unit/test_table.py
chalmerlowe21cac59 Update tests/unit/test_table.py
chalmerlowe69a7235 Update tests/unit/test_table.py
chalmerlowec6ac023 Update tests/unit/test_table.py
chalmerlowe7f40eab Update tests/unit/test_table.py
chalmerlowe1959ef9 Merge branch 'main' into main
chalmerlowe22256f6 Merge branch 'main' into main
chalmerlowe506afa4 Merge branch 'main' into main
chalmerloweFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -390,6 +390,7 @@ class Table(_TableBase): | ||
| "view_use_legacy_sql": "view", | ||
| "view_query": "view", | ||
| "require_partition_filter": "requirePartitionFilter", | ||
| "table_constraints": "tableConstraints", | ||
| } | ||
| def __init__(self, table_ref, schema=None) -> None: | ||
| @@ -973,6 +974,16 @@ def clone_definition(self) -> Optional["CloneDefinition"]: | ||
| clone_info = CloneDefinition(clone_info) | ||
| return clone_info | ||
| @property | ||
| def table_constraints(self) -> Optional["TableConstraints"]: | ||
| """Tables Primary Key and Foreign Key information.""" | ||
| table_constraints = self._properties.get( | ||
| self._PROPERTY_TO_API_FIELD["table_constraints"] | ||
| ) | ||
| if table_constraints is not None: | ||
| table_constraints = TableConstraints.from_api_repr(table_constraints) | ||
| return table_constraints | ||
| @classmethod | ||
| def from_string(cls, full_table_id: str) -> "Table": | ||
| """Construct a table from fully-qualified table ID. | ||
| @@ -2958,6 +2969,123 @@ def __repr__(self): | ||
| return "TimePartitioning({})".format(",".join(key_vals)) | ||
| class PrimaryKey: | ||
| """Represents the primary key constraint on a table's columns. | ||
| Args: | ||
| columns: The columns that are composed of the primary key constraint. | ||
| """ | ||
| def __init__(self, columns: List[str]): | ||
| self.columns = columns | ||
| def __eq__(self, other): | ||
| if not isinstance(other, PrimaryKey): | ||
| raise TypeError("The value provided is not a BigQuery PrimaryKey.") | ||
| return self.columns == other.columns | ||
| class ColumnReference: | ||
| """The pair of the foreign key column and primary key column. | ||
| Args: | ||
| referencing_column: The column that composes the foreign key. | ||
| referenced_column: The column in the primary key that are referenced by the referencingColumn. | ||
| """ | ||
| def __init__(self, referencing_column: str, referenced_column: str): | ||
| self.referencing_column = referencing_column | ||
| self.referenced_column = referenced_column | ||
| def __eq__(self, other): | ||
| if not isinstance(other, ColumnReference): | ||
| raise TypeError("The value provided is not a BigQuery ColumnReference.") | ||
| return ( | ||
| self.referencing_column == other.referencing_column | ||
| and self.referenced_column == other.referenced_column | ||
| ) | ||
| class ForeignKey: | ||
| """Represents a foreign key constraint on a table's columns. | ||
| Args: | ||
| name: Set only if the foreign key constraint is named. | ||
| referenced_table: The table that holds the primary key and is referenced by this foreign key. | ||
| column_references: The columns that compose the foreign key. | ||
| """ | ||
| def __init__( | ||
| self, | ||
| name: str, | ||
| referenced_table: TableReference, | ||
| column_references: List[ColumnReference], | ||
| ): | ||
| self.name = name | ||
| self.referenced_table = referenced_table | ||
| self.column_references = column_references | ||
| def __eq__(self, other): | ||
| if not isinstance(other, ForeignKey): | ||
Collaborator 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. This (3019) is also identified by coverage.py as being untested | ||
| raise TypeError("The value provided is not a BigQuery ForeignKey.") | ||
| return ( | ||
| self.name == other.name | ||
| and self.referenced_table == other.referenced_table | ||
| and self.column_references == other.column_references | ||
| ) | ||
| @classmethod | ||
| def from_api_repr(cls, api_repr: Dict[str, Any]) -> "ForeignKey": | ||
| """Create an instance from API representation.""" | ||
| return cls( | ||
| name=api_repr["name"], | ||
| referenced_table=TableReference.from_api_repr(api_repr["referencedTable"]), | ||
| column_references=[ | ||
| ColumnReference( | ||
| column_reference_resource["referencingColumn"], | ||
| column_reference_resource["referencedColumn"], | ||
| ) | ||
| for column_reference_resource in api_repr["columnReferences"] | ||
| ], | ||
| ) | ||
| class TableConstraints: | ||
| """The TableConstraints defines the primary key and foreign key. | ||
| Args: | ||
| primary_key: | ||
| Represents a primary key constraint on a table's columns. Present only if the table | ||
| has a primary key. The primary key is not enforced. | ||
| foreign_keys: | ||
| Present only if the table has a foreign key. The foreign key is not enforced. | ||
| """ | ||
| def __init__( | ||
| self, | ||
| primary_key: Optional[PrimaryKey], | ||
| foreign_keys: Optional[List[ForeignKey]], | ||
| ): | ||
| self.primary_key = primary_key | ||
| self.foreign_keys = foreign_keys | ||
| @classmethod | ||
| def from_api_repr(cls, resource: Dict[str, Any]) -> "TableConstraints": | ||
| """Create an instance from API representation.""" | ||
| primary_key = None | ||
| if "primaryKey" in resource: | ||
| primary_key = PrimaryKey(resource["primaryKey"]["columns"]) | ||
| foreign_keys = None | ||
| if "foreignKeys" in resource: | ||
| foreign_keys = [ | ||
| ForeignKey.from_api_repr(foreign_key_resource) | ||
| for foreign_key_resource in resource["foreignKeys"] | ||
| ] | ||
| return cls(primary_key, foreign_keys) | ||
| def _item_to_row(iterator, resource): | ||
| """Convert a JSON row to the native object. | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.