Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,12 @@ public static Object getNestedFieldVal(GenericRecord record, String fieldName, b

if (returnNullIfNotFound) {
return null;
} else {
} else if (valueNode.getSchema().getField(parts[i]) == null) {
throw new HoodieException(
fieldName + "(Part -" + parts[i] + ") field not found in record. Acceptable fields were :"
+ valueNode.getSchema().getFields().stream().map(Field::name).collect(Collectors.toList()));
} else {
throw new HoodieException("The value of " + parts[i] + " can not be null");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,34 @@ public void testAddingAndRemovingMetadataFields() {
Schema schemaWithoutMetaCols = HoodieAvroUtils.removeMetadataFields(schemaWithMetaCols);
assertEquals(schemaWithoutMetaCols.getFields().size(), NUM_FIELDS_IN_EXAMPLE_SCHEMA);
}

@Test
public void testGetNestedFieldVal() {
GenericRecord rec = new GenericData.Record(new Schema.Parser().parse(EXAMPLE_SCHEMA));
rec.put("_row_key", "key1");
rec.put("non_pii_col", "val1");
rec.put("pii_col", "val2");

Object rowKey = HoodieAvroUtils.getNestedFieldVal(rec, "_row_key", true);
assertEquals(rowKey, "key1");

Object rowKeyNotExist = HoodieAvroUtils.getNestedFieldVal(rec, "fake_key", true);
assertNull(rowKeyNotExist);

// Field does not exist
try {
HoodieAvroUtils.getNestedFieldVal(rec, "fake_key", false);
} catch (Exception e) {
assertEquals("fake_key(Part -fake_key) field not found in record. Acceptable fields were :[timestamp, _row_key, non_pii_col, pii_col]",
e.getMessage());
}

// Field exist while value not
try {
HoodieAvroUtils.getNestedFieldVal(rec, "timestamp", false);
} catch (Exception e) {
assertEquals("The value of timestamp can not be null", e.getMessage());
}
}

}