diff --git a/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/HiveTypeToIcebergType.java b/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/HiveTypeToIcebergType.java index 0ed64d154..e27a4097d 100644 --- a/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/HiveTypeToIcebergType.java +++ b/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/HiveTypeToIcebergType.java @@ -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 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 fields = Lists.newArrayListWithExpectedSize(unionResults.size() + 1); fields.add(Types.NestedField.required(allocateId(), "tag", Types.IntegerType.get())); for (int i = 0; i < unionResults.size(); i++) { diff --git a/hivelink-core/src/test/java/org/apache/iceberg/hivelink/core/TestHiveSchemaConversions.java b/hivelink-core/src/test/java/org/apache/iceberg/hivelink/core/TestHiveSchemaConversions.java index d7044dce3..4bf9e8f7d 100644 --- a/hivelink-core/src/test/java/org/apache/iceberg/hivelink/core/TestHiveSchemaConversions.java +++ b/hivelink-core/src/test/java/org/apache/iceberg/hivelink/core/TestHiveSchemaConversions.java @@ -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)); } } @@ -82,6 +83,8 @@ public void testConversions() { "length:int,count:int,list:array>," + "wordcounts:map>"); check("struct<1: tag: required int, 2: field0: optional int, 3: field1: optional string>", "uniontype"); + // single type union + check("int", "uniontype"); } private static void check(String icebergTypeStr, String hiveTypeStr) {