@@ -86,10 +86,30 @@ private static String getAvroSchema(String name, String dataType)
8686 return getAvroSchema (ImmutableMap .of (name , dataType ));
8787 }
8888
89+ private static String determineDefaultValue (String dataType )
90+ {
91+ // Apache Avro 1.9 is more strict on the nullability of data types.
92+ // If the data type is not nullable, then we don't want to set null as the default.
93+ if (dataType .contains ("\" array\" " )) {
94+ // In the case of an Array we just will return an empty array
95+ return ", \" default\" : []" ;
96+ }
97+ if (dataType .contains ("\" map\" " )) {
98+ // In the case of a Map we just will return an empty map
99+ return ", \" default\" : {}" ;
100+ }
101+ if (dataType .contains ("null" )) {
102+ // Will match ["null", "string"] and any other variation.
103+ return ", \" default\" : null" ;
104+ }
105+ // In case of non-nullable types like "string"
106+ return "" ;
107+ }
108+
89109 private static String getAvroSchema (Map <String , String > fields )
90110 {
91111 String fieldSchema = fields .entrySet ().stream ()
92- .map (entry -> "{\" name\" : \" " + entry .getKey () + "\" ,\" type\" : " + entry .getValue () + ", \" default \" : null }" )
112+ .map (entry -> "{\" name\" : \" " + entry .getKey () + "\" ,\" type\" : " + entry .getValue () + determineDefaultValue ( entry . getValue ()) + " }" )
93113 .collect (Collectors .joining ("," ));
94114
95115 return "{\" type\" : \" record\" ," +
@@ -424,7 +444,7 @@ public void testArrayDecodedAsArray()
424444 {
425445 DecoderTestColumnHandle row = new DecoderTestColumnHandle (0 , "row" , new ArrayType (BIGINT ), "array_field" , null , null , false , false , false );
426446
427- Map <DecoderColumnHandle , FieldValueProvider > decodedRow = buildAndDecodeColumn (row , "array_field" , "{\" type\" : \" array\" , \" items\" : \" long\" }" , ImmutableList .of (114L , 136L ));
447+ Map <DecoderColumnHandle , FieldValueProvider > decodedRow = buildAndDecodeColumn (row , "array_field" , "{\" type\" : \" array\" , \" items\" : [ \" long\" ] }" , ImmutableList .of (114L , 136L ));
428448 checkArrayValue (decodedRow , row , new long [] {114 , 136 });
429449 }
430450
@@ -436,7 +456,7 @@ public void testArrayWithNulls()
436456
437457 List <Long > values = new ArrayList <>();
438458 values .add (null );
439- Map <DecoderColumnHandle , FieldValueProvider > decodedRow = buildAndDecodeColumn (row , "array_field" , "{\" type\" : \" array\" , \" items\" : \" null\" }" , values );
459+ Map <DecoderColumnHandle , FieldValueProvider > decodedRow = buildAndDecodeColumn (row , "array_field" , "{\" type\" : \" array\" , \" items\" : [ \" null\" ] }" , values );
440460 checkArrayItemIsNull (decodedRow , row , new long [] {0 });
441461 }
442462
0 commit comments