|
31 | 31 |
|
32 | 32 | #include "iceberg/avro/avro_data_util_internal.h" |
33 | 33 | #include "iceberg/avro/avro_schema_util_internal.h" |
| 34 | +#include "iceberg/expression/literal.h" |
34 | 35 | #include "iceberg/schema.h" |
35 | 36 | #include "iceberg/schema_internal.h" |
36 | 37 | #include "iceberg/schema_util.h" |
@@ -662,6 +663,43 @@ TEST(AppendDatumToBuilderTest, StructWithMissingOptionalField) { |
662 | 663 | avro_data, expected_json)); |
663 | 664 | } |
664 | 665 |
|
| 666 | +TEST(AppendDatumToBuilderTest, StructWithMissingDefaultFields) { |
| 667 | + Schema iceberg_schema({ |
| 668 | + SchemaField::MakeRequired(1, "id", iceberg::int32()), |
| 669 | + // Missing required field with an initial-default: filled with the default. |
| 670 | + SchemaField(2, "score", iceberg::int64(), /*optional=*/false, /*doc=*/{}, |
| 671 | + std::make_shared<const Literal>(Literal::Long(100))), |
| 672 | + // Missing optional field with an initial-default: also filled, not null. |
| 673 | + SchemaField(3, "grade", iceberg::string(), /*optional=*/true, /*doc=*/{}, |
| 674 | + std::make_shared<const Literal>(Literal::String("A"))), |
| 675 | + }); |
| 676 | + |
| 677 | + // Create Avro schema that only has the id field (missing score and grade). |
| 678 | + std::string avro_schema_json = R"({ |
| 679 | + "type": "record", |
| 680 | + "name": "person", |
| 681 | + "fields": [ |
| 682 | + {"name": "id", "type": "int", "field-id": 1} |
| 683 | + ] |
| 684 | + })"; |
| 685 | + auto avro_schema = ::avro::compileJsonSchemaFromString(avro_schema_json); |
| 686 | + |
| 687 | + std::vector<::avro::GenericDatum> avro_data; |
| 688 | + for (int i = 0; i < 2; ++i) { |
| 689 | + ::avro::GenericDatum avro_datum(avro_schema.root()); |
| 690 | + auto& record = avro_datum.value<::avro::GenericRecord>(); |
| 691 | + record.fieldAt(0).value<int32_t>() = i + 1; |
| 692 | + avro_data.push_back(avro_datum); |
| 693 | + } |
| 694 | + |
| 695 | + const std::string expected_json = R"([ |
| 696 | + {"id": 1, "score": 100, "grade": "A"}, |
| 697 | + {"id": 2, "score": 100, "grade": "A"} |
| 698 | + ])"; |
| 699 | + ASSERT_NO_FATAL_FAILURE(VerifyAppendDatumToBuilder(iceberg_schema, avro_schema.root(), |
| 700 | + avro_data, expected_json)); |
| 701 | +} |
| 702 | + |
665 | 703 | TEST(AppendDatumToBuilderTest, NestedStructWithMissingOptionalFields) { |
666 | 704 | Schema iceberg_schema({ |
667 | 705 | SchemaField::MakeRequired(1, "id", iceberg::int32()), |
|
0 commit comments