|
29 | 29 | TYPE_CHECKING, |
30 | 30 | Any, |
31 | 31 | Callable, |
| 32 | + ClassVar, |
32 | 33 | Dict, |
33 | 34 | Iterable, |
34 | 35 | Iterator, |
35 | 36 | List, |
| 37 | + Sequence, |
36 | 38 | Set, |
37 | 39 | Tuple, |
38 | 40 | Type, |
@@ -1025,20 +1027,70 @@ def commit_transaction(self) -> Table: |
1025 | 1027 | return self._table |
1026 | 1028 |
|
1027 | 1029 |
|
1028 | | -class Namespace(IcebergRootModel[List[str]]): |
| 1030 | +class Namespace(IcebergRootModel[Tuple[str, ...]]): |
1029 | 1031 | """Reference to one or more levels of a namespace.""" |
1030 | 1032 |
|
1031 | | - root: List[str] = Field( |
| 1033 | + root: Tuple[str, ...] = Field( |
1032 | 1034 | ..., |
1033 | 1035 | description="Reference to one or more levels of a namespace", |
1034 | 1036 | ) |
1035 | 1037 |
|
| 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 | + |
1036 | 1058 |
|
1037 | 1059 | class TableIdentifier(IcebergBaseModel): |
1038 | 1060 | """Fully Qualified identifier to a table.""" |
1039 | 1061 |
|
1040 | 1062 | namespace: Namespace |
1041 | 1063 | 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) |
1042 | 1094 |
|
1043 | 1095 |
|
1044 | 1096 | class CommitTableRequest(IcebergBaseModel): |
|
0 commit comments