27
27
28
28
import pytest
29
29
import thrift .transport .TSocket
30
- from hive_metastore .ttypes import (
30
+ from hive_metastore .v4 . ttypes import (
31
31
AlreadyExistsException ,
32
32
EnvironmentContext ,
33
33
FieldSchema ,
39
39
SerDeInfo ,
40
40
SkewedInfo ,
41
41
StorageDescriptor ,
42
+ Database as HiveDatabase ,
43
+ Table as HiveTable ,
42
44
)
43
- from hive_metastore .ttypes import Database as HiveDatabase
44
- from hive_metastore .ttypes import Table as HiveTable
45
45
46
46
from pyiceberg .catalog import PropertiesUpdateSummary
47
47
from pyiceberg .catalog .hive import (
@@ -254,6 +254,8 @@ def test_no_uri_supplied() -> None:
254
254
255
255
256
256
def test_check_number_of_namespaces (table_schema_simple : Schema ) -> None :
257
+ _HiveClient ._get_hive_version = MagicMock ()
258
+ _HiveClient ._get_hive_version .return_value = 3
257
259
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
258
260
259
261
with pytest .raises (ValueError ):
@@ -280,7 +282,8 @@ def test_create_table(
280
282
281
283
catalog ._client = MagicMock ()
282
284
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
284
287
catalog ._client .__enter__ ().get_database .return_value = hive_database
285
288
catalog .create_table (("default" , "table" ), schema = table_schema_with_all_types , properties = {"owner" : "javaberg" })
286
289
@@ -459,7 +462,8 @@ def test_create_table_with_given_location_removes_trailing_slash(
459
462
460
463
catalog ._client = MagicMock ()
461
464
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
463
467
catalog ._client .__enter__ ().get_database .return_value = hive_database
464
468
catalog .create_table (
465
469
("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
632
636
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
633
637
634
638
catalog ._client = MagicMock ()
639
+ catalog ._get_hive_table = MagicMock ()
635
640
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
637
642
catalog ._client .__enter__ ().get_database .return_value = hive_database
638
643
catalog .create_table (
639
644
("default" , "table" ), schema = table_schema_simple , properties = {"owner" : "javaberg" , "format-version" : "1" }
@@ -684,10 +689,11 @@ def test_load_table(hive_table: HiveTable) -> None:
684
689
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
685
690
686
691
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
688
694
table = catalog .load_table (("default" , "new_tabl2e" ))
689
695
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" )
691
697
692
698
expected = TableMetadataV2 (
693
699
location = "s3://bucket/test/location" ,
@@ -784,11 +790,12 @@ def test_load_table_from_self_identifier(hive_table: HiveTable) -> None:
784
790
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
785
791
786
792
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
788
795
intermediate = catalog .load_table (("default" , "new_tabl2e" ))
789
796
table = catalog .load_table (intermediate .name ())
790
797
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" )
792
799
793
800
expected = TableMetadataV2 (
794
801
location = "s3://bucket/test/location" ,
@@ -889,7 +896,8 @@ def test_rename_table(hive_table: HiveTable) -> None:
889
896
renamed_table .tableName = "new_tabl3e"
890
897
891
898
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 ]
893
901
catalog ._client .__enter__ ().alter_table_with_environment_context .return_value = None
894
902
895
903
from_identifier = ("default" , "new_tabl2e" )
@@ -898,8 +906,8 @@ def test_rename_table(hive_table: HiveTable) -> None:
898
906
899
907
assert table .name () == to_identifier
900
908
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 )
903
911
catalog ._client .__enter__ ().alter_table_with_environment_context .assert_called_with (
904
912
dbname = "default" ,
905
913
tbl_name = "new_tabl2e" ,
@@ -912,25 +920,26 @@ def test_rename_table_from_self_identifier(hive_table: HiveTable) -> None:
912
920
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
913
921
914
922
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
916
925
917
926
from_identifier = ("default" , "new_tabl2e" )
918
927
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" )
920
929
921
930
renamed_table = copy .deepcopy (hive_table )
922
931
renamed_table .dbName = "default"
923
932
renamed_table .tableName = "new_tabl3e"
924
933
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 ]
926
935
catalog ._client .__enter__ ().alter_table_with_environment_context .return_value = None
927
936
to_identifier = ("default" , "new_tabl3e" )
928
937
table = catalog .rename_table (from_table .name (), to_identifier )
929
938
930
939
assert table .name () == to_identifier
931
940
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 )
934
943
catalog ._client .__enter__ ().alter_table_with_environment_context .assert_called_with (
935
944
dbname = "default" ,
936
945
tbl_name = "new_tabl2e" ,
@@ -943,6 +952,7 @@ def test_rename_table_from_does_not_exists() -> None:
943
952
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
944
953
945
954
catalog ._client = MagicMock ()
955
+ catalog ._client .__enter__ ()._hive_version = 3
946
956
catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = NoSuchObjectException (
947
957
message = "hive.default.does_not_exists table not found"
948
958
)
@@ -957,6 +967,7 @@ def test_rename_table_to_namespace_does_not_exists() -> None:
957
967
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
958
968
959
969
catalog ._client = MagicMock ()
970
+ catalog ._client .__enter__ ()._hive_version = 3
960
971
catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = InvalidOperationException (
961
972
message = "Unable to change partition or table. Database default does not exist Check metastore logs for detailed stack.does_not_exists"
962
973
)
@@ -1013,13 +1024,14 @@ def test_list_tables(hive_table: HiveTable) -> None:
1013
1024
1014
1025
catalog ._client = MagicMock ()
1015
1026
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 ]
1017
1029
1018
1030
got_tables = catalog .list_tables ("database" )
1019
1031
assert got_tables == [("database" , "table1" ), ("database" , "table2" )]
1020
1032
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" ]
1023
1035
)
1024
1036
1025
1037
@@ -1049,7 +1061,8 @@ def test_drop_table_from_self_identifier(hive_table: HiveTable) -> None:
1049
1061
catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
1050
1062
1051
1063
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
1053
1066
table = catalog .load_table (("default" , "new_tabl2e" ))
1054
1067
1055
1068
catalog ._client .__enter__ ().get_all_databases .return_value = ["namespace1" , "namespace2" ]
@@ -1156,7 +1169,7 @@ def test_update_namespace_properties(hive_database: HiveDatabase) -> None:
1156
1169
name = "default" ,
1157
1170
description = None ,
1158
1171
locationUri = hive_database .locationUri ,
1159
- parameters = {"label" : "core" },
1172
+ parameters = {"test" : None , " label" : "core" },
1160
1173
privileges = None ,
1161
1174
ownerName = None ,
1162
1175
ownerType = 1 ,
0 commit comments