umehrot2 commented on a change in pull request #1427: [HUDI-727]: Copy default
values of fields if not present when rewriting incoming record with new schema
URL: https://github.com/apache/incubator-hudi/pull/1427#discussion_r399722305
##########
File path:
hudi-common/src/main/java/org/apache/hudi/common/util/HoodieAvroUtils.java
##########
@@ -214,6 +220,63 @@ private static GenericRecord rewrite(GenericRecord
record, Schema schemaWithFiel
return newRecord;
}
+ /*
+ This function takes the union of all the fields except hoodie metadata fields
+ */
+ private static List<Field> getAllFieldsToWrite(Schema oldSchema, Schema
newSchema) {
+ Set<Field> allFields = new HashSet<>(oldSchema.getFields());
+ List<Field> fields = new ArrayList<>(oldSchema.getFields());
+ for (Schema.Field f : newSchema.getFields()) {
+ if (!allFields.contains(f) && !isMetadataField(f.name())) {
+ fields.add(f);
+ }
+ }
+
+ return fields;
+ }
+
+ private static void populateNewRecordAsPerDataType(GenericRecord record,
Field field) {
+ switch (getSchemaTypeForField(field)) {
+ case STRING:
+ case BYTES:
+ case ENUM:
+ case FIXED:
+ record.put(field.name(), field.defaultVal() == null ? null : (String)
field.defaultVal());
+ break;
+ case LONG:
+ record.put(field.name(), field.defaultVal() == null ? null : (long)
field.defaultVal());
+ break;
+ case INT:
+ record.put(field.name(), field.defaultVal() == null ? null : (int)
field.defaultVal());
+ break;
+ case FLOAT:
+ record.put(field.name(), field.defaultVal() == null ? null : (float)
field.defaultVal());
+ break;
+ case DOUBLE:
+ record.put(field.name(), field.defaultVal() == null ? null : (double)
field.defaultVal());
+ break;
+ case BOOLEAN:
+ record.put(field.name(), field.defaultVal() == null ? null : (boolean)
field.defaultVal());
+ break;
+ default:
+ record.put(field.name(), field.defaultVal());
+ }
+ }
+
+ private static Schema.Type getSchemaTypeForField(Field field) {
+ if (!field.schema().getType().equals(Schema.Type.UNION)) {
+ return field.schema().getType();
+ }
+
+ for (Schema schema : field.schema().getTypes()) {
+ if (!schema.getType().equals(Schema.Type.NULL)) {
+ return schema.getType();
+ }
+ }
+
+ return Schema.Type.STRING;
Review comment:
Shouldn't we return `Schema.Type.NULL` here ? Seems like the only case where
we will reach this line is when type is null.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services