Skip to content

Commit c8cf40a

Browse files
committed
Refactor Namespace and TableIdentifier classes
improved type handling and added utility methods
1 parent 0a94a96 commit c8cf40a

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

mkdocs/docs/multi-part-namespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ tables = catalog.list_tables("default.multi")
2828

2929
## Configuration
3030

31-
When using catalogs that support multi-part namespaces, make sure to use the appropriate delimiter (typically `.`) when referencing namespaces in your code.
31+
When using catalogs that support multi-part namespaces, make sure to use the appropriate delimiter (typically `.`) when referencing namespaces in your code.

pyiceberg/table/__init__.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
TYPE_CHECKING,
3030
Any,
3131
Callable,
32+
ClassVar,
3233
Dict,
3334
Iterable,
3435
Iterator,
3536
List,
37+
Sequence,
3638
Set,
3739
Tuple,
3840
Type,
@@ -1025,20 +1027,70 @@ def commit_transaction(self) -> Table:
10251027
return self._table
10261028

10271029

1028-
class Namespace(IcebergRootModel[List[str]]):
1030+
class Namespace(IcebergRootModel[Tuple[str, ...]]):
10291031
"""Reference to one or more levels of a namespace."""
10301032

1031-
root: List[str] = Field(
1033+
root: Tuple[str, ...] = Field(
10321034
...,
10331035
description="Reference to one or more levels of a namespace",
10341036
)
10351037

1038+
def __len__(self) -> int:
1039+
"""Fetch the size of Namespace."""
1040+
return len(self.root)
1041+
1042+
def __getitem__(self, index: int) -> str:
1043+
"""Fetch a value from a Namespace."""
1044+
return self.root[index]
1045+
1046+
def __iter__(self) -> Iterator[str]:
1047+
"""Return an iterator over the elements in the root of the table."""
1048+
return iter(self.root)
1049+
1050+
def levels(self) -> int:
1051+
"""Return the number of levels in this namespace."""
1052+
return len(self.root)
1053+
1054+
def __repr__(self) -> str:
1055+
"""Return a string representation of the namespace."""
1056+
return f"Namespace({self.root})"
1057+
10361058

10371059
class TableIdentifier(IcebergBaseModel):
10381060
"""Fully Qualified identifier to a table."""
10391061

10401062
namespace: Namespace
10411063
name: str
1064+
_separator: ClassVar[str] = "."
1065+
1066+
@classmethod
1067+
def from_string(cls, identifier: str) -> TableIdentifier:
1068+
"""Create a TableIdentifier from a separator.
1069+
1070+
Args:
1071+
identifier: A separator representing the table identifier, e.g., "db.schema.table".
1072+
1073+
Returns:
1074+
A TableIdentifier instance.
1075+
"""
1076+
parts = identifier.split(cls._separator)
1077+
return cls.from_tuple(parts)
1078+
1079+
@classmethod
1080+
def from_tuple(cls, identifier: Sequence[str]) -> TableIdentifier:
1081+
"""Create a TableIdentifier from a tuple.
1082+
1083+
Args:
1084+
identifier: A tuple representing the table identifier, e.g., ("db", "schema", "table").
1085+
1086+
Returns:
1087+
A TableIdentifier instance.
1088+
"""
1089+
if len(identifier) < 2:
1090+
raise ValueError("Identifier must include at least a namespace and a table name.")
1091+
namespace = Namespace(root=tuple(identifier[:-1]))
1092+
name = identifier[-1]
1093+
return cls(namespace=namespace, name=name)
10421094

10431095

10441096
class CommitTableRequest(IcebergBaseModel):

0 commit comments

Comments
 (0)