-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-1441] Fixing HoodieAvroUtils.rewriteRecord for nested record schema evolution #2982
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
nsivabalan
wants to merge
1
commit into
apache:master
from
nsivabalan:hoodieAvroUtils_rewriteToEvolvedSchema
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import org.apache.hudi.exception.SchemaCompatibilityException; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.SchemaBuilder; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -236,4 +237,81 @@ public void testGetNestedFieldVal() { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRewriteToEvolvedNestedRecord() throws Exception { | ||
|
Member
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. remove |
||
| // schema definition for inner record | ||
| Schema nestedSchema = SchemaBuilder.record("inner_rec").fields().requiredDouble("color_id").endRecord(); | ||
| Schema evolvedNestedSchema = SchemaBuilder.record("inner_rec").fields().requiredDouble("color_id") | ||
| .optionalString("color_name").endRecord(); | ||
|
|
||
| // schema definition for outer record | ||
| Schema recordSchema = SchemaBuilder.record("outer_rec").fields().requiredDouble("timestamp") | ||
| .requiredString("_row_key").requiredString("non_pii_col").name("color_rec").type(nestedSchema) | ||
| .noDefault().requiredString("pii_col").endRecord(); | ||
| Schema evolvedRecordSchema = SchemaBuilder.record("outer_rec").fields().requiredDouble("timestamp") | ||
| .requiredString("_row_key").requiredString("non_pii_col").name("color_rec").type(evolvedNestedSchema) | ||
| .noDefault().requiredString("pii_col").endRecord(); | ||
|
|
||
| // populate inner record, with fewer fields | ||
| GenericRecord nestedRec = new GenericData.Record(nestedSchema); | ||
| nestedRec.put("color_id", 55.5); | ||
|
|
||
| // populate outer record | ||
| GenericRecord rec = new GenericData.Record(recordSchema); | ||
| rec.put("timestamp", 3.5); | ||
| rec.put("_row_key", "key1"); | ||
| rec.put("non_pii_col", "val1"); | ||
| rec.put("color_rec", nestedRec); | ||
| rec.put("pii_col", "val2"); | ||
|
|
||
| // rewrite record with less number of fields into an evolved record (with optional fields added). | ||
| try { | ||
| GenericRecord newRecord = HoodieAvroUtils.rewriteRecord(rec, evolvedRecordSchema); | ||
| assertEquals("val2", newRecord.get("pii_col")); | ||
| assertEquals(null, ((GenericRecord)newRecord.get("color_rec")).get("color_name")); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
|
Member
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. let's remove |
||
| assertTrue(false, "Failed to rewrite Record"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRewriteToShorterRecord() throws Exception { | ||
| // schema definition for inner record | ||
| Schema nestedSchema = SchemaBuilder.record("inner_rec").fields().requiredDouble("color_id").endRecord(); | ||
| Schema largerNestedSchema = SchemaBuilder.record("inner_rec").fields().requiredDouble("color_id") | ||
| .requiredString("color_name").endRecord(); | ||
|
|
||
| // schema definition for outer record | ||
| Schema recordSchema = SchemaBuilder.record("outer_rec").fields().requiredDouble("timestamp") | ||
| .requiredString("_row_key").requiredString("non_pii_col").name("color_rec").type(nestedSchema) | ||
| .noDefault().requiredString("pii_col").endRecord(); | ||
| Schema largerRecordSchema = SchemaBuilder.record("outer_rec").fields().requiredDouble("timestamp") | ||
| .requiredString("_row_key").requiredString("non_pii_col").name("color_rec").type(largerNestedSchema) | ||
| .noDefault().requiredString("pii_col").endRecord(); | ||
|
|
||
| // populate larger inner record | ||
| GenericRecord nestedRec = new GenericData.Record(largerNestedSchema); | ||
| nestedRec.put("color_id", 55.5); | ||
| nestedRec.put("color_name", "blue"); | ||
|
|
||
| // populate outer record, with larger inner record | ||
| GenericRecord largerRec = new GenericData.Record(largerRecordSchema); | ||
| largerRec.put("timestamp", 3.5); | ||
| largerRec.put("_row_key", "key1"); | ||
| largerRec.put("non_pii_col", "val1"); | ||
| largerRec.put("color_rec", nestedRec); | ||
| largerRec.put("pii_col", "val2"); | ||
|
|
||
| // rewrite record with larger inner record to record with shorter inner record. | ||
| try { | ||
| GenericRecord shorterRec = HoodieAvroUtils.rewriteRecord(largerRec, recordSchema); | ||
| assertEquals("val2", shorterRec.get("pii_col")); | ||
| assertEquals(null, ((GenericRecord)shorterRec.get("color_rec")).get("color_name")); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| assertTrue(false, "Failed to rewrite Record"); | ||
| } | ||
| } | ||
| } | ||
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.
instead of wrapper class, can we use the primitive type int here?