diff --git a/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/schema/MergeHiveSchemaWithAvro.java b/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/schema/MergeHiveSchemaWithAvro.java index 406513d3a..ad338fa65 100644 --- a/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/schema/MergeHiveSchemaWithAvro.java +++ b/hivelink-core/src/main/java/org/apache/iceberg/hivelink/core/schema/MergeHiveSchemaWithAvro.java @@ -101,6 +101,8 @@ private Schema reorderOptionIfRequired(Schema schema, Object defaultValue) { boolean isNullFirstOption = schema.getTypes().get(0).getType() == Schema.Type.NULL; if (isNullFirstOption && defaultValue.equals(JsonProperties.NULL_VALUE)) { return schema; + } else if (!isNullFirstOption && !defaultValue.equals(JsonProperties.NULL_VALUE)) { + return schema; } else { return Schema.createUnion(schema.getTypes().get(1), schema.getTypes().get(0)); } @@ -143,7 +145,7 @@ public Schema union(UnionTypeInfo union, Schema partner, List results) { @Override public Schema primitive(PrimitiveTypeInfo primitive, Schema partner) { boolean shouldResultBeOptional = partner == null || AvroSchemaUtil.isOptionSchema(partner); - boolean nullShouldBeSecondElementInOptionalUnionSchema = + boolean nullShouldBeSecondElementInOptionalUnionSchema = partner != null && shouldResultBeOptional && AvroSchemaUtil.getNullIndexInUnion(partner) == 1; Schema hivePrimitive = hivePrimitiveToAvro(primitive); // if there was no matching Avro primitive, use the Hive primitive