samredai commented on a change in pull request #4318:
URL: https://github.com/apache/iceberg/pull/4318#discussion_r837538567



##########
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:
+        index = index_by_id(self)
+        if isinstance(name_or_id, int):
+            return index[name_or_id]
+        return self._find_field_by_name(index, name_or_id)
+
+    def find_type(self, name_or_id: Union[str, int], case_sensitive: bool = 
True) -> IcebergType:
+        index = index_by_id(self)
+        if isinstance(name_or_id, int):
+            return index[name_or_id].type
+        return self._find_field_by_name(index, name_or_id).type
+
+    def find_column_name(self, id: int) -> str:
+        index = index_by_id(self)
+        return index[id].name
+
+    def select(self, names: List[str], case_sensitive: bool = True) -> 
"Schema":
+        return (
+            self._case_sensitive_select(schema=self, names=names)
+            if case_sensitive
+            else self._case_insensitive_select(schema=self, names=names)
+        )
+
+    @classmethod
+    def _case_sensitive_select(cls, schema: "Schema", names: List[str]):
+        return cls(*[column for column in schema.columns if 
column.name.lower() in names])

Review comment:
       I noticed that the java select method returns a `StructType`. Should the 
python select method do the same? Currently the return type is `Schema`. For 
that matter, the argument type also has this same difference.




-- 
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]

Reply via email to