Skip to content

Commit

Permalink
test console with sqlcatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjqliu committed Mar 13, 2024
1 parent 36a505f commit 5cf98c9
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 136 deletions.
2 changes: 1 addition & 1 deletion pyiceberg/catalog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def identifier_to_database_and_table(
) -> Tuple[str, str]:
tuple_identifier = Catalog.identifier_to_tuple(identifier)
if len(tuple_identifier) != 2:
raise err(f"Invalid path, hierarchical namespaces are not supported: {identifier}")
raise err(f"Table does not exist: {tuple_identifier}")

return tuple_identifier[0], tuple_identifier[1]

Expand Down
20 changes: 14 additions & 6 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def create_table(

database_name, table_name = self.identifier_to_database_and_table(identifier)
if not self._namespace_exists(database_name):
raise NoSuchNamespaceError(f"Namespace does not exist: {database_name}")
self.create_namespace(database_name)

location = self._resolve_table_location(location, database_name, table_name)
metadata_location = self._get_metadata_location(location=location)
Expand Down Expand Up @@ -263,7 +263,7 @@ def load_table(self, identifier: Union[str, Identifier]) -> Table:
result = session.scalar(stmt)
if result:
return self._convert_orm_to_iceberg(result)
raise NoSuchTableError(f"Table does not exist: {database_name}.{table_name}")
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}")

def drop_table(self, identifier: Union[str, Identifier]) -> None:
"""Drop a table.
Expand All @@ -286,7 +286,7 @@ def drop_table(self, identifier: Union[str, Identifier]) -> None:
)
)
if res.rowcount < 1:
raise NoSuchTableError(f"Table does not exist: {database_name}.{table_name}")
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}")
else:
try:
tbl = (
Expand All @@ -301,7 +301,7 @@ def drop_table(self, identifier: Union[str, Identifier]) -> None:
)
session.delete(tbl)
except NoResultFound as e:
raise NoSuchTableError(f"Table does not exist: {database_name}.{table_name}") from e
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}") from e
session.commit()

def rename_table(self, from_identifier: Union[str, Identifier], to_identifier: Union[str, Identifier]) -> Table:
Expand Down Expand Up @@ -501,6 +501,8 @@ def drop_namespace(self, namespace: Union[str, Identifier]) -> None:
)
)
session.commit()
else:
raise NoSuchNamespaceError(f"Namespace does not exist: {self.identifier_to_tuple(namespace)}")

def list_tables(self, namespace: Union[str, Identifier]) -> List[Identifier]:
"""List tables under the given namespace in the catalog.
Expand Down Expand Up @@ -537,6 +539,10 @@ def list_namespaces(self, namespace: Union[str, Identifier] = ()) -> List[Identi
Raises:
NoSuchNamespaceError: If a namespace with the given name does not exist.
"""
# Hierarchical namespace is not supported. Return an empty list
if namespace:
return []

if namespace and not self._namespace_exists(namespace):
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}")

Expand Down Expand Up @@ -573,7 +579,9 @@ def load_namespace_properties(self, namespace: Union[str, Identifier]) -> Proper
IcebergNamespaceProperties.catalog_name == self.name, IcebergNamespaceProperties.namespace == database_name
)
with Session(self.engine) as session:
result = session.scalars(stmt)
result = list(session.scalars(stmt))
if len(result) == 0:
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}")
return {props.property_key: props.property_value for props in result}

def update_namespace_properties(
Expand All @@ -592,7 +600,7 @@ def update_namespace_properties(
"""
database_name = self.identifier_to_database(namespace)
if not self._namespace_exists(database_name):
raise NoSuchNamespaceError(f"Database {database_name} does not exists")
raise NoSuchNamespaceError(f"Namespace does not exist: {self.identifier_to_tuple(namespace)}")

current_properties = self.load_namespace_properties(namespace=namespace)
properties_update_summary = self._get_updated_props_and_update_summary(
Expand Down
Loading

0 comments on commit 5cf98c9

Please sign in to comment.