Skip to content

Commit

Permalink
keep previous (v2) behaviour for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomezvillamor committed Jan 10, 2025
1 parent 5b1bbdf commit 42aff3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class AvroSchemaConverter implements SchemaConverter<Schema> {
private static final Map<String, Supplier<SchemaFieldDataType.Type>> LOGICAL_TYPE_MAPPING;
public static final String ARRAY_ITEMS_FIELD_NAME = "items";
public static final String MAP_VALUE_FIELD_NAME = "value";
public static final String UNION_TYPE_FIELD_NAME = "type";

static {
Map<String, Supplier<SchemaFieldDataType.Type>> logicalTypeMap = new HashMap<>();
Expand Down Expand Up @@ -472,9 +471,16 @@ private void processUnionField(
int typeIndex = 0;
for (Schema unionSchema : unionTypes) {
if (unionSchema.getType() != Schema.Type.NULL) {
String unionFieldName = field.name();
FieldPath indexedFieldPath = fieldPath.popLast();
indexedFieldPath =
indexedFieldPath.clonePlus(
new FieldElement(
Collections.singletonList("union"), new ArrayList<>(), null, null));

Schema.Field unionFieldInner =
new Schema.Field(
UNION_TYPE_FIELD_NAME,
unionFieldName,
unionSchema,
unionSchema.getDoc() != null ? unionSchema.getDoc() : unionDescription,
null);
Expand All @@ -483,7 +489,7 @@ private void processUnionField(
typeIndex,
unionFieldPath.asString(),
unionFieldInner.doc());
processField(unionFieldInner, unionFieldPath, defaultNullable, fields);
processField(unionFieldInner, indexedFieldPath, defaultNullable, fields);
}
typeIndex++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void testPrimitiveTypes() throws IOException {
.setNestedTypes(new StringArray(Collections.singletonList("union"))))));
assertSchemaField(
schema.getFields().get(2),
"[version=2.0].[type=PrimitiveType].[type=union].intFieldV2.[type=int].type",
"[version=2.0].[type=PrimitiveType].[type=union].[type=int].intFieldV2",
"int",
false,
false,
Expand Down Expand Up @@ -302,14 +302,14 @@ void testComplexMaps() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(17),
"[version=2.0].[type=MapType].[type=map].mapOfUnion.[type=union].value.[type=string].type",
"[version=2.0].[type=MapType].[type=map].mapOfUnion.[type=union].[type=string].value",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(18),
"[version=2.0].[type=MapType].[type=map].mapOfUnion.[type=union].value.[type=int].type",
"[version=2.0].[type=MapType].[type=map].mapOfUnion.[type=union].[type=int].value",
"int",
false,
false,
Expand Down Expand Up @@ -430,21 +430,21 @@ void testComplexArrays() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(11),
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].items.[type=string].type",
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].[type=string].items",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(12),
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].items.[type=int].type",
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].[type=int].items",
"int",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new NumberType())));
assertSchemaField(
schema.getFields().get(13),
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].items.[type=boolean].type",
"[version=2.0].[type=ArrayType].[type=array].arrayOfUnion.[type=union].[type=boolean].items",
"boolean",
false,
false,
Expand Down Expand Up @@ -591,14 +591,14 @@ void testComplexStructs() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(10),
"[version=2.0].[type=StructType].[type=ComplexStruct].structField.[type=union].fieldUnion.[type=string].type",
"[version=2.0].[type=StructType].[type=ComplexStruct].structField.[type=union].[type=string].fieldUnion",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(11),
"[version=2.0].[type=StructType].[type=ComplexStruct].structField.[type=union].fieldUnion.[type=int].type",
"[version=2.0].[type=StructType].[type=ComplexStruct].structField.[type=union].[type=int].fieldUnion",
"int",
false,
false,
Expand Down Expand Up @@ -638,21 +638,21 @@ void testComplexUnions() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(1),
"[version=2.0].[type=UnionType].[type=union].fieldUnionNullablePrimitives.[type=string].type",
"[version=2.0].[type=UnionType].[type=union].[type=string].fieldUnionNullablePrimitives",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(2),
"[version=2.0].[type=UnionType].[type=union].fieldUnionNullablePrimitives.[type=int].type",
"[version=2.0].[type=UnionType].[type=union].[type=int].fieldUnionNullablePrimitives",
"int",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new NumberType())));
assertSchemaField(
schema.getFields().get(3),
"[version=2.0].[type=UnionType].[type=union].fieldUnionNullablePrimitives.[type=boolean].type",
"[version=2.0].[type=UnionType].[type=union].[type=boolean].fieldUnionNullablePrimitives",
"boolean",
false,
false,
Expand All @@ -669,28 +669,28 @@ void testComplexUnions() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(5),
"[version=2.0].[type=UnionType].[type=union].fieldUnionComplexTypes.[type=NestedRecord].type",
"[version=2.0].[type=UnionType].[type=union].[type=NestedRecord].fieldUnionComplexTypes",
"NestedRecord",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new RecordType())));
assertSchemaField(
schema.getFields().get(6),
"[version=2.0].[type=UnionType].[type=union].fieldUnionComplexTypes.[type=NestedRecord].type.[type=string].nestedField1",
"[version=2.0].[type=UnionType].[type=union].[type=NestedRecord].fieldUnionComplexTypes.[type=string].nestedField1",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(7),
"[version=2.0].[type=UnionType].[type=union].fieldUnionComplexTypes.[type=NestedRecord].type.[type=int].nestedField2",
"[version=2.0].[type=UnionType].[type=union].[type=NestedRecord].fieldUnionComplexTypes.[type=int].nestedField2",
"int",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new NumberType())));
assertSchemaField(
schema.getFields().get(8),
"[version=2.0].[type=UnionType].[type=union].fieldUnionComplexTypes.[type=map].type",
"[version=2.0].[type=UnionType].[type=union].[type=map].fieldUnionComplexTypes",
"map<string,string>",
false,
false,
Expand All @@ -710,28 +710,28 @@ void testComplexUnions() throws IOException {
new UnionType().setNestedTypes(new StringArray("union")))));
assertSchemaField(
schema.getFields().get(10),
"[version=2.0].[type=UnionType].[type=union].fieldUnionPrimitiveAndComplex.[type=string].type",
"[version=2.0].[type=UnionType].[type=union].[type=string].fieldUnionPrimitiveAndComplex",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(11),
"[version=2.0].[type=UnionType].[type=union].fieldUnionPrimitiveAndComplex.[type=ComplexTypeRecord].type",
"[version=2.0].[type=UnionType].[type=union].[type=ComplexTypeRecord].fieldUnionPrimitiveAndComplex",
"ComplexTypeRecord",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new RecordType())));
assertSchemaField(
schema.getFields().get(12),
"[version=2.0].[type=UnionType].[type=union].fieldUnionPrimitiveAndComplex.[type=ComplexTypeRecord].type.[type=string].complexField1",
"[version=2.0].[type=UnionType].[type=union].[type=ComplexTypeRecord].fieldUnionPrimitiveAndComplex.[type=string].complexField1",
"string",
false,
false,
new SchemaFieldDataType().setType(SchemaFieldDataType.Type.create(new StringType())));
assertSchemaField(
schema.getFields().get(13),
"[version=2.0].[type=UnionType].[type=union].fieldUnionPrimitiveAndComplex.[type=ComplexTypeRecord].type.[type=int].complexField2",
"[version=2.0].[type=UnionType].[type=union].[type=ComplexTypeRecord].fieldUnionPrimitiveAndComplex.[type=int].complexField2",
"int",
false,
false,
Expand Down

0 comments on commit 42aff3b

Please sign in to comment.