|
29 | 29 | TYPE_CHECKING, |
30 | 30 | Any, |
31 | 31 | Callable, |
| 32 | + ClassVar, |
32 | 33 | Iterable, |
33 | 34 | Iterator, |
| 35 | + List, |
| 36 | + Set, |
| 37 | + Tuple, |
| 38 | + Type, |
34 | 39 | TypeVar, |
35 | 40 | ) |
36 | 41 |
|
@@ -1020,20 +1025,70 @@ def commit_transaction(self) -> Table: |
1020 | 1025 | return self._table |
1021 | 1026 |
|
1022 | 1027 |
|
1023 | | -class Namespace(IcebergRootModel[list[str]]): |
| 1028 | +class Namespace(IcebergRootModel[tuple[str, ...]]): |
1024 | 1029 | """Reference to one or more levels of a namespace.""" |
1025 | 1030 |
|
1026 | | - root: list[str] = Field( |
| 1031 | + root: tuple[str, ...] = Field( |
1027 | 1032 | ..., |
1028 | 1033 | description="Reference to one or more levels of a namespace", |
1029 | 1034 | ) |
1030 | 1035 |
|
| 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 | + |
1031 | 1056 |
|
1032 | 1057 | class TableIdentifier(IcebergBaseModel): |
1033 | 1058 | """Fully Qualified identifier to a table.""" |
1034 | 1059 |
|
1035 | 1060 | namespace: Namespace |
1036 | 1061 | 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) |
1037 | 1092 |
|
1038 | 1093 |
|
1039 | 1094 | class CommitTableRequest(IcebergBaseModel): |
|
0 commit comments