Skip to content

Commit 076a544

Browse files
authored
Fix mishandling of null types in output Avro schemas (#146)
1 parent a7e1835 commit 076a544

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

hoptimator-avro/src/main/java/com/linkedin/hoptimator/avro/AvroConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public static Schema avro(String namespace, String name, RelDataType dataType) {
6969
dataType.isNullable());
7070
case UNKNOWN:
7171
case NULL:
72-
return createAvroSchemaWithNullability(Schema.createUnion(Schema.create(Schema.Type.NULL)), true);
72+
// We can't have a union of null and null ["null", "null"], nor a nested union ["null",["null"]],
73+
// so we ignore nullability and just use ["null"] here.
74+
return Schema.createUnion(Schema.create(Schema.Type.NULL));
7375
default:
7476
throw new UnsupportedOperationException("No support yet for " + dataType.getSqlTypeName().toString());
7577
}

hoptimator-avro/src/test/java/com/linkedin/hoptimator/avro/AvroConverterTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public void convertsNestedUnionSchemas() {
8181
assertEquals(avroSchema4.toString(), avroSchema4.getFields().size(), rel1.getFieldCount());
8282
}
8383

84+
@Test
85+
public void supportsNullTypes() {
86+
RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
87+
RelDataType rel = typeFactory.createStructType(Collections.singletonList(typeFactory.createSqlType(SqlTypeName.NULL)),
88+
Collections.singletonList("field1"));
89+
90+
Schema avroSchema = AvroConverter.avro("NS", "R", rel);
91+
assertEquals(avroSchema.toString(), avroSchema.getFields().size(), rel.getFieldCount());
92+
}
93+
8494
@Test
8595
public void testAvroKeyPayloadSchemaNoKeyOptions() {
8696
RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);

0 commit comments

Comments
 (0)