Fokko commented on code in PR #6525:
URL: https://github.com/apache/iceberg/pull/6525#discussion_r1064698892


##########
python/pyiceberg/avro/resolver.py:
##########
@@ -109,38 +109,46 @@ def resolve(
 
 
 class SchemaResolver(PrimitiveWithPartnerVisitor[IcebergType, Reader]):
-    read_types: Optional[Dict[int, Callable[[Schema], StructProtocol]]]
+    read_types: Dict[int, Type[StructProtocol]]
+    context: List[int]
 
-    def __init__(self, read_types: Optional[Dict[int, Callable[[Schema], 
StructProtocol]]]):
+    def __init__(self, read_types: Dict[int, Type[StructProtocol]] = 
EMPTY_DICT) -> None:
         self.read_types = read_types
+        self.context = []
 
     def schema(self, schema: Schema, expected_schema: Optional[IcebergType], 
result: Reader) -> Reader:
         return result
 
+    def before_field(self, field: NestedField, field_partner: 
Optional[NestedField]) -> None:
+        self.context.append(field.field_id)
+
+    def after_field(self, field: NestedField, field_partner: 
Optional[NestedField]) -> None:
+        self.context.pop()
+
     def struct(self, struct: StructType, expected_struct: 
Optional[IcebergType], field_readers: List[Reader]) -> Reader:
+        # -1 indicates the struct root
+        read_struct_id = self.context[-1] if len(self.context) > 0 else -1
+        struct_callable = self.read_types.get(read_struct_id, Record)

Review Comment:
   I also spend quite some CPU cycles on this, and I think it will go alright 
since we already solve this when building the read-tree. We have to file schema 
from the file, and the read schema from `manifest.py`. It is very important 
that we align the Pydantic classes and the schema, if these aren't aligned, it 
will be assigned to the incorrect field.
   
   I've added a test to validate this:
   ```python
   def test_column_assignment() -> None:
       int_schema = {
           "type": "record",
           "name": "ints",
           "fields": [
               {"name": "a", "type": "int", "field-id": 1},
               {"name": "b", "type": "int", "field-id": 2},
               {"name": "c", "type": "int", "field-id": 3},
           ],
       }
   
       from fastavro import parse_schema, writer
   
       parsed_schema = parse_schema(int_schema)
   
       int_records = [
           {
               "a": 1,
               "b": 2,
               "c": 3,
           }
       ]
   
       with TemporaryDirectory() as tmpdir:
           tmp_avro_file = tmpdir + "/manifest.avro"
           with open(tmp_avro_file, "wb") as out:
               writer(out, parsed_schema, int_records)
   
           class Ints(PydanticStruct):
               c: int = Field()
               d: Optional[int] = Field()
   
           MANIFEST_ENTRY_SCHEMA = Schema(
               NestedField(3, "c", IntegerType(), required=True),
               NestedField(4, "d", IntegerType(), required=False),
           )
   
           with AvroFile[Ints](PyArrowFileIO().new_input(tmp_avro_file), 
MANIFEST_ENTRY_SCHEMA, {-1: Ints}) as reader:
               records = list(reader)
   
       assert records == [Ints(c=3, d=None)]
   ```
   
   I've confirmed that `__set__` is called twice, one time with the `c` column, 
and then a `NoneReader`.



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