Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE/AVRO: Handle all union options coersion to single type #20331

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ private static BlockBuildingDecoder createBlockBuildingDecoderForAction(Resolver
case RECORD -> new RowBlockBuildingDecoder(action, typeManager);
case ENUM -> new EnumBlockBuildingDecoder((Resolver.EnumAdjust) action);
case WRITER_UNION -> {
if (isSimpleNullableUnion(action.reader)) {
yield new WriterUnionBlockBuildingDecoder((Resolver.WriterUnion) action, typeManager);
if (action.reader.getType() == Schema.Type.UNION && !isSimpleNullableUnion(action.reader)) {
yield new WriterUnionCoercedIntoRowBlockBuildingDecoder((Resolver.WriterUnion) action, typeManager);
}
else {
yield new WriterUnionCoercedIntoRowBlockBuildingDecoder((Resolver.WriterUnion) action, typeManager);
// reading a union with non-union or nullable union, optimistically try to create the reader, will fail at read time with any underlying issues
yield new WriterUnionBlockBuildingDecoder((Resolver.WriterUnion) action, typeManager);
}
}
case READER_UNION -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,37 @@ public void testRead3UnionWith2UnionDataWith2Union()
}
}
}

@Test
public void testReadUnionWithNonUnionAllCoercions()
throws IOException, AvroTypeException
{
Schema nonUnion = Schema.create(Schema.Type.STRING);
Schema union = Schema.createUnion(Schema.create(Schema.Type.STRING), Schema.create(Schema.Type.BYTES));

Schema nonUnionRecord = SchemaBuilder.builder()
.record("aRecord")
.fields()
.name("aField")
.type(nonUnion)
.noDefault()
.endRecord();

Schema unionRecord = SchemaBuilder.builder()
.record("aRecord")
.fields()
.name("aField")
.type(union)
.noDefault()
.endRecord();

TrinoInputFile inputFile = createWrittenFileWithSchema(1000, unionRecord);

//read the file with the non-union schema and ensure that no error thrown
try (AvroFileReader avroFileReader = new AvroFileReader(inputFile, nonUnionRecord, NoOpAvroTypeManager.INSTANCE)) {
while (avroFileReader.hasNext()) {
assertThat(avroFileReader.next()).isNotNull();
}
}
}
}
Loading