@@ -938,64 +938,6 @@ public void testUnionStringMapConversion() {
938938 testSendRecord (icebergSchema , icebergRecord );
939939 }
940940
941- @ Test
942- public void testUnionArrayMapConversion () {
943- String avroSchemaStr = " {\n " +
944- " \" type\" : \" record\" ,\n " +
945- " \" name\" : \" TestRecord\" ,\n " +
946- " \" fields\" : [\n " +
947- " {\n " +
948- " \" name\" : \" mapField\" ,\n " +
949- " \" type\" : {\n " +
950- " \" type\" : \" array\" ,\n " +
951- " \" logicalType\" : \" map\" ,\n " +
952- " \" items\" : [\n " +
953- " \" null\" ,\n " +
954- " {\n " +
955- " \" type\" : \" record\" ,\n " +
956- " \" name\" : \" UnionMapEntry\" ,\n " +
957- " \" fields\" : [\n " +
958- " {\" name\" : \" key\" , \" type\" : \" int\" },\n " +
959- " {\" name\" : \" value\" , \" type\" : \" string\" }\n " +
960- " ]\n " +
961- " }\n " +
962- " ]\n " +
963- " }\n " +
964- " }\n " +
965- " ]\n " +
966- " }\n " ;
967-
968- avroSchema = new Schema .Parser ().parse (avroSchemaStr );
969-
970- Map <Integer , String > expectedMap = new HashMap <>();
971- expectedMap .put (10 , "alpha" );
972- expectedMap .put (20 , "beta" );
973-
974- Schema mapFieldSchema = avroSchema .getField ("mapField" ).schema ();
975- Schema elementUnionSchema = mapFieldSchema .getElementType ();
976- Schema entrySchema = elementUnionSchema .getTypes ().stream ()
977- .filter (s -> s .getType () == Schema .Type .RECORD )
978- .findFirst ()
979- .orElseThrow (() -> new IllegalStateException ("Array element UNION schema does not contain a RECORD type" ));
980-
981- GenericData .Array <Object > mapEntries = new GenericData .Array <>(expectedMap .size () + 1 , mapFieldSchema );
982- for (Map .Entry <Integer , String > entry : expectedMap .entrySet ()) {
983- GenericRecord mapEntry = new GenericData .Record (entrySchema );
984- mapEntry .put ("key" , entry .getKey ());
985- mapEntry .put ("value" , entry .getValue ());
986- mapEntries .add (mapEntry );
987- }
988- mapEntries .add (null );
989-
990- AvroValueAdapter adapter = new AvroValueAdapter ();
991- Types .MapType mapType = Types .MapType .ofOptional (1 , 2 , Types .IntegerType .get (), Types .StringType .get ());
992-
993- @ SuppressWarnings ("unchecked" )
994- Map <Integer , Object > result = (Map <Integer , Object >) adapter .convert (mapEntries , mapFieldSchema , mapType );
995-
996- assertEquals (expectedMap , result );
997- }
998-
999941 // Test method for converting a record with nested fields
1000942 @ Test
1001943 public void testNestedRecordConversion () {
@@ -1132,6 +1074,40 @@ public void testUnionFieldConversion() {
11321074 " {\n " +
11331075 " \" name\" : \" unionField4\" ,\n " +
11341076 " \" type\" : [\" null\" , \" string\" ]\n " +
1077+ " },\n " +
1078+ " {\n " +
1079+ " \" name\" : \" unionListField\" ,\n " +
1080+ " \" type\" : [\n " +
1081+ " \" null\" ,\n " +
1082+ " {\n " +
1083+ " \" type\" : \" array\" ,\n " +
1084+ " \" items\" : \" string\" \n " +
1085+ " }\n " +
1086+ " ]\n " +
1087+ " },\n " +
1088+ " {\n " +
1089+ " \" name\" : \" unionMapField\" ,\n " +
1090+ " \" type\" : [\n " +
1091+ " \" null\" ,\n " +
1092+ " {\n " +
1093+ " \" type\" : \" map\" ,\n " +
1094+ " \" values\" : \" int\" \n " +
1095+ " }\n " +
1096+ " ]\n " +
1097+ " },\n " +
1098+ " {\n " +
1099+ " \" name\" : \" unionStructField\" ,\n " +
1100+ " \" type\" : [\n " +
1101+ " \" null\" ,\n " +
1102+ " {\n " +
1103+ " \" type\" : \" record\" ,\n " +
1104+ " \" name\" : \" UnionStruct\" ,\n " +
1105+ " \" fields\" : [\n " +
1106+ " {\" name\" : \" innerString\" , \" type\" : \" string\" },\n " +
1107+ " {\" name\" : \" innerInt\" , \" type\" : \" int\" }\n " +
1108+ " ]\n " +
1109+ " }\n " +
1110+ " ]\n " +
11351111 " }\n " +
11361112 " ]\n " +
11371113 " }\n " ;
@@ -1141,6 +1117,17 @@ public void testUnionFieldConversion() {
11411117 avroRecord .put ("unionField1" , "union_string" );
11421118 avroRecord .put ("unionField2" , 42 );
11431119 avroRecord .put ("unionField3" , true );
1120+ List <String > unionList = Arrays .asList ("item1" , "item2" );
1121+ avroRecord .put ("unionListField" , unionList );
1122+ Map <String , Integer > unionMap = new HashMap <>();
1123+ unionMap .put ("one" , 1 );
1124+ unionMap .put ("two" , 2 );
1125+ avroRecord .put ("unionMapField" , unionMap );
1126+ Schema unionStructSchema = avroSchema .getField ("unionStructField" ).schema ().getTypes ().get (1 );
1127+ GenericRecord unionStruct = new GenericData .Record (unionStructSchema );
1128+ unionStruct .put ("innerString" , "nested" );
1129+ unionStruct .put ("innerInt" , 99 );
1130+ avroRecord .put ("unionStructField" , unionStruct );
11441131
11451132 // Convert Avro record to Iceberg record using the wrapper
11461133 org .apache .iceberg .Schema icebergSchema = AvroSchemaUtil .toIceberg (avroSchema );
@@ -1156,6 +1143,15 @@ public void testUnionFieldConversion() {
11561143 Object unionField3 = icebergRecord .getField ("unionField3" );
11571144 assertEquals (true , unionField3 );
11581145
1146+ assertNull (icebergRecord .getField ("unionField4" ));
1147+
1148+ assertEquals (unionList , normalizeValue (icebergRecord .getField ("unionListField" )));
1149+ assertEquals (unionMap , normalizeValue (icebergRecord .getField ("unionMapField" )));
1150+
1151+ Record unionStructRecord = (Record ) icebergRecord .getField ("unionStructField" );
1152+ assertEquals ("nested" , unionStructRecord .getField ("innerString" ).toString ());
1153+ assertEquals (99 , unionStructRecord .getField ("innerInt" ));
1154+
11591155 // Send the record to the table
11601156 testSendRecord (icebergSchema , icebergRecord );
11611157 }
0 commit comments