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



##########
File path: python/src/iceberg/table/schema.py
##########
@@ -0,0 +1,346 @@
+# 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.
+
+import sys
+from typing import Iterable, Tuple
+
+if sys.version_info >= (3, 8):  # pragma: no cover
+    from functools import singledispatchmethod
+    from typing import Protocol
+else:  # pragma: no cover
+    from typing_extensions import Protocol  # type: ignore
+    from singledispatch import singledispatchmethod  # type: ignore
+
+from iceberg.types import IcebergType, ListType, MapType, NestedField, 
StructType
+
+
+class SchemaVisitor(Protocol):
+    def visit(self, node) -> None:  # pragma: no cover
+        ...
+
+
+class Schema:
+    """Schema of a table
+
+    Example:
+        >>> from iceberg.table.schema import Schema
+        >>> from iceberg.types import BooleanType, IntegerType, NestedField, 
StringType
+        >>> fields = [ \
+            NestedField(field_id=1, name="foo", field_type=StringType(), 
is_optional=False), \
+            NestedField(field_id=2, name="bar", field_type=IntegerType(), 
is_optional=True), \
+            NestedField(field_id=3, name="baz", field_type=BooleanType(), 
is_optional=False), \
+        ]
+        >>> table_schema = Schema(fields=fields, schema_id=1, aliases={"qux": 
3})
+        >>> print(table_schema)
+        1: name=foo, type=string, required=True
+        2: name=bar, type=int, required=False
+        3: name=baz, type=boolean, required=True
+    """
+
+    def __init__(self, fields: Iterable[NestedField], schema_id: int, aliases: 
dict = {}):
+        self._struct = StructType(*fields)
+        self._schema_id = schema_id
+        self._aliases = aliases
+
+        index_by_id_visitor = IndexById()
+        index_by_id_visitor.visit(self._struct)
+        self._index_by_id = index_by_id_visitor.result
+
+        index_by_name_visitor = IndexByName()
+        index_by_name_visitor.visit(self._struct)
+        self._index_by_name = index_by_name_visitor.result
+
+    def __len__(self):
+        return len(self.struct.fields)
+
+    def __str__(self):
+        schema_str = ""
+        for field in self.fields:
+            schema_str += f"{field.field_id}: name={field.name}, 
type={field.type}, required={field.is_required}\n"
+        return schema_str.rstrip()
+
+    def __repr__(self):
+        return f"Schema(fields={repr(self.fields)}, 
schema_id={self.schema_id})"
+
+    @property
+    def fields(self) -> Tuple:
+        return self._struct.fields
+
+    @property
+    def schema_id(self) -> int:
+        return self._schema_id
+
+    @property
+    def struct(self) -> StructType:

Review comment:
       `as_struct`?
   
   Also, should this be a property or a method?




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