rdblue commented on code in PR #5116:
URL: https://github.com/apache/iceberg/pull/5116#discussion_r907849548


##########
python/src/iceberg/avro/resolver.py:
##########
@@ -0,0 +1,150 @@
+# 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 functools import singledispatch
+from typing import (
+    Dict,
+    List,
+    Optional,
+    Set,
+    Tuple,
+    Type,
+)
+
+from iceberg.avro.reader import (
+    ConstructReader,
+    ListReader,
+    MapReader,
+    NoneReader,
+    OptionReader,
+    Reader,
+    StructReader,
+    primitive_reader,
+)
+from iceberg.schema import Schema, visit
+from iceberg.types import (
+    BinaryType,
+    IcebergType,
+    IntegerType,
+    ListType,
+    LongType,
+    MapType,
+    PrimitiveType,
+    StringType,
+    StructType,
+)
+
+
+class ResolveException(Exception):
+    pass
+
+
+@singledispatch
+def resolve(write_schema, read_schema) -> Reader:
+    """This resolves the write and read schema
+
+    The function traverses the schema in post-order fashion
+
+     Args:
+         write_schema (Schema | IcebergType): The write schema of the Avro file
+         read_schema (Schema | IcebergType): The requested read schema which 
is equal or a subset of the write schema
+
+     Raises:
+         NotImplementedError: If attempting to resolve an unrecognized object 
type
+    """
+    raise NotImplementedError("Cannot resolve non-type: %s" % write_schema)
+
+
[email protected](Schema)
+def _(write_schema: Schema, read_schema: Schema) -> Reader:
+    """Visit a Schema and starts resolving it by converting it to a struct"""
+    return resolve(write_schema.as_struct(), read_schema.as_struct())
+
+
[email protected](StructType)
+def _(write_struct: StructType, read_struct: StructType) -> Reader:
+    """Iterates over the write schema, and checks if the field is in the read 
schema"""
+    results: List[Tuple[Optional[int], Reader]] = []
+
+    read_fields = {field.name: (pos, field) for pos, field in 
enumerate(read_struct.fields)}

Review Comment:
   In Iceberg, resolution should be done by ID rather than by name. This 
name-based code is correct for reading Avro, but Iceberg requires the 
resolution to be done by ID rather than by name.
   
   In the Java implementation, we construct a read schema from the file schema 
and the Iceberg schema that can be used to read by name but produce the correct 
ID-based resolution. That's an annoying and difficult to maintain bit of code, 
which is why we want to do the ID-based resolution directly here, rather than 
relying on name-based resolution in an Avro library.



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