Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pyiceberg/catalog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import re
import uuid
from abc import ABC, abstractmethod
from collections.abc import Callable
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from enum import Enum
from typing import (
Expand Down Expand Up @@ -581,42 +581,42 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
"""

@abstractmethod
def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
"""List tables under the given namespace in the catalog.

Args:
namespace (str | Identifier): Namespace identifier to search.

Returns:
List[Identifier]: list of table identifiers.
Iterator[Identifier]: iterator of table identifiers.

Raises:
NoSuchNamespaceError: If a namespace with the given name does not exist.
"""

@abstractmethod
def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
"""List namespaces from the given namespace. If not given, list top-level namespaces from the catalog.

Args:
namespace (str | Identifier): Namespace identifier to search.

Returns:
List[Identifier]: a List of namespace identifiers.
Iterator[Identifier]: iterator of namespace identifiers.

Raises:
NoSuchNamespaceError: If a namespace with the given name does not exist.
"""

@abstractmethod
def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
"""List views under the given namespace in the catalog.

Args:
namespace (str | Identifier): Namespace identifier to search.

Returns:
List[Identifier]: list of table identifiers.
Iterator[Identifier]: iterator of table identifiers.

Raises:
NoSuchNamespaceError: If a namespace with the given name does not exist.
Expand Down
11 changes: 6 additions & 5 deletions pyiceberg/catalog/bigquery_metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import json
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Union

from google.api_core.exceptions import NotFound
Expand Down Expand Up @@ -244,7 +245,7 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
except NotFound as e:
raise NoSuchNamespaceError(f"Namespace {namespace} does not exist.") from e

def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
database_name = self.identifier_to_database(namespace)
iceberg_tables: list[Identifier] = []
try:
Expand All @@ -256,17 +257,17 @@ def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
iceberg_tables.append((database_name, bq_table_list_item.table_id))
except NotFound:
raise NoSuchNamespaceError(f"Namespace (dataset) '{database_name}' not found.") from None
return iceberg_tables
yield from iceberg_tables

def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
# Since this catalog only supports one-level namespaces, it always returns an empty list unless
# passed an empty namespace to list all namespaces within the catalog.
if namespace:
raise NoSuchNamespaceError(f"Namespace (dataset) '{namespace}' not found.") from None

# List top-level datasets
datasets_iterator = self.client.list_datasets()
return [(dataset.dataset_id,) for dataset in datasets_iterator]
yield from ((dataset.dataset_id,) for dataset in datasets_iterator)

def register_table(self, identifier: str | Identifier, metadata_location: str) -> Table:
"""Register a new table using existing metadata.
Expand Down Expand Up @@ -299,7 +300,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str) -

return self.load_table(identifier=identifier)

def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def drop_view(self, identifier: str | Identifier) -> None:
Expand Down
25 changes: 10 additions & 15 deletions pyiceberg/catalog/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import uuid
from collections.abc import Iterator
from time import time
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -384,7 +385,7 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
database_name = self.identifier_to_database(namespace, NoSuchNamespaceError)
table_identifiers = self.list_tables(namespace=database_name)

if len(table_identifiers) > 0:
if len(list(table_identifiers)) > 0:
raise NamespaceNotEmptyError(f"Database {database_name} is not empty")

try:
Expand All @@ -396,14 +397,14 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
except ConditionalCheckFailedException as e:
raise NoSuchNamespaceError(f"Database does not exist: {database_name}") from e

def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
"""List Iceberg tables under the given namespace in the catalog.

Args:
namespace (str | Identifier): Namespace identifier to search.

Returns:
List[Identifier]: list of table identifiers.
Iterator[Identifier]: iterator of table identifiers.
"""
database_name = self.identifier_to_database(namespace, NoSuchNamespaceError)

Expand All @@ -428,29 +429,26 @@ def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
) as e:
raise GenericDynamoDbError(e.message) from e

table_identifiers = []
for page in page_iterator:
for item in page["Items"]:
_dict = _convert_dynamo_item_to_regular_dict(item)
identifier_col = _dict[DYNAMODB_COL_IDENTIFIER]
if identifier_col == DYNAMODB_NAMESPACE:
continue

table_identifiers.append(self.identifier_to_tuple(identifier_col))
yield self.identifier_to_tuple(identifier_col)

return table_identifiers

def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
"""List top-level namespaces from the catalog.

We do not support hierarchical namespace.

Returns:
List[Identifier]: a List of namespace identifiers.
Iterator[Identifier]: iterator of namespace identifiers.
"""
# Hierarchical namespace is not supported. Return an empty list
if namespace:
return []
return

paginator = self.dynamodb.get_paginator("query")

Expand All @@ -473,14 +471,11 @@ def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
) as e:
raise GenericDynamoDbError(e.message) from e

database_identifiers = []
for page in page_iterator:
for item in page["Items"]:
_dict = _convert_dynamo_item_to_regular_dict(item)
namespace_col = _dict[DYNAMODB_COL_NAMESPACE]
database_identifiers.append(self.identifier_to_tuple(namespace_col))

return database_identifiers
yield self.identifier_to_tuple(namespace_col)

def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
"""
Expand Down Expand Up @@ -537,7 +532,7 @@ def update_namespace_properties(

return properties_update_summary

def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def drop_view(self, identifier: str | Identifier) -> None:
Expand Down
26 changes: 12 additions & 14 deletions pyiceberg/catalog/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.


from collections.abc import Iterator
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -92,7 +93,6 @@
from mypy_boto3_glue.type_defs import (
ColumnTypeDef,
DatabaseInputTypeDef,
DatabaseTypeDef,
StorageDescriptorTypeDef,
TableInputTypeDef,
TableTypeDef,
Expand Down Expand Up @@ -701,20 +701,19 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
)
self.glue.delete_database(Name=database_name)

def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
"""List Iceberg tables under the given namespace in the catalog.

Args:
namespace (str | Identifier): Namespace identifier to search.

Returns:
List[Identifier]: list of table identifiers.
Iterator[Identifier]: iterator of table 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)
table_list: list[TableTypeDef] = []
next_token: str | None = None
try:
while True:
Expand All @@ -723,37 +722,36 @@ def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
if not next_token
else self.glue.get_tables(DatabaseName=database_name, NextToken=next_token)
)
table_list.extend(table_list_response["TableList"])
for table in table_list_response["TableList"]:
if self.__is_iceberg_table(table):
yield (database_name, table["Name"])
next_token = table_list_response.get("NextToken")
if not next_token:
break

except self.glue.exceptions.EntityNotFoundException as e:
raise NoSuchNamespaceError(f"Database does not exist: {database_name}") from e
return [(database_name, table["Name"]) for table in table_list if self.__is_iceberg_table(table)]

def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
"""List namespaces from the given namespace. If not given, list top-level namespaces from the catalog.

Returns:
List[Identifier]: a List of namespace identifiers.
Iterator[Identifier]: iterator of namespace identifiers.
"""
# Hierarchical namespace is not supported. Return an empty list
if namespace:
return []
return

database_list: list[DatabaseTypeDef] = []
next_token: str | None = None

while True:
databases_response = self.glue.get_databases() if not next_token else self.glue.get_databases(NextToken=next_token)
database_list.extend(databases_response["DatabaseList"])
for database in databases_response["DatabaseList"]:
yield self.identifier_to_tuple(database["Name"])
next_token = databases_response.get("NextToken")
if not next_token:
break

return [self.identifier_to_tuple(database["Name"]) for database in database_list]

def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
"""Get properties for a namespace.

Expand Down Expand Up @@ -808,7 +806,7 @@ def update_namespace_properties(

return properties_update_summary

def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def drop_view(self, identifier: str | Identifier) -> None:
Expand Down
17 changes: 9 additions & 8 deletions pyiceberg/catalog/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import socket
import time
from collections.abc import Iterator
from types import TracebackType
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -464,7 +465,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str) -

return self._convert_hive_into_iceberg(hive_table)

def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def view_exists(self, identifier: str | Identifier) -> bool:
Expand Down Expand Up @@ -710,7 +711,7 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
except MetaException as e:
raise NoSuchNamespaceError(f"Database does not exists: {database_name}") from e

def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
"""List Iceberg tables under the given namespace in the catalog.

When the database doesn't exist, it will just return an empty list.
Expand All @@ -719,33 +720,33 @@ def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
namespace: Database to list.

Returns:
List[Identifier]: list of table identifiers.
Iterator[Identifier]: iterator of table 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 [
yield from [
(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
]

def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
"""List namespaces from the given namespace. If not given, list top-level namespaces from the catalog.

Returns:
List[Identifier]: a List of namespace identifiers.
Iterator[Identifier]: an iterator of namespace identifiers.
"""
# Hierarchical namespace is not supported. Return an empty list
if namespace:
return []
return iter([])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best not to use iter when not needed. In fact, we only need it for the list-tables of the REST Catalog. Does mypy allow to use list here? Same for line 748

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy says

  pyiceberg/catalog/hive.py:746: error: Incompatible return value type (got "list[Never]", expected "Iterator[tuple[str, ...]]")  [return-value]
  pyiceberg/catalog/hive.py:746: note: "list" is missing following "Iterator" protocol member:
  pyiceberg/catalog/hive.py:746: note:     __next__
  Found 1 error in 1 file (checked 166 source files)

Copy link
Contributor

@Fokko Fokko Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The reason I ask is because iter doesn't allow for len:

Python 3.14.1 (main, Dec  2 2025, 12:51:37) [Clang 17.0.0 (clang-1700.4.4.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> len(iter([1,2,3]))
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    len(iter([1,2,3]))
    ~~~^^^^^^^^^^^^^^^
TypeError: object of type 'list_iterator' has no len()
>>> len([1,2,3])
3


with self._client as open_client:
return list(map(self.identifier_to_tuple, open_client.get_all_databases()))
return map(self.identifier_to_tuple, open_client.get_all_databases())

def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
"""Get properties for a namespace.
Expand Down
7 changes: 4 additions & 3 deletions pyiceberg/catalog/noop.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from collections.abc import Iterator
from typing import (
TYPE_CHECKING,
Union,
Expand Down Expand Up @@ -102,10 +103,10 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties =
def drop_namespace(self, namespace: str | Identifier) -> None:
raise NotImplementedError

def list_tables(self, namespace: str | Identifier) -> list[Identifier]:
def list_tables(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
def list_namespaces(self, namespace: str | Identifier = ()) -> Iterator[Identifier]:
raise NotImplementedError

def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
Expand All @@ -116,7 +117,7 @@ def update_namespace_properties(
) -> PropertiesUpdateSummary:
raise NotImplementedError

def list_views(self, namespace: str | Identifier) -> list[Identifier]:
def list_views(self, namespace: str | Identifier) -> Iterator[Identifier]:
raise NotImplementedError

def view_exists(self, identifier: str | Identifier) -> bool:
Expand Down
Loading