|
35 | 35 | from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec |
36 | 36 | from pyiceberg.schema import Schema |
37 | 37 | from pyiceberg.serializers import FromInputFile |
38 | | -from pyiceberg.table import CommitTableResponse, Table |
| 38 | +from pyiceberg.table import CommitTableResponse, CreateTableTransaction, Table |
39 | 39 | from pyiceberg.table.metadata import new_table_metadata |
40 | 40 | from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder |
41 | 41 | from pyiceberg.table.update import TableRequirement, TableUpdate |
@@ -92,36 +92,57 @@ def commit_table( |
92 | 92 | table_identifier = table.name() |
93 | 93 | database_name, table_name = self.identifier_to_database_and_table(table_identifier, NoSuchTableError) |
94 | 94 |
|
95 | | - current_table, version_token = self._load_table_and_version(identifier=table_identifier) |
96 | | - |
97 | | - updated_staged_table = self._update_and_stage_table(current_table, table_identifier, requirements, updates) |
98 | | - if current_table and updated_staged_table.metadata == current_table.metadata: |
99 | | - # no changes, do nothing |
100 | | - return CommitTableResponse(metadata=current_table.metadata, metadata_location=current_table.metadata_location) |
101 | | - |
102 | | - self._write_metadata( |
103 | | - metadata=updated_staged_table.metadata, |
104 | | - io=updated_staged_table.io, |
105 | | - metadata_path=updated_staged_table.metadata_location, |
106 | | - overwrite=True, |
107 | | - ) |
108 | | - |
109 | | - # try to update metadata location which will fail if the versionToken changed meanwhile |
| 95 | + current_table: Optional[Table] |
| 96 | + version_token: Optional[str] |
110 | 97 | try: |
111 | | - self.s3tables.update_table_metadata_location( |
112 | | - tableBucketARN=self.table_bucket_arn, |
113 | | - namespace=database_name, |
114 | | - name=table_name, |
115 | | - versionToken=version_token, |
116 | | - metadataLocation=updated_staged_table.metadata_location, |
| 98 | + current_table, version_token = self._load_table_and_version(identifier=table_identifier) |
| 99 | + except NoSuchTableError: |
| 100 | + current_table = None |
| 101 | + version_token = None |
| 102 | + |
| 103 | + if current_table: |
| 104 | + updated_staged_table = self._update_and_stage_table(current_table, table_identifier, requirements, updates) |
| 105 | + if updated_staged_table.metadata == current_table.metadata: |
| 106 | + # no changes, do nothing |
| 107 | + return CommitTableResponse(metadata=current_table.metadata, metadata_location=current_table.metadata_location) |
| 108 | + |
| 109 | + self._write_metadata( |
| 110 | + metadata=updated_staged_table.metadata, |
| 111 | + io=updated_staged_table.io, |
| 112 | + metadata_path=updated_staged_table.metadata_location, |
| 113 | + overwrite=True, |
117 | 114 | ) |
118 | | - except self.s3tables.exceptions.ConflictException as e: |
119 | | - raise CommitFailedException( |
120 | | - f"Cannot commit {database_name}.{table_name} because of a concurrent update to the table version {version_token}." |
121 | | - ) from e |
122 | | - return CommitTableResponse( |
123 | | - metadata=updated_staged_table.metadata, metadata_location=updated_staged_table.metadata_location |
124 | | - ) |
| 115 | + |
| 116 | + # try to update metadata location which will fail if the versionToken changed meanwhile |
| 117 | + try: |
| 118 | + self.s3tables.update_table_metadata_location( |
| 119 | + tableBucketARN=self.table_bucket_arn, |
| 120 | + namespace=database_name, |
| 121 | + name=table_name, |
| 122 | + versionToken=version_token, |
| 123 | + metadataLocation=updated_staged_table.metadata_location, |
| 124 | + ) |
| 125 | + except self.s3tables.exceptions.ConflictException as e: |
| 126 | + raise CommitFailedException( |
| 127 | + f"Cannot commit {database_name}.{table_name} because of a concurrent update to the table version {version_token}." |
| 128 | + ) from e |
| 129 | + return CommitTableResponse( |
| 130 | + metadata=updated_staged_table.metadata, metadata_location=updated_staged_table.metadata_location |
| 131 | + ) |
| 132 | + else: |
| 133 | + # table does not exist, create it |
| 134 | + raise NotImplementedError("Creating a table on commit is currently not supported.") |
| 135 | + |
| 136 | + def create_table_transaction( |
| 137 | + self, |
| 138 | + identifier: Union[str, Identifier], |
| 139 | + schema: Union[Schema, "pa.Schema"], |
| 140 | + location: Optional[str] = None, |
| 141 | + partition_spec: PartitionSpec = UNPARTITIONED_PARTITION_SPEC, |
| 142 | + sort_order: SortOrder = UNSORTED_SORT_ORDER, |
| 143 | + properties: Properties = EMPTY_DICT, |
| 144 | + ) -> CreateTableTransaction: |
| 145 | + raise NotImplementedError("create_table_transaction currently not supported.") |
125 | 146 |
|
126 | 147 | def create_namespace(self, namespace: Union[str, Identifier], properties: Properties = EMPTY_DICT) -> None: |
127 | 148 | if properties: |
|
0 commit comments