Skip to content

Commit d1bc236

Browse files
committed
Refactor Namespace and TableIdentifier classes
improved type handling and added utility methods
1 parent d5448c3 commit d1bc236

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-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: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929
TYPE_CHECKING,
3030
Any,
3131
Callable,
32+
ClassVar,
3233
Iterable,
3334
Iterator,
35+
List,
36+
Set,
37+
Tuple,
38+
Type,
3439
TypeVar,
3540
)
3641

@@ -1020,20 +1025,70 @@ def commit_transaction(self) -> Table:
10201025
return self._table
10211026

10221027

1023-
class Namespace(IcebergRootModel[list[str]]):
1028+
class Namespace(IcebergRootModel[tuple[str, ...]]):
10241029
"""Reference to one or more levels of a namespace."""
10251030

1026-
root: list[str] = Field(
1031+
root: tuple[str, ...] = Field(
10271032
...,
10281033
description="Reference to one or more levels of a namespace",
10291034
)
10301035

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

10321057
class TableIdentifier(IcebergBaseModel):
10331058
"""Fully Qualified identifier to a table."""
10341059

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

10381093

10391094
class CommitTableRequest(IcebergBaseModel):

0 commit comments

Comments
 (0)