2727
2828import pytest
2929import thrift .transport .TSocket
30- from hive_metastore .ttypes import (
30+ from hive_metastore .v4 . ttypes import (
3131 AlreadyExistsException ,
3232 EnvironmentContext ,
3333 FieldSchema ,
3939 SerDeInfo ,
4040 SkewedInfo ,
4141 StorageDescriptor ,
42+ Database as HiveDatabase ,
43+ Table as HiveTable ,
4244)
43- from hive_metastore .ttypes import Database as HiveDatabase
44- from hive_metastore .ttypes import Table as HiveTable
4545
4646from pyiceberg .catalog import PropertiesUpdateSummary
4747from pyiceberg .catalog .hive import (
@@ -254,6 +254,8 @@ def test_no_uri_supplied() -> None:
254254
255255
256256def test_check_number_of_namespaces (table_schema_simple : Schema ) -> None :
257+ _HiveClient ._get_hive_version = MagicMock ()
258+ _HiveClient ._get_hive_version .return_value = 3
257259 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
258260
259261 with pytest .raises (ValueError ):
@@ -280,7 +282,8 @@ def test_create_table(
280282
281283 catalog ._client = MagicMock ()
282284 catalog ._client .__enter__ ().create_table .return_value = None
283- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [hive_table ]
285+ catalog ._get_hive_table = MagicMock ()
286+ catalog ._get_hive_table .return_value = hive_table
284287 catalog ._client .__enter__ ().get_database .return_value = hive_database
285288 catalog .create_table (("default" , "table" ), schema = table_schema_with_all_types , properties = {"owner" : "javaberg" })
286289
@@ -459,7 +462,8 @@ def test_create_table_with_given_location_removes_trailing_slash(
459462
460463 catalog ._client = MagicMock ()
461464 catalog ._client .__enter__ ().create_table .return_value = None
462- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [hive_table ]
465+ catalog ._get_hive_table = MagicMock ()
466+ catalog ._get_hive_table .return_value = hive_table
463467 catalog ._client .__enter__ ().get_database .return_value = hive_database
464468 catalog .create_table (
465469 ("default" , "table" ), schema = table_schema_with_all_types , properties = {"owner" : "javaberg" }, location = f"{ location } /"
@@ -632,8 +636,9 @@ def test_create_v1_table(table_schema_simple: Schema, hive_database: HiveDatabas
632636 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
633637
634638 catalog ._client = MagicMock ()
639+ catalog ._get_hive_table = MagicMock ()
635640 catalog ._client .__enter__ ().create_table .return_value = None
636- catalog ._client . __enter__ (). get_table_objects_by_name . return_value = [ hive_table ]
641+ catalog ._get_hive_table . return_value = hive_table
637642 catalog ._client .__enter__ ().get_database .return_value = hive_database
638643 catalog .create_table (
639644 ("default" , "table" ), schema = table_schema_simple , properties = {"owner" : "javaberg" , "format-version" : "1" }
@@ -684,10 +689,11 @@ def test_load_table(hive_table: HiveTable) -> None:
684689 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
685690
686691 catalog ._client = MagicMock ()
687- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [hive_table ]
692+ catalog ._get_hive_table = MagicMock ()
693+ catalog ._get_hive_table .return_value = hive_table
688694 table = catalog .load_table (("default" , "new_tabl2e" ))
689695
690- catalog ._client . __enter__ (). get_table_objects_by_name . assert_called_with ( dbname = "default" , tbl_names = [ "new_tabl2e" ] )
696+ catalog ._get_hive_table . assert_called_with ( catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl2e" )
691697
692698 expected = TableMetadataV2 (
693699 location = "s3://bucket/test/location" ,
@@ -784,11 +790,12 @@ def test_load_table_from_self_identifier(hive_table: HiveTable) -> None:
784790 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
785791
786792 catalog ._client = MagicMock ()
787- catalog ._client .__enter__ ().get_table_objects_by_name .side_effect = lambda dbname , tbl_names : [hive_table ]
793+ catalog ._get_hive_table = MagicMock ()
794+ catalog ._get_hive_table .return_value = hive_table
788795 intermediate = catalog .load_table (("default" , "new_tabl2e" ))
789796 table = catalog .load_table (intermediate .name ())
790797
791- catalog ._client . __enter__ (). get_table_objects_by_name . assert_called_with ( dbname = "default" , tbl_names = [ "new_tabl2e" ] )
798+ catalog ._get_hive_table . assert_called_with ( catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl2e" )
792799
793800 expected = TableMetadataV2 (
794801 location = "s3://bucket/test/location" ,
@@ -889,7 +896,8 @@ def test_rename_table(hive_table: HiveTable) -> None:
889896 renamed_table .tableName = "new_tabl3e"
890897
891898 catalog ._client = MagicMock ()
892- catalog ._client .__enter__ ().get_table_objects_by_name .side_effect = [[hive_table ], [renamed_table ]]
899+ catalog ._get_hive_table = MagicMock ()
900+ catalog ._get_hive_table .side_effect = [hive_table , renamed_table ]
893901 catalog ._client .__enter__ ().alter_table_with_environment_context .return_value = None
894902
895903 from_identifier = ("default" , "new_tabl2e" )
@@ -898,8 +906,8 @@ def test_rename_table(hive_table: HiveTable) -> None:
898906
899907 assert table .name () == to_identifier
900908
901- calls = [call (dbname = "default" , tbl_names = [ "new_tabl2e" ] ), call (dbname = "default" , tbl_names = [ "new_tabl3e" ] )]
902- catalog ._client . __enter__ (). get_table_objects_by_name .assert_has_calls (calls )
909+ calls = [call (catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl2e" ), call (catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl3e" )]
910+ catalog ._get_hive_table .assert_has_calls (calls )
903911 catalog ._client .__enter__ ().alter_table_with_environment_context .assert_called_with (
904912 dbname = "default" ,
905913 tbl_name = "new_tabl2e" ,
@@ -912,25 +920,26 @@ def test_rename_table_from_self_identifier(hive_table: HiveTable) -> None:
912920 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
913921
914922 catalog ._client = MagicMock ()
915- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [hive_table ]
923+ catalog ._get_hive_table = MagicMock ()
924+ catalog ._get_hive_table .return_value = hive_table
916925
917926 from_identifier = ("default" , "new_tabl2e" )
918927 from_table = catalog .load_table (from_identifier )
919- catalog ._client . __enter__ (). get_table_objects_by_name . assert_called_with ( dbname = "default" , tbl_names = [ "new_tabl2e" ] )
928+ catalog ._get_hive_table . assert_called_with ( catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl2e" )
920929
921930 renamed_table = copy .deepcopy (hive_table )
922931 renamed_table .dbName = "default"
923932 renamed_table .tableName = "new_tabl3e"
924933
925- catalog ._client . __enter__ (). get_table_objects_by_name . side_effect = [[ hive_table ], [ renamed_table ] ]
934+ catalog ._get_hive_table . side_effect = [hive_table , renamed_table ]
926935 catalog ._client .__enter__ ().alter_table_with_environment_context .return_value = None
927936 to_identifier = ("default" , "new_tabl3e" )
928937 table = catalog .rename_table (from_table .name (), to_identifier )
929938
930939 assert table .name () == to_identifier
931940
932- calls = [call (dbname = "default" , tbl_names = [ "new_tabl2e" ] ), call (dbname = "default" , tbl_names = [ "new_tabl3e" ] )]
933- catalog ._client . __enter__ (). get_table_objects_by_name .assert_has_calls (calls )
941+ calls = [call (catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl2e" ), call (catalog . _client . __enter__ (), dbname = "default" , tbl_name = "new_tabl3e" )]
942+ catalog ._get_hive_table .assert_has_calls (calls )
934943 catalog ._client .__enter__ ().alter_table_with_environment_context .assert_called_with (
935944 dbname = "default" ,
936945 tbl_name = "new_tabl2e" ,
@@ -943,6 +952,7 @@ def test_rename_table_from_does_not_exists() -> None:
943952 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
944953
945954 catalog ._client = MagicMock ()
955+ catalog ._client .__enter__ ()._hive_version = 3
946956 catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = NoSuchObjectException (
947957 message = "hive.default.does_not_exists table not found"
948958 )
@@ -957,6 +967,7 @@ def test_rename_table_to_namespace_does_not_exists() -> None:
957967 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
958968
959969 catalog ._client = MagicMock ()
970+ catalog ._client .__enter__ ()._hive_version = 3
960971 catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = InvalidOperationException (
961972 message = "Unable to change partition or table. Database default does not exist Check metastore logs for detailed stack.does_not_exists"
962973 )
@@ -1013,13 +1024,14 @@ def test_list_tables(hive_table: HiveTable) -> None:
10131024
10141025 catalog ._client = MagicMock ()
10151026 catalog ._client .__enter__ ().get_all_tables .return_value = ["table1" , "table2" , "table3" , "table4" ]
1016- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [tbl1 , tbl2 , tbl3 , tbl4 ]
1027+ catalog ._get_table_objects_by_name = MagicMock ()
1028+ catalog ._get_table_objects_by_name .return_value = [tbl1 , tbl2 , tbl3 , tbl4 ]
10171029
10181030 got_tables = catalog .list_tables ("database" )
10191031 assert got_tables == [("database" , "table1" ), ("database" , "table2" )]
10201032 catalog ._client .__enter__ ().get_all_tables .assert_called_with (db_name = "database" )
1021- catalog ._client . __enter__ (). get_table_objects_by_name .assert_called_with (
1022- dbname = "database" , tbl_names = ["table1" , "table2" , "table3" , "table4" ]
1033+ catalog ._get_table_objects_by_name .assert_called_with (
1034+ catalog . _client . __enter__ (), dbname = "database" , tbl_names = ["table1" , "table2" , "table3" , "table4" ]
10231035 )
10241036
10251037
@@ -1049,7 +1061,8 @@ def test_drop_table_from_self_identifier(hive_table: HiveTable) -> None:
10491061 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
10501062
10511063 catalog ._client = MagicMock ()
1052- catalog ._client .__enter__ ().get_table_objects_by_name .return_value = [hive_table ]
1064+ catalog ._get_hive_table = MagicMock ()
1065+ catalog ._get_hive_table .return_value = hive_table
10531066 table = catalog .load_table (("default" , "new_tabl2e" ))
10541067
10551068 catalog ._client .__enter__ ().get_all_databases .return_value = ["namespace1" , "namespace2" ]
@@ -1156,7 +1169,7 @@ def test_update_namespace_properties(hive_database: HiveDatabase) -> None:
11561169 name = "default" ,
11571170 description = None ,
11581171 locationUri = hive_database .locationUri ,
1159- parameters = {"label" : "core" },
1172+ parameters = {"test" : None , " label" : "core" },
11601173 privileges = None ,
11611174 ownerName = None ,
11621175 ownerType = 1 ,
0 commit comments