Skip to content

Commit c6a1a0c

Browse files
committed
refactor hive
1 parent 1534869 commit c6a1a0c

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

pyiceberg/catalog/hive.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ def _convert_iceberg_into_hive(self, table: Table) -> HiveTable:
293293
parameters=_construct_parameters(table.metadata_location),
294294
)
295295

296+
def _create_hive_table(self, open_client: Client, hive_table: HiveTable) -> None:
297+
try:
298+
open_client.create_table(hive_table)
299+
except AlreadyExistsException as e:
300+
raise TableAlreadyExistsError(f"Table {hive_table.dbName}.{hive_table.tableName} already exists") from e
301+
296302
def create_table(
297303
self,
298304
identifier: Union[str, Identifier],
@@ -332,12 +338,10 @@ def create_table(
332338

333339
self._write_metadata(staged_table.metadata, staged_table.io, staged_table.metadata_location)
334340
tbl = self._convert_iceberg_into_hive(staged_table)
335-
try:
336-
with self._client as open_client:
337-
open_client.create_table(tbl)
338-
hive_table = open_client.get_table(dbname=database_name, tbl_name=table_name)
339-
except AlreadyExistsException as e:
340-
raise TableAlreadyExistsError(f"Table {database_name}.{table_name} already exists") from e
341+
342+
with self._client as open_client:
343+
self._create_hive_table(open_client, tbl)
344+
hive_table = open_client.get_table(dbname=database_name, tbl_name=table_name)
341345

342346
return self._convert_hive_into_iceberg(hive_table, staged_table.io)
343347

@@ -420,11 +424,7 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons
420424
new_metadata_version = 0
421425
new_metadata_location = self._get_metadata_location(updated_metadata.location, new_metadata_version)
422426
io = self._load_file_io(updated_metadata.properties, new_metadata_location)
423-
self._write_metadata(
424-
updated_metadata,
425-
io,
426-
new_metadata_location,
427-
)
427+
self._write_metadata(updated_metadata, io, new_metadata_location)
428428

429429
tbl = self._convert_iceberg_into_hive(
430430
StagedTable(
@@ -435,10 +435,7 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons
435435
catalog=self,
436436
)
437437
)
438-
try:
439-
open_client.create_table(tbl)
440-
except AlreadyExistsException as e:
441-
raise TableAlreadyExistsError(f"Table {database_name}.{table_name} already exists") from e
438+
self._create_hive_table(open_client, tbl)
442439
finally:
443440
open_client.unlock(UnlockRequest(lockid=lock.lockid))
444441

tests/integration/test_writes/test_writes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def test_create_table_transaction(catalog: Catalog, format_version: int) -> None
599599
"There is a bug in the REST catalog (maybe server side) that prevents create and commit a staged version 1 table"
600600
)
601601

602-
identifier = f"default.arrow_create_table_transaction{format_version}"
602+
identifier = f"default.arrow_create_table_transaction_{catalog.name}_{format_version}"
603603

604604
try:
605605
catalog.drop_table(identifier=identifier)

0 commit comments

Comments
 (0)