Skip to content

Commit b496cbf

Browse files
committed
merge some upstream, add exception handler
1 parent db78726 commit b496cbf

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

pyiceberg/catalog/hive.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ def _get_hive_table(self, open_client: Client, *, dbname: str, tbl_name: str) ->
421421
except NoSuchObjectException as e:
422422
raise NoSuchTableError(f"Table does not exists: {tbl_name}") from e
423423

424-
425424
def _get_table_objects_by_name(self, open_client: Client, *, dbname: str, tbl_names: list[str]) -> list[HiveTable]:
426425
if all((self._client._hive_version.major >= 4, self._client._hive_version.minor > 0)):
427426
return open_client.get_table_objects_by_name_req(GetTablesRequest(dbName=dbname, tblNames=tbl_names)).tables
@@ -691,7 +690,7 @@ def rename_table(self, from_identifier: Union[str, Identifier], to_identifier: U
691690
to_database_name, to_table_name = self.identifier_to_database_and_table(to_identifier)
692691

693692
if self.table_exists(to_identifier):
694-
raise TableAlreadyExistsError(f"Table already exists: {to_table_name}")
693+
raise TableAlreadyExistsError(f"Table already exists: {to_table_name}")
695694

696695
try:
697696
with self._client as open_client:

tests/catalog/test_hive.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
NamespaceNotEmptyError,
6767
NoSuchNamespaceError,
6868
NoSuchTableError,
69+
TableAlreadyExistsError,
6970
WaitingForLockException,
7071
)
7172
from pyiceberg.partitioning import PartitionField, PartitionSpec
@@ -890,6 +891,7 @@ def test_load_table_from_self_identifier(hive_table: HiveTable) -> None:
890891

891892
def test_rename_table(hive_table: HiveTable) -> None:
892893
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
894+
catalog.table_exists = MagicMock(return_value=False) # type: ignore[method-assign]
893895

894896
renamed_table = copy.deepcopy(hive_table)
895897
renamed_table.dbName = "default"
@@ -920,6 +922,7 @@ def test_rename_table(hive_table: HiveTable) -> None:
920922

921923
def test_rename_table_from_self_identifier(hive_table: HiveTable) -> None:
922924
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
925+
catalog.table_exists = MagicMock(return_value=False) # type: ignore[method-assign]
923926

924927
catalog._client = MagicMock()
925928
catalog._get_hive_table = MagicMock(return_value=hive_table) # type: ignore
@@ -954,6 +957,7 @@ def test_rename_table_from_self_identifier(hive_table: HiveTable) -> None:
954957

955958
def test_rename_table_from_does_not_exists() -> None:
956959
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
960+
catalog.table_exists = MagicMock(return_value=False) # type: ignore[method-assign]
957961

958962
catalog._client = MagicMock()
959963
catalog._client._hive_version = HiveVersion(4, 0, 0)
@@ -967,8 +971,19 @@ def test_rename_table_from_does_not_exists() -> None:
967971
assert "Table does not exist: does_not_exists" in str(exc_info.value)
968972

969973

974+
def test_rename_table_to_table_already_exists(hive_table: HiveTable) -> None:
975+
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
976+
catalog.load_table = MagicMock(return_value=hive_table) # type: ignore[method-assign]
977+
978+
with pytest.raises(TableAlreadyExistsError) as exc_info:
979+
catalog.rename_table(("default", "some_table"), ("default", "new_tabl2e"))
980+
981+
assert "Table already exists: new_tabl2e" in str(exc_info.value)
982+
983+
970984
def test_rename_table_to_namespace_does_not_exists() -> None:
971985
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
986+
catalog.table_exists = MagicMock(return_value=False) # type: ignore[method-assign]
972987

973988
catalog._client = MagicMock()
974989
catalog._client._hive_version = HiveVersion(4, 0, 0)
@@ -1172,7 +1187,7 @@ def test_update_namespace_properties(hive_database: HiveDatabase) -> None:
11721187
name="default",
11731188
description=None,
11741189
locationUri=hive_database.locationUri,
1175-
parameters={"test": None, "label": "core"},
1190+
parameters={"label": "core"},
11761191
privileges=None,
11771192
ownerName=None,
11781193
ownerType=1,

0 commit comments

Comments
 (0)