Skip to content

Commit 273e4ba

Browse files
author
Wenzhuo Zhao
committed
Ignore tables without table_type parameters while loading all iceberg table from Glue and Hive catalog (#1331)
1 parent de976fe commit 273e4ba

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

pyiceberg/catalog/glue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,4 +784,4 @@ def drop_view(self, identifier: Union[str, Identifier]) -> None:
784784

785785
@staticmethod
786786
def __is_iceberg_table(table: TableTypeDef) -> bool:
787-
return table["Parameters"] is not None and table["Parameters"][TABLE_TYPE].lower() == ICEBERG
787+
return table["Parameters"] is not None and TABLE_TYPE in table["Parameters"] and table["Parameters"][TABLE_TYPE].lower() == ICEBERG

pyiceberg/catalog/hive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def list_tables(self, namespace: Union[str, Identifier]) -> List[Identifier]:
651651
for table in open_client.get_table_objects_by_name(
652652
dbname=database_name, tbl_names=open_client.get_all_tables(db_name=database_name)
653653
)
654-
if table.parameters[TABLE_TYPE].lower() == ICEBERG
654+
if TABLE_TYPE in table.parameters and table.parameters[TABLE_TYPE].lower() == ICEBERG
655655
]
656656

657657
def list_namespaces(self, namespace: Union[str, Identifier] = ()) -> List[Identifier]:

tests/catalog/test_glue.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ def test_list_tables(
450450
test_catalog.create_namespace(namespace=database_name)
451451

452452
non_iceberg_table_name = "non_iceberg_table"
453+
non_table_type_table_name = "non_table_type_table"
453454
glue_client = boto3.client("glue", endpoint_url=moto_endpoint_url)
454455
glue_client.create_table(
455456
DatabaseName=database_name,
@@ -459,12 +460,21 @@ def test_list_tables(
459460
"Parameters": {"table_type": "noniceberg"},
460461
},
461462
)
463+
glue_client.create_table(
464+
DatabaseName=database_name,
465+
TableInput={
466+
"Name": non_table_type_table_name,
467+
"TableType": "OTHER_TABLE_TYPE",
468+
"Parameters": {},
469+
},
470+
)
462471

463472
for table_name in table_list:
464473
test_catalog.create_table((database_name, table_name), table_schema_nested)
465474
loaded_table_list = test_catalog.list_tables(database_name)
466475

467476
assert (database_name, non_iceberg_table_name) not in loaded_table_list
477+
assert (database_name, non_table_type_table_name) not in loaded_table_list
468478
for table_name in table_list:
469479
assert (database_name, table_name) in loaded_table_list
470480

tests/catalog/test_hive.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,16 +919,20 @@ def test_list_tables(hive_table: HiveTable) -> None:
919919
tbl3.tableName = "table3"
920920
tbl3.dbName = "database"
921921
tbl3.parameters["table_type"] = "non_iceberg"
922+
tbl4 = deepcopy(hive_table)
923+
tbl4.tableName = "table4"
924+
tbl4.dbName = "database"
925+
tbl4.parameters.pop("table_type")
922926

923927
catalog._client = MagicMock()
924-
catalog._client.__enter__().get_all_tables.return_value = ["table1", "table2", "table3"]
925-
catalog._client.__enter__().get_table_objects_by_name.return_value = [tbl1, tbl2, tbl3]
928+
catalog._client.__enter__().get_all_tables.return_value = ["table1", "table2", "table3", "table4"]
929+
catalog._client.__enter__().get_table_objects_by_name.return_value = [tbl1, tbl2, tbl3, tbl4]
926930

927931
got_tables = catalog.list_tables("database")
928932
assert got_tables == [("database", "table1"), ("database", "table2")]
929933
catalog._client.__enter__().get_all_tables.assert_called_with(db_name="database")
930934
catalog._client.__enter__().get_table_objects_by_name.assert_called_with(
931-
dbname="database", tbl_names=["table1", "table2", "table3"]
935+
dbname="database", tbl_names=["table1", "table2", "table3", "table4"]
932936
)
933937

934938

0 commit comments

Comments
 (0)