rdblue commented on a change in pull request #4318: URL: https://github.com/apache/iceberg/pull/4318#discussion_r841250975
########## File path: python/src/iceberg/table/schema.py ########## @@ -0,0 +1,286 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Dict, Generic, Iterable, List, TypeVar + +from iceberg.types import ( + IcebergType, + ListType, + MapType, + NestedField, + PrimitiveType, + StructType, +) + +T = TypeVar("T") + + +class Schema: + """A table Schema""" + + def __init__(self, *columns: Iterable[NestedField], schema_id: int, identifier_field_ids: List[int] = []): + self._struct = StructType(*columns) # type: ignore + self._schema_id = schema_id + self._identifier_field_ids = identifier_field_ids + self._name_index: Dict[str, int] = index_by_name(self) + self._id_index: Dict[int, NestedField] = {} # Will be lazily set when self.id_index property method is called + + def __str__(self): + return "table { \n" + "\n".join([" " + str(field) for field in self.columns]) + "\n }" + + def __repr__(self): + return f"Schema(fields={repr(self.columns)})" + + @property + def columns(self) -> Iterable[NestedField]: + return self._struct.fields + + @property + def id(self) -> int: + return self._schema_id + + @property + def identifier_field_ids(self) -> List[int]: + return self._identifier_field_ids + + @property + def id_index(self) -> Dict[int, NestedField]: + if not self._id_index: + self._id_index = index_by_id(self) + return self._id_index + + @property + def name_index(self) -> Dict[str, int]: + return self._name_index + + def as_struct(self) -> StructType: + return self._struct + + def find_field(self, name_or_id: str | int, case_sensitive: bool = True) -> NestedField: + if isinstance(name_or_id, int): + return self.id_index[name_or_id] + if case_sensitive: + field_id = self.name_index[name_or_id] + else: + name_index_lower = {name.lower(): field_id for name, field_id in self.name_index.items()} + field_id = name_index_lower[name_or_id.lower()] + return self.id_index[field_id] + + def find_type(self, name_or_id: str | int, case_sensitive: bool = True) -> IcebergType: + if isinstance(name_or_id, int): + return self.id_index[name_or_id].type + if case_sensitive: + field_id = self.name_index[name_or_id] + else: + name_index_lower = {name.lower(): field_id for name, field_id in self.name_index.items()} + field_id = name_index_lower[name_or_id.lower()] + return self.id_index[field_id].type + + def find_column_name(self, column_id: int) -> str: + matched_column = [name for name, field_id in self.name_index.items() if field_id == column_id] + if not matched_column: + raise ValueError(f"Cannot find column name: {column_id}") Review comment: This should return `None` if there is column with the given ID. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
