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