-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-3921] Improve rewriteRecordWithNewSchema and refactor code #5393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,10 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Deque; | ||
| import java.util.LinkedList; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Spliterator; | ||
| import java.util.Spliterators; | ||
| import java.util.stream.StreamSupport; | ||
| import java.util.TimeZone; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
@@ -94,6 +97,10 @@ public class HoodieAvroUtils { | |
| //Export for test | ||
| public static final Conversions.DecimalConversion DECIMAL_CONVERSION = new Conversions.DecimalConversion(); | ||
|
|
||
| // Name of ArrayType/MapType for avro Schema | ||
| private static final String ARRAY_TYPE_ELEMENT_NAME = "element"; | ||
| private static final String MAP_TYPE_VALUE_NAME = "value"; | ||
|
|
||
| // As per https://avro.apache.org/docs/current/spec.html#names | ||
| private static final String INVALID_AVRO_CHARS_IN_NAMES = "[^A-Za-z0-9_]"; | ||
| private static final String INVALID_AVRO_FIRST_CHAR_IN_NAMES = "[^A-Za-z_]"; | ||
|
|
@@ -410,7 +417,7 @@ public static GenericRecord rewriteRecordWithMetadata(GenericRecord genericRecor | |
|
|
||
| // TODO Unify the logical of rewriteRecordWithMetadata and rewriteEvolutionRecordWithMetadata, and delete this function. | ||
| public static GenericRecord rewriteEvolutionRecordWithMetadata(GenericRecord genericRecord, Schema newSchema, String fileName) { | ||
| GenericRecord newRecord = HoodieAvroUtils.rewriteRecordWithNewSchema(genericRecord, newSchema, new HashMap<>()); | ||
| GenericRecord newRecord = HoodieAvroUtils.rewriteRecordWithNewSchema(genericRecord, newSchema, Collections.emptyMap()); | ||
| // do not preserve FILENAME_METADATA_FIELD | ||
| newRecord.put(HoodieRecord.FILENAME_METADATA_FIELD_POS, fileName); | ||
| return newRecord; | ||
|
|
@@ -745,7 +752,7 @@ public static Object getRecordColumnValues(HoodieRecord<? extends HoodieRecordPa | |
| * @return newRecord for new Schema | ||
| */ | ||
| public static GenericRecord rewriteRecordWithNewSchema(IndexedRecord oldRecord, Schema newSchema, Map<String, String> renameCols) { | ||
| Object newRecord = rewriteRecordWithNewSchema(oldRecord, oldRecord.getSchema(), newSchema, renameCols, new LinkedList<>()); | ||
| Object newRecord = rewriteRecordWithNewSchema(oldRecord, oldRecord.getSchema(), newSchema, renameCols, new ArrayDeque<>()); | ||
| return (GenericData.Record) newRecord; | ||
| } | ||
|
|
||
|
|
@@ -773,39 +780,32 @@ private static Object rewriteRecordWithNewSchema(Object oldRecord, Schema oldSch | |
| } | ||
| IndexedRecord indexedRecord = (IndexedRecord) oldRecord; | ||
| List<Schema.Field> fields = newSchema.getFields(); | ||
| Map<Integer, Object> helper = new HashMap<>(); | ||
|
|
||
| GenericData.Record newRecord = new GenericData.Record(newSchema); | ||
| for (int i = 0; i < fields.size(); i++) { | ||
| Schema.Field field = fields.get(i); | ||
| String fieldName = field.name(); | ||
| fieldNames.push(fieldName); | ||
| if (oldSchema.getField(field.name()) != null) { | ||
| Schema.Field oldField = oldSchema.getField(field.name()); | ||
| helper.put(i, rewriteRecordWithNewSchema(indexedRecord.get(oldField.pos()), oldField.schema(), fields.get(i).schema(), renameCols, fieldNames)); | ||
| newRecord.put(i, rewriteRecordWithNewSchema(indexedRecord.get(oldField.pos()), oldField.schema(), fields.get(i).schema(), renameCols, fieldNames)); | ||
| } else { | ||
| String fieldFullName = createFullName(fieldNames); | ||
| String[] colNamePartsFromOldSchema = renameCols.getOrDefault(fieldFullName, "").split("\\."); | ||
| String lastColNameFromOldSchema = colNamePartsFromOldSchema[colNamePartsFromOldSchema.length - 1]; | ||
| String fieldNameFromOldSchema = renameCols.getOrDefault(fieldFullName, ""); | ||
| // deal with rename | ||
| if (oldSchema.getField(field.name()) == null && oldSchema.getField(lastColNameFromOldSchema) != null) { | ||
| if (oldSchema.getField(field.name()) == null && oldSchema.getField(fieldNameFromOldSchema) != null) { | ||
| // find rename | ||
| Schema.Field oldField = oldSchema.getField(lastColNameFromOldSchema); | ||
| helper.put(i, rewriteRecordWithNewSchema(indexedRecord.get(oldField.pos()), oldField.schema(), fields.get(i).schema(), renameCols, fieldNames)); | ||
| } | ||
| } | ||
| fieldNames.pop(); | ||
| } | ||
| GenericData.Record newRecord = new GenericData.Record(newSchema); | ||
| for (int i = 0; i < fields.size(); i++) { | ||
| if (helper.containsKey(i)) { | ||
| newRecord.put(i, helper.get(i)); | ||
| } else { | ||
| if (fields.get(i).defaultVal() instanceof JsonProperties.Null) { | ||
| newRecord.put(i, null); | ||
| Schema.Field oldField = oldSchema.getField(fieldNameFromOldSchema); | ||
| newRecord.put(i, rewriteRecordWithNewSchema(indexedRecord.get(oldField.pos()), oldField.schema(), fields.get(i).schema(), renameCols, fieldNames)); | ||
| } else { | ||
| newRecord.put(i, fields.get(i).defaultVal()); | ||
| // deal with default value | ||
| if (fields.get(i).defaultVal() instanceof JsonProperties.Null) { | ||
| newRecord.put(i, null); | ||
| } else { | ||
| newRecord.put(i, fields.get(i).defaultVal()); | ||
| } | ||
| } | ||
| } | ||
| fieldNames.pop(); | ||
| } | ||
| return newRecord; | ||
| case ARRAY: | ||
|
|
@@ -814,7 +814,7 @@ private static Object rewriteRecordWithNewSchema(Object oldRecord, Schema oldSch | |
| } | ||
| Collection array = (Collection)oldRecord; | ||
| List<Object> newArray = new ArrayList(); | ||
| fieldNames.push("element"); | ||
| fieldNames.push(ARRAY_TYPE_ELEMENT_NAME); | ||
| for (Object element : array) { | ||
| newArray.add(rewriteRecordWithNewSchema(element, oldSchema.getElementType(), newSchema.getElementType(), renameCols, fieldNames)); | ||
| } | ||
|
|
@@ -826,7 +826,7 @@ private static Object rewriteRecordWithNewSchema(Object oldRecord, Schema oldSch | |
| } | ||
| Map<Object, Object> map = (Map<Object, Object>) oldRecord; | ||
| Map<Object, Object> newMap = new HashMap<>(); | ||
| fieldNames.push("value"); | ||
| fieldNames.push(MAP_TYPE_VALUE_NAME); | ||
| for (Map.Entry<Object, Object> entry : map.entrySet()) { | ||
| newMap.put(entry.getKey(), rewriteRecordWithNewSchema(entry.getValue(), oldSchema.getValueType(), newSchema.getValueType(), renameCols, fieldNames)); | ||
| } | ||
|
|
@@ -840,13 +840,9 @@ private static Object rewriteRecordWithNewSchema(Object oldRecord, Schema oldSch | |
| } | ||
|
|
||
| private static String createFullName(Deque<String> fieldNames) { | ||
| String result = ""; | ||
| if (!fieldNames.isEmpty()) { | ||
| List<String> parentNames = new ArrayList<>(); | ||
| fieldNames.descendingIterator().forEachRemaining(parentNames::add); | ||
| result = parentNames.stream().collect(Collectors.joining(".")); | ||
| } | ||
| return result; | ||
| return StreamSupport | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for Stream you can just do |
||
| .stream(Spliterators.spliteratorUnknownSize(fieldNames.descendingIterator(), Spliterator.ORDERED), false) | ||
| .collect(Collectors.joining(".")); | ||
| } | ||
|
|
||
| private static Object rewritePrimaryType(Object oldValue, Schema oldSchema, Schema newSchema) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the variable only used by this method, there is no need to make static variables, the JVM would cache the string constants.