Skip to content

Commit

Permalink
Add single union schema conversion logic for hive to iceberg
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhang10 committed Jan 10, 2024
1 parent 59b5423 commit 32aa187
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public Type list(ListTypeInfo list, Type elementResult) {
// Mimic the struct call behavior to construct a union converted struct type
@Override
public Type union(UnionTypeInfo union, List<Type> unionResults) {
// if the union is single-typed, return the type directly
if (union.getAllUnionObjectTypeInfos().size() == 1) {
return unionResults.get(0);
}
// else, it's a complex union
List<Types.NestedField> fields = Lists.newArrayListWithExpectedSize(unionResults.size() + 1);
fields.add(Types.NestedField.required(allocateId(), "tag", Types.IntegerType.get()));
for (int i = 0; i < unionResults.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void testPrimitiveTypes() {
for (int i = 0; i < primitives.size(); i += 1) {
Type icebergType = primitives.get(i);
PrimitiveTypeInfo hiveType = hivePrimitives.get(i);
Assert.assertEquals("Hive schema to primitive: " + hiveType, icebergType, HiveTypeUtil.convert(hiveType));
Assert.assertEquals("Hive schema to primitive: " + hiveType, icebergType,
HiveTypeUtil.convert(hiveType));
}
}

Expand All @@ -82,6 +83,8 @@ public void testConversions() {
"length:int,count:int,list:array<struct<lastword:string,lastwordlength:int>>," +
"wordcounts:map<string,int>>");
check("struct<1: tag: required int, 2: field0: optional int, 3: field1: optional string>", "uniontype<int,string>");
// single type union
check("int", "uniontype<int>");
}

private static void check(String icebergTypeStr, String hiveTypeStr) {
Expand Down

0 comments on commit 32aa187

Please sign in to comment.