Fokko commented on code in PR #40: URL: https://github.com/apache/iceberg-python/pull/40#discussion_r1352318212
########## pyiceberg/avro/resolver.py: ########## @@ -233,7 +256,93 @@ def skip(self, decoder: BinaryDecoder) -> None: pass -class SchemaResolver(PrimitiveWithPartnerVisitor[IcebergType, Reader]): +class WriteSchemaResolver(PrimitiveWithPartnerVisitor[IcebergType, Writer]): + def schema(self, write_schema: Schema, data_schema: Optional[IcebergType], result: Writer) -> Writer: + return result + + def struct(self, write_schema: StructType, data_struct: Optional[IcebergType], field_writers: List[Writer]) -> Writer: + if not isinstance(data_struct, StructType): + raise ResolveError(f"File/write schema are not aligned for struct, got {data_struct}") + + data_positions: Dict[int, int] = {field.field_id: pos for pos, field in enumerate(data_struct.fields)} + results: List[Tuple[Optional[int], Writer]] = [] + + for writer, write_field in zip(field_writers, write_schema.fields): + if write_field.field_id in data_positions: + results.append((data_positions[write_field.field_id], writer)) + else: + # There is a default value + if write_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=write_field.write_default))) # type: ignore + elif write_field.required: + raise ValueError(f"Field is required, and there is no write default: {write_field}") + + return StructWriter(field_writers=tuple(results)) + + def field(self, write_field: NestedField, data_type: Optional[IcebergType], field_writer: Writer) -> Writer: + return field_writer if write_field.required else OptionWriter(field_writer) + + def list(self, write_list_type: ListType, write_list: Optional[IcebergType], element_reader: Writer) -> Writer: Review Comment: Nice, thanks! -- 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