@@ -903,6 +903,166 @@ Result<SchemaProjection> Project(const Schema& expected_schema,
903903
904904namespace {
905905
906+ Result<std::shared_ptr<Type>> FromAvroNode (const ::avro::NodePtr& node);
907+
908+ std::optional<std::string> GetIcebergFieldName (const ::avro::NodePtr& node,
909+ size_t field_idx) {
910+ if (field_idx >= node->customAttributes ()) {
911+ return std::nullopt ;
912+ }
913+ auto name = node->customAttributesAt (field_idx).getAttribute (
914+ std::string (kIcebergFieldNameProp ));
915+ if (name.has_value () && name->size () >= 2 && name->front () == ' "' &&
916+ name->back () == ' "' ) {
917+ return name->substr (1 , name->size () - 2 );
918+ }
919+ return name;
920+ }
921+
922+ // Converts an Avro field node to an Iceberg field. A union with null is read as an
923+ // optional field and a bare null node as an optional field of unknown type.
924+ Result<SchemaField> FromAvroField (int32_t field_id, std::string_view name,
925+ const ::avro::NodePtr& node) {
926+ if (node->type () == ::avro::AVRO_NULL ) {
927+ return SchemaField::MakeOptional (field_id, name, iceberg::unknown ());
928+ }
929+ ::avro::NodePtr value_node;
930+ ICEBERG_RETURN_UNEXPECTED (UnwrapUnion (node, &value_node));
931+ ICEBERG_ASSIGN_OR_RAISE (auto type, FromAvroNode (value_node));
932+ bool optional = node->type () == ::avro::AVRO_UNION ;
933+ return SchemaField (field_id, name, std::move (type), optional);
934+ }
935+
936+ Result<std::vector<SchemaField>> FromAvroRecordFields (const ::avro::NodePtr& node) {
937+ if (node->type () != ::avro::AVRO_RECORD ) {
938+ return InvalidSchema (" Expected AVRO_RECORD type, but got {}" , ToString (node));
939+ }
940+ std::vector<SchemaField> fields;
941+ fields.reserve (node->leaves ());
942+ for (size_t i = 0 ; i < node->leaves (); ++i) {
943+ ICEBERG_ASSIGN_OR_RAISE (int32_t field_id, GetFieldId (node, i));
944+ std::string name = GetIcebergFieldName (node, i).value_or (node->nameAt (i));
945+ ICEBERG_ASSIGN_OR_RAISE (auto field, FromAvroField (field_id, name, node->leafAt (i)));
946+ fields.emplace_back (std::move (field));
947+ }
948+ return fields;
949+ }
950+
951+ Result<std::shared_ptr<Type>> FromAvroTimestamp (const ::avro::NodePtr& node, bool nanos) {
952+ bool adjust_to_utc = GetAdjustToUtc (node).value_or (" false" ) == " true" ;
953+ if (nanos) {
954+ return adjust_to_utc ? std::shared_ptr<Type>(iceberg::timestamptz_ns ())
955+ : std::shared_ptr<Type>(iceberg::timestamp_ns ());
956+ }
957+ return adjust_to_utc ? std::shared_ptr<Type>(iceberg::timestamp_tz ())
958+ : std::shared_ptr<Type>(iceberg::timestamp ());
959+ }
960+
961+ Result<std::shared_ptr<Type>> FromAvroNode (const ::avro::NodePtr& node) {
962+ switch (node->type ()) {
963+ case ::avro::AVRO_RECORD : {
964+ ICEBERG_ASSIGN_OR_RAISE (auto fields, FromAvroRecordFields (node));
965+ return iceberg::struct_ (std::move (fields));
966+ }
967+ case ::avro::AVRO_ARRAY : {
968+ if (node->leaves () != 1 ) {
969+ return InvalidSchema (" Array type must have exactly one node, got {}" ,
970+ node->leaves ());
971+ }
972+ if (HasMapLogicalType (node)) {
973+ const auto & entry_node = node->leafAt (0 );
974+ ICEBERG_ASSIGN_OR_RAISE (auto entry_fields, FromAvroRecordFields (entry_node));
975+ if (entry_fields.size () != 2 ) {
976+ return InvalidSchema (
977+ " Array-backed map type must have a record node with two fields" );
978+ }
979+ SchemaField key (entry_fields[0 ].field_id (), MapType::kKeyName ,
980+ entry_fields[0 ].type (), entry_fields[0 ].optional ());
981+ SchemaField value (entry_fields[1 ].field_id (), MapType::kValueName ,
982+ entry_fields[1 ].type (), entry_fields[1 ].optional ());
983+ return iceberg::map (std::move (key), std::move (value));
984+ }
985+ ICEBERG_ASSIGN_OR_RAISE (int32_t element_id, GetElementId (node));
986+ ICEBERG_ASSIGN_OR_RAISE (
987+ auto element,
988+ FromAvroField (element_id, ListType::kElementName , node->leafAt (0 )));
989+ return iceberg::list (std::move (element));
990+ }
991+ case ::avro::AVRO_MAP : {
992+ if (node->leaves () != 2 ) {
993+ return InvalidSchema (" Map type must have exactly two nodes, got {}" ,
994+ node->leaves ());
995+ }
996+ ICEBERG_ASSIGN_OR_RAISE (int32_t key_id, GetKeyId (node));
997+ ICEBERG_ASSIGN_OR_RAISE (int32_t value_id, GetValueId (node));
998+ auto key = SchemaField::MakeRequired (key_id, MapType::kKeyName , iceberg::string ());
999+ ICEBERG_ASSIGN_OR_RAISE (
1000+ auto value, FromAvroField (value_id, MapType::kValueName , node->leafAt (1 )));
1001+ return iceberg::map (std::move (key), std::move (value));
1002+ }
1003+ case ::avro::AVRO_BOOL :
1004+ return iceberg::boolean ();
1005+ case ::avro::AVRO_INT :
1006+ if (HasLogicalType (node, ::avro::LogicalType::DATE )) {
1007+ return iceberg::date ();
1008+ }
1009+ if (HasLogicalType (node, ::avro::LogicalType::NONE )) {
1010+ return iceberg::int32 ();
1011+ }
1012+ break ;
1013+ case ::avro::AVRO_LONG :
1014+ switch (node->logicalType ().type ()) {
1015+ case ::avro::LogicalType::NONE :
1016+ return iceberg::int64 ();
1017+ case ::avro::LogicalType::TIME_MICROS :
1018+ return iceberg::time ();
1019+ case ::avro::LogicalType::TIMESTAMP_MICROS :
1020+ return FromAvroTimestamp (node, /* nanos=*/ false );
1021+ case ::avro::LogicalType::TIMESTAMP_NANOS :
1022+ return FromAvroTimestamp (node, /* nanos=*/ true );
1023+ default :
1024+ break ;
1025+ }
1026+ break ;
1027+ case ::avro::AVRO_FLOAT :
1028+ return iceberg::float32 ();
1029+ case ::avro::AVRO_DOUBLE :
1030+ return iceberg::float64 ();
1031+ case ::avro::AVRO_STRING :
1032+ return iceberg::string ();
1033+ case ::avro::AVRO_BYTES :
1034+ if (HasLogicalType (node, ::avro::LogicalType::NONE )) {
1035+ return iceberg::binary ();
1036+ }
1037+ break ;
1038+ case ::avro::AVRO_FIXED :
1039+ if (HasLogicalType (node, ::avro::LogicalType::DECIMAL )) {
1040+ const auto logical_type = node->logicalType ();
1041+ return iceberg::decimal (static_cast <int32_t >(logical_type.precision ()),
1042+ static_cast <int32_t >(logical_type.scale ()));
1043+ }
1044+ if (HasLogicalType (node, ::avro::LogicalType::UUID ) && node->fixedSize () == 16 ) {
1045+ return iceberg::uuid ();
1046+ }
1047+ if (HasLogicalType (node, ::avro::LogicalType::NONE )) {
1048+ return iceberg::fixed (static_cast <int32_t >(node->fixedSize ()));
1049+ }
1050+ break ;
1051+ default :
1052+ break ;
1053+ }
1054+ return NotSupported (" Cannot convert Avro type {} to an Iceberg type" , ToString (node));
1055+ }
1056+
1057+ } // namespace
1058+
1059+ Result<std::unique_ptr<Schema>> FromAvroSchema (const ::avro::NodePtr& avro_node) {
1060+ ICEBERG_ASSIGN_OR_RAISE (auto fields, FromAvroRecordFields (avro_node));
1061+ return std::make_unique<Schema>(std::move (fields));
1062+ }
1063+
1064+ namespace {
1065+
9061066class NamesGuard {
9071067 public:
9081068 NamesGuard (std::vector<std::string>& names, const std::string& name) : names_(names) {
0 commit comments