Skip to content

Commit 52d7283

Browse files
committed
Refactor Namespace and TableIdentifier classes
improved type handling and added utility methods
1 parent f83fb5e commit 52d7283

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-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: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
TYPE_CHECKING,
3030
Any,
3131
Callable,
32+
ClassVar,
3233
Dict,
3334
Iterable,
35+
Iterator,
3436
List,
3537
Optional,
38+
Sequence,
3639
Set,
3740
Tuple,
3841
Type,
@@ -1028,20 +1031,70 @@ def commit_transaction(self) -> Table:
10281031
return self._table
10291032

10301033

1031-
class Namespace(IcebergRootModel[List[str]]):
1034+
class Namespace(IcebergRootModel[Tuple[str, ...]]):
10321035
"""Reference to one or more levels of a namespace."""
10331036

1034-
root: List[str] = Field(
1037+
root: Tuple[str, ...] = Field(
10351038
...,
10361039
description="Reference to one or more levels of a namespace",
10371040
)
10381041

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

10401063
class TableIdentifier(IcebergBaseModel):
10411064
"""Fully Qualified identifier to a table."""
10421065

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

10461099

10471100
class CommitTableRequest(IcebergBaseModel):

0 commit comments

Comments
 (0)