Skip to content

Commit 94ce254

Browse files
committed
feat: delete s3 table if writing new_table_metadata is unsuccessful
1 parent 1afaa8c commit 94ce254

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

pyiceberg/catalog/s3tables.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -150,46 +150,49 @@ def create_table(
150150
# S3 Tables API and then write the new metadata.json to the warehouseLocation associated with the newly
151151
# created S3 Table.
152152
try:
153-
self.s3tables.create_table(
153+
version_token = self.s3tables.create_table(
154154
tableBucketARN=self.table_bucket_arn, namespace=namespace, name=table_name, format=S3TABLES_FORMAT
155-
)
155+
)["versionToken"]
156156
except self.s3tables.exceptions.NotFoundException as e:
157157
raise NoSuchNamespaceError(f"Cannot create {namespace}.{table_name} because no such namespace exists.") from e
158158
except self.s3tables.exceptions.ConflictException as e:
159159
raise TableAlreadyExistsError(
160160
f"Cannot create {namespace}.{table_name} because a table of the same name already exists in the namespace."
161161
) from e
162162

163-
response = self.s3tables.get_table_metadata_location(tableBucketARN=self.table_bucket_arn, namespace=namespace, name=table_name)
164-
version_token = response["versionToken"]
165-
166-
warehouse_location = response["warehouseLocation"]
167-
metadata_location = self._get_metadata_location(location=warehouse_location)
168-
metadata = new_table_metadata(
169-
location=warehouse_location,
170-
schema=schema,
171-
partition_spec=partition_spec,
172-
sort_order=sort_order,
173-
properties=properties,
174-
)
175-
176-
io = load_file_io(properties=self.properties, location=metadata_location)
177-
# this triggers unsupported list operation error as S3 Table Buckets only support a subset of the S3 Bucket API,
178-
# setting overwrite=True is a workaround for now since it prevents a call to list_objects
179-
self._write_metadata(metadata, io, metadata_location, overwrite=True)
180-
181163
try:
182-
self.s3tables.update_table_metadata_location(
183-
tableBucketARN=self.table_bucket_arn,
184-
namespace=namespace,
185-
name=table_name,
186-
versionToken=version_token,
187-
metadataLocation=metadata_location,
164+
response = self.s3tables.get_table_metadata_location(tableBucketARN=self.table_bucket_arn, namespace=namespace, name=table_name)
165+
warehouse_location = response["warehouseLocation"]
166+
167+
metadata_location = self._get_metadata_location(location=warehouse_location)
168+
metadata = new_table_metadata(
169+
location=warehouse_location,
170+
schema=schema,
171+
partition_spec=partition_spec,
172+
sort_order=sort_order,
173+
properties=properties,
188174
)
189-
except self.s3tables.exceptions.ConflictException as e:
190-
raise CommitFailedException(
191-
f"Cannot create {namespace}.{table_name} because of a concurrent update to the table version {version_token}."
192-
) from e
175+
176+
io = load_file_io(properties=self.properties, location=metadata_location)
177+
# this triggers unsupported list operation error as S3 Table Buckets only support a subset of the S3 Bucket API,
178+
# setting overwrite=True is a workaround for now since it prevents a call to list_objects
179+
self._write_metadata(metadata, io, metadata_location, overwrite=True)
180+
181+
try:
182+
self.s3tables.update_table_metadata_location(
183+
tableBucketARN=self.table_bucket_arn,
184+
namespace=namespace,
185+
name=table_name,
186+
versionToken=version_token,
187+
metadataLocation=metadata_location,
188+
)
189+
except self.s3tables.exceptions.ConflictException as e:
190+
raise CommitFailedException(
191+
f"Cannot create {namespace}.{table_name} because of a concurrent update to the table version {version_token}."
192+
) from e
193+
except:
194+
self.s3tables.delete_table(tableBucketARN=self.table_bucket_arn, namespace=namespace, name=table_name)
195+
raise
193196

194197
return self.load_table(identifier=identifier)
195198

0 commit comments

Comments
 (0)