ebyhr commented on code in PR #3717:
URL: https://github.com/apache/iceberg-python/pull/3717#discussion_r3661730497


##########
pyiceberg/catalog/hive.py:
##########
@@ -480,7 +481,28 @@ def register_table(self, identifier: str | Identifier, 
metadata_location: str, o
 
     @override
     def list_views(self, namespace: str | Identifier) -> list[Identifier]:
-        raise NotImplementedError
+        """List Iceberg views under the given namespace in the catalog.
+
+        When the database doesn't exist, it will just return an empty list.
+
+        Args:
+            namespace: Database to list.
+
+        Returns:
+            List[Identifier]: list of views identifiers.
+
+        Raises:
+            NoSuchNamespaceError: If a namespace with the given name does not 
exist, or the identifier is invalid.
+        """
+        database_name = self.identifier_to_database(namespace, 
NoSuchNamespaceError)
+        with self._client as open_client:
+            return [
+                (database_name, table.tableName)
+                for table in open_client.get_table_objects_by_name(
+                    dbname=database_name, 
tbl_names=open_client.get_all_tables(db_name=database_name)
+                )
+                if table.parameters.get(TABLE_TYPE, "").lower() == ICEBERG_VIEW

Review Comment:
   Getting all tables and filtering them at the caller's side is slow. We 
should consider providing a VIRTUAL_VIEW condition. However, follow-up is still 
acceptable. 



##########
pyiceberg/catalog/hive.py:
##########
@@ -480,7 +481,28 @@ def register_table(self, identifier: str | Identifier, 
metadata_location: str, o
 
     @override
     def list_views(self, namespace: str | Identifier) -> list[Identifier]:
-        raise NotImplementedError
+        """List Iceberg views under the given namespace in the catalog.
+
+        When the database doesn't exist, it will just return an empty list.

Review Comment:
   I think we should throw an exception if the database doesn't exist. 



##########
pyiceberg/catalog/hive.py:
##########
@@ -480,7 +481,28 @@ def register_table(self, identifier: str | Identifier, 
metadata_location: str, o
 
     @override
     def list_views(self, namespace: str | Identifier) -> list[Identifier]:
-        raise NotImplementedError
+        """List Iceberg views under the given namespace in the catalog.
+
+        When the database doesn't exist, it will just return an empty list.
+
+        Args:
+            namespace: Database to list.
+
+        Returns:
+            List[Identifier]: list of views identifiers.
+
+        Raises:
+            NoSuchNamespaceError: If a namespace with the given name does not 
exist, or the identifier is invalid.

Review Comment:
   ```
   When the database doesn't exist, it will just return an empty list.
   ...
   NoSuchNamespaceError: If a namespace with the given name does not exist
   ```
   
   These sentences appear contradictory.



##########
tests/catalog/test_hive.py:
##########
@@ -1144,6 +1144,38 @@ def test_create_database_already_exists() -> None:
     assert "Database default already exists" in str(exc_info.value)
 
 
+def test_list_views(hive_table: HiveTable) -> None:

Review Comment:
   Can we add an integration test? Create a view by Spark, and list it by 
PyIceberg. 



##########
tests/catalog/test_hive.py:
##########
@@ -1144,6 +1144,38 @@ def test_create_database_already_exists() -> None:
     assert "Database default already exists" in str(exc_info.value)
 
 
+def test_list_views(hive_table: HiveTable) -> None:
+    catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
+
+    tbl1 = deepcopy(hive_table)
+    tbl1.tableName = "table1"
+    tbl1.dbName = "database"
+    tbl1.parameters["table_type"] = "iceberg-view"
+    tbl2 = deepcopy(hive_table)
+    tbl2.tableName = "table2"
+    tbl2.dbName = "database"
+    tbl2.parameters["table_type"] = "iceberg-view"
+    tbl3 = deepcopy(hive_table)
+    tbl3.tableName = "table3"
+    tbl3.dbName = "database"
+    tbl3.parameters["table_type"] = "iceberg"
+    tbl4 = deepcopy(hive_table)
+    tbl4.tableName = "table4"
+    tbl4.dbName = "database"
+    tbl4.parameters.pop("table_type")
+
+    catalog._client = MagicMock()
+    catalog._client.__enter__().get_all_tables.return_value = ["table1", 
"table2", "table3", "table4"]
+    catalog._client.__enter__().get_table_objects_by_name.return_value = 
[tbl1, tbl2, tbl3, tbl4]
+
+    got_tables = catalog.list_views("database")
+    assert got_tables == [("database", "table1"), ("database", "table2")]
+    
catalog._client.__enter__().get_all_tables.assert_called_with(db_name="database")
+    catalog._client.__enter__().get_table_objects_by_name.assert_called_with(
+        dbname="database", tbl_names=["table1", "table2", "table3", "table4"]
+    )
+
+

Review Comment:
   I recommend adding a negative test - specifying a non-existing database. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to