rdblue commented on code in PR #40: URL: https://github.com/apache/iceberg-python/pull/40#discussion_r1353328176
########## pyiceberg/avro/resolver.py: ########## @@ -233,7 +256,95 @@ def skip(self, decoder: BinaryDecoder) -> None: pass -class SchemaResolver(PrimitiveWithPartnerVisitor[IcebergType, Reader]): +class WriteSchemaResolver(PrimitiveWithPartnerVisitor[IcebergType, Writer]): + def schema(self, file_schema: Schema, record_schema: Optional[IcebergType], result: Writer) -> Writer: + return result + + def struct(self, file_schema: StructType, record_struct: Optional[IcebergType], file_writers: List[Writer]) -> Writer: + if not isinstance(record_struct, StructType): + raise ResolveError(f"File/write schema are not aligned for struct, got {record_struct}") + + record_struct_positions: Dict[int, int] = {field.field_id: pos for pos, field in enumerate(record_struct.fields)} + results: List[Tuple[Optional[int], Writer]] = [] + + for writer, file_field in zip(file_writers, file_schema.fields): + if file_field.field_id in record_struct_positions: + results.append((record_struct_positions[file_field.field_id], writer)) + elif file_field.required: + # There is a default value + if file_field.write_default is not None: + # The field is not in the record, but there is a write default value + results.append((None, DefaultWriter(writer=writer, value=file_field.write_default))) # type: ignore + elif file_field.required: + raise ValueError(f"Field is required, and there is no write default: {file_field}") + else: + results.append((None, OptionWriter(option=writer))) Review Comment: Won't writer already be an `OptionWriter`? It was already processed by `field` which should create one if the field is not required. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org