Skip to content
Merged
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
42 changes: 3 additions & 39 deletions core/src/main/java/org/apache/iceberg/avro/PruneColumns.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.avro.JsonProperties;
import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.iceberg.mapping.NameMapping;
Expand Down Expand Up @@ -106,27 +105,7 @@ public Schema record(Schema record, List<String> names, List<Schema> fields) {

@Override
public Schema union(Schema union, List<Schema> options) {
if (AvroSchemaUtil.isOptionSchema(union)) {
// case option union
Schema pruned = null;
if (options.get(0) != null) {
pruned = options.get(0);
} else if (options.get(1) != null) {
pruned = options.get(1);
}

if (pruned != null) {
if (pruned != AvroSchemaUtil.fromOption(union)) {
return AvroSchemaUtil.toOption(pruned);
}
return union;
}

return null;
} else {
// Complex union case
return copyUnion(union, options);
}
return copyUnion(union, options);
}


Expand Down Expand Up @@ -261,23 +240,8 @@ private static Schema copyRecord(Schema record, List<Schema.Field> newFields) {
}

private static Schema.Field copyField(Schema.Field field, Schema newSchema, Integer fieldId) {
Schema newSchemaReordered;
// if the newSchema is an optional schema with no, or null, default value, then make sure the
// NULL option is the first
boolean hasNonNullDefaultValue = AvroSchemaUtil.hasNonNullDefaultValue(field);
if (isOptionSchemaWithNonNullFirstOption(newSchema) && !hasNonNullDefaultValue) {
newSchemaReordered = AvroSchemaUtil.toOption(AvroSchemaUtil.fromOption(newSchema));
} else if (AvroSchemaUtil.isOptionSchema(newSchema) && hasNonNullDefaultValue) {
// o.w. if the newSchema is an optional that has a non-null default value, then make sure the
// NULL option is the second
newSchemaReordered = AvroSchemaUtil.toOption(AvroSchemaUtil.fromOption(newSchema), true);
} else {
newSchemaReordered = newSchema;
}
// copy over non-null default values
Object defaultValue = hasNonNullDefaultValue ? field.defaultVal() :
(AvroSchemaUtil.isOptionSchema(newSchemaReordered) ? JsonProperties.NULL_VALUE : null);
Schema.Field copy = new Schema.Field(field.name(), newSchemaReordered, field.doc(), defaultValue, field.order());
// always copy over default values
Schema.Field copy = new Schema.Field(field.name(), newSchema, field.doc(), field.defaultVal(), field.order());

for (Map.Entry<String, Object> prop : field.getObjectProps().entrySet()) {
copy.addProp(prop.getKey(), prop.getValue());
Expand Down