-
Notifications
You must be signed in to change notification settings - Fork 470
Support CreateTableTransaction in Glue and Rest #498
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 3 commits
998c6f1
8eace7c
64e6346
ffb8ff6
3a579cd
049e0e2
df0c5ed
755aebf
c98b3b4
09b60ca
04ef8df
211de32
d57ac1c
978a0aa
a413c2e
ad840d5
47ce986
1f5cc28
9ac2f7f
d2617fb
44df2d7
1a4d262
f7c04cf
cecf1c0
2152542
c449fb0
8fc1562
b99c619
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 |
|---|---|---|
|
|
@@ -64,7 +64,14 @@ | |
| from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec | ||
| from pyiceberg.schema import Schema, SchemaVisitor, visit | ||
| from pyiceberg.serializers import FromInputFile | ||
| from pyiceberg.table import CommitTableRequest, CommitTableResponse, Table, update_table_metadata | ||
| from pyiceberg.table import ( | ||
| CommitTableRequest, | ||
| CommitTableResponse, | ||
| StagedTable, | ||
| Table, | ||
| construct_initial_table_metadata, | ||
| update_table_metadata, | ||
| ) | ||
| from pyiceberg.table.metadata import TableMetadata, new_table_metadata | ||
| from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder | ||
| from pyiceberg.typedef import EMPTY_DICT | ||
|
|
@@ -325,12 +332,39 @@ def _update_glue_table(self, database_name: str, table_name: str, table_input: T | |
| f"Cannot commit {database_name}.{table_name} because Glue detected concurrent update to table version {version_id}" | ||
| ) from e | ||
|
|
||
| def _get_glue_table(self, database_name: str, table_name: str) -> TableTypeDef: | ||
| def _get_glue_table(self, database_name: str, table_name: str) -> Optional[TableTypeDef]: | ||
| try: | ||
| load_table_response = self.glue.get_table(DatabaseName=database_name, Name=table_name) | ||
| return load_table_response["Table"] | ||
| except self.glue.exceptions.EntityNotFoundException as e: | ||
| raise NoSuchTableError(f"Table does not exist: {database_name}.{table_name}") from e | ||
| except self.glue.exceptions.EntityNotFoundException: | ||
| return None | ||
|
|
||
| def _create_staged_table( | ||
|
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. I think we might have an opportunity here to move _create_staged_table function into pyiceberg/catalog/init.py and refactor the existing create_table functions on the different catalog implementations to all use _create_staged_table to create an instance of the StagedTable, and then commit that StagedTable to the catalog backend. I think what sets each catalog's implementation of create_table apart is how it handles the commit against the catalog backened, but they all seem to share the same sequence of operations in how it instantiates its notion of a new table. What are your thoughts on this idea @HonahX ?
Contributor
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. Great suggestion! In the initial implementation I did not pay much attention to the catalog code organization. Let me refactor it. |
||
| self, | ||
| identifier: Union[str, Identifier], | ||
| schema: Union[Schema, "pa.Schema"], | ||
| location: Optional[str] = None, | ||
| partition_spec: PartitionSpec = UNPARTITIONED_PARTITION_SPEC, | ||
| sort_order: SortOrder = UNSORTED_SORT_ORDER, | ||
| properties: Properties = EMPTY_DICT, | ||
| ) -> StagedTable: | ||
| schema: Schema = self._convert_schema_if_needed(schema) # type: ignore | ||
|
|
||
| database_name, table_name = self.identifier_to_database_and_table(identifier) | ||
|
|
||
| location = self._resolve_table_location(location, database_name, table_name) | ||
| metadata_location = self._get_metadata_location(location=location) | ||
| metadata = new_table_metadata( | ||
| location=location, schema=schema, partition_spec=partition_spec, sort_order=sort_order, properties=properties | ||
| ) | ||
| io = load_file_io(properties=self.properties, location=metadata_location) | ||
| return StagedTable( | ||
| identifier=(self.name, database_name, table_name), | ||
| metadata=metadata, | ||
| metadata_location=metadata_location, | ||
| io=io, | ||
| catalog=self, | ||
| ) | ||
|
|
||
| def create_table( | ||
| self, | ||
|
|
@@ -412,45 +446,68 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons | |
| database_name, table_name = self.identifier_to_database_and_table(identifier_tuple) | ||
|
|
||
| current_glue_table = self._get_glue_table(database_name=database_name, table_name=table_name) | ||
| glue_table_version_id = current_glue_table.get("VersionId") | ||
| if not glue_table_version_id: | ||
| raise CommitFailedException(f"Cannot commit {database_name}.{table_name} because Glue table version id is missing") | ||
| current_table = self._convert_glue_to_iceberg(glue_table=current_glue_table) | ||
| base_metadata = current_table.metadata | ||
|
|
||
| # Validate the update requirements | ||
| for requirement in table_request.requirements: | ||
| requirement.validate(base_metadata) | ||
|
|
||
| updated_metadata = update_table_metadata(base_metadata, table_request.updates) | ||
| if updated_metadata == base_metadata: | ||
| # no changes, do nothing | ||
| return CommitTableResponse(metadata=base_metadata, metadata_location=current_table.metadata_location) | ||
|
|
||
| # write new metadata | ||
| new_metadata_version = self._parse_metadata_version(current_table.metadata_location) + 1 | ||
| new_metadata_location = self._get_metadata_location(current_table.metadata.location, new_metadata_version) | ||
| self._write_metadata(updated_metadata, current_table.io, new_metadata_location) | ||
|
|
||
| update_table_input = _construct_table_input( | ||
| table_name=table_name, | ||
| metadata_location=new_metadata_location, | ||
| properties=current_table.properties, | ||
| metadata=updated_metadata, | ||
| glue_table=current_glue_table, | ||
| prev_metadata_location=current_table.metadata_location, | ||
| ) | ||
| if current_glue_table is not None: | ||
| # Update the table | ||
| glue_table_version_id = current_glue_table.get("VersionId") | ||
| if not glue_table_version_id: | ||
| raise CommitFailedException( | ||
| f"Cannot commit {database_name}.{table_name} because Glue table version id is missing" | ||
| ) | ||
| current_table = self._convert_glue_to_iceberg(glue_table=current_glue_table) | ||
| base_metadata = current_table.metadata | ||
|
|
||
| # Validate the update requirements | ||
| for requirement in table_request.requirements: | ||
|
HonahX marked this conversation as resolved.
|
||
| requirement.validate(base_metadata) | ||
|
|
||
| updated_metadata = update_table_metadata(base_metadata, table_request.updates) | ||
| if updated_metadata == base_metadata: | ||
| # no changes, do nothing | ||
| return CommitTableResponse(metadata=base_metadata, metadata_location=current_table.metadata_location) | ||
|
|
||
| # write new metadata | ||
| new_metadata_version = self._parse_metadata_version(current_table.metadata_location) + 1 | ||
| new_metadata_location = self._get_metadata_location(current_table.metadata.location, new_metadata_version) | ||
| self._write_metadata(updated_metadata, current_table.io, new_metadata_location) | ||
|
|
||
| update_table_input = _construct_table_input( | ||
| table_name=table_name, | ||
| metadata_location=new_metadata_location, | ||
| properties=current_table.properties, | ||
| metadata=updated_metadata, | ||
| glue_table=current_glue_table, | ||
| prev_metadata_location=current_table.metadata_location, | ||
| ) | ||
|
|
||
| # Pass `version_id` to implement optimistic locking: it ensures updates are rejected if concurrent | ||
| # modifications occur. See more details at https://iceberg.apache.org/docs/latest/aws/#optimistic-locking | ||
| self._update_glue_table( | ||
| database_name=database_name, | ||
| table_name=table_name, | ||
| table_input=update_table_input, | ||
| version_id=glue_table_version_id, | ||
| ) | ||
| # Pass `version_id` to implement optimistic locking: it ensures updates are rejected if concurrent | ||
| # modifications occur. See more details at https://iceberg.apache.org/docs/latest/aws/#optimistic-locking | ||
| self._update_glue_table( | ||
| database_name=database_name, | ||
| table_name=table_name, | ||
| table_input=update_table_input, | ||
| version_id=glue_table_version_id, | ||
| ) | ||
|
|
||
| return CommitTableResponse(metadata=updated_metadata, metadata_location=new_metadata_location) | ||
| else: | ||
| # Create the table | ||
| updated_metadata = construct_initial_table_metadata(table_request.updates) | ||
| new_metadata_version = 0 | ||
| new_metadata_location = self._get_metadata_location(updated_metadata.location, new_metadata_version) | ||
| self._write_metadata( | ||
| updated_metadata, self._load_file_io(updated_metadata.properties, new_metadata_location), new_metadata_location | ||
| ) | ||
|
|
||
| create_table_input = _construct_table_input( | ||
| table_name=table_name, | ||
| metadata_location=new_metadata_location, | ||
| properties=updated_metadata.properties, | ||
| metadata=updated_metadata, | ||
| ) | ||
|
|
||
| self._create_glue_table(database_name=database_name, table_name=table_name, table_input=create_table_input) | ||
|
|
||
| return CommitTableResponse(metadata=updated_metadata, metadata_location=new_metadata_location) | ||
| return CommitTableResponse(metadata=updated_metadata, metadata_location=new_metadata_location) | ||
|
|
||
| def load_table(self, identifier: Union[str, Identifier]) -> Table: | ||
| """Load the table's metadata and returns the table instance. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.