samredai commented on a change in pull request #4318: URL: https://github.com/apache/iceberg/pull/4318#discussion_r837525040
########## File path: python/src/iceberg/table/schema.py ########## @@ -0,0 +1,215 @@ +# 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 abc import ABC, abstractmethod +from typing import Dict, Generic, Iterable, List, TypeVar, Union + +from iceberg.types import ( + IcebergType, + ListType, + MapType, + NestedField, + PrimitiveType, + StructType, +) + +T = TypeVar("T") + + +class Schema: + """A table Schema""" + + def __init__(self, *columns: Iterable[NestedField]): + self._struct = StructType(*columns) # type: ignore + + 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): + return self._struct.fields + + def as_struct(self): + return self._struct + + def _find_field_by_name(self, index: dict, name: str) -> NestedField: + matched_fields = [field for field_id, field in index.items() if field.name == name] + if not matched_fields: + raise ValueError("Cannot find field: {name_or_id}") + return matched_fields[0] + + def find_field(self, name_or_id: Union[str, int], case_sensitive: bool = True) -> NestedField: Review comment: It's been back-ported the earliest version we support but not enabled by default. We'd have to add `from __future__ import annotations` to the top of every file where we use it. I do like the newer syntax, let me know if the visual cost of the import feels worth it and I'll go ahead and update these. -- 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]
