rdblue commented on code in PR #40: URL: https://github.com/apache/iceberg-python/pull/40#discussion_r1353347354
########## tests/avro/test_resolver.py: ########## @@ -293,11 +306,103 @@ def test_resolver_initial_value() -> None: schema_id=2, ) - assert resolve(write_schema, read_schema) == StructReader( + assert resolve_reader(write_schema, read_schema) == StructReader( ( (None, StringReader()), # The one we skip (0, DefaultReader("vo")), ), Record, read_schema.as_struct(), ) + + +def test_resolve_writer() -> None: + actual = resolve_writer(record_schema=MANIFEST_ENTRY_SCHEMAS[2], file_schema=MANIFEST_ENTRY_SCHEMAS[1]) + expected = StructWriter( + ( + (0, IntegerWriter()), + (1, IntegerWriter()), + ( + 4, + StructWriter( + ( + (1, StringWriter()), + (2, StringWriter()), + (3, StructWriter(())), + (4, IntegerWriter()), + (5, IntegerWriter()), + (None, DefaultWriter(writer=IntegerWriter(), value=67108864)), + (6, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=IntegerWriter()))), + (7, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=IntegerWriter()))), + (8, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=IntegerWriter()))), + (9, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=IntegerWriter()))), + (10, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=BinaryWriter()))), + (11, OptionWriter(option=MapWriter(key_writer=IntegerWriter(), value_writer=BinaryWriter()))), + (12, OptionWriter(option=BinaryWriter())), + (13, OptionWriter(option=ListWriter(element_writer=IntegerWriter()))), + (15, OptionWriter(option=IntegerWriter())), + ) + ), + ), + ) + ) + + assert actual == expected + + +def test_resolve_writer_promotion() -> None: + with pytest.raises(ResolveError) as exc_info: + _ = resolve_writer( + record_schema=Schema(NestedField(field_id=1, name="floating", type=DoubleType(), required=True)), + file_schema=Schema(NestedField(field_id=1, name="floating", type=FloatType(), required=True)), + ) + + assert "Cannot promote double to float" in str(exc_info.value) + + +def test_writer_ordering() -> None: + actual = resolve_writer( + record_schema=Schema( + NestedField(field_id=1, name="str", type=StringType(), required=True), + NestedField(field_id=2, name="dbl", type=DoubleType(), required=True), + ), + file_schema=Schema( + NestedField(field_id=2, name="dbl", type=DoubleType(), required=True), + NestedField(field_id=1, name="str", type=StringType(), required=True), + ), + ) + + expected = StructWriter(((1, DoubleWriter()), (0, StringWriter()))) + + assert actual == expected + + +def test_writer_one_more_field() -> None: + actual = resolve_writer( + record_schema=Schema( + NestedField(field_id=3, name="bool", type=BooleanType(), required=True), + NestedField(field_id=1, name="str", type=StringType(), required=True), + NestedField(field_id=2, name="dbl", type=DoubleType(), required=True), + ), + file_schema=Schema( + NestedField(field_id=2, name="dbl", type=DoubleType(), required=True), + NestedField(field_id=1, name="str", type=StringType(), required=True), + ), + ) + + expected = StructWriter(((2, DoubleWriter()), (1, StringWriter()))) + + assert actual == expected + + +def test_writer_missing_optional_in_read_schema() -> None: + actual = resolve_writer( + record_schema=Schema(), + file_schema=Schema( + NestedField(field_id=1, name="str", type=StringType(), required=False), + ), + ) + + expected = StructWriter(field_writers=((None, OptionWriter(option=OptionWriter(option=StringWriter()))),)) Review Comment: Yeah, the double option writer is suspicious! -- 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