Skip to content

Commit

Permalink
NIFI-8120 Updated normalizeValue() to use LocalDate.ofEpochDay()
Browse files Browse the repository at this point in the history
  • Loading branch information
exceptionfactory committed Jan 9, 2021
1 parent 05d728c commit 6a95ff6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,8 @@ private static Object normalizeValue(final Object value, final Schema avroSchema
final String logicalName = logicalType.getName();
if (LOGICAL_TYPE_DATE.equals(logicalName)) {
// date logical name means that the value is number of days since Jan 1, 1970
return new java.sql.Date(TimeUnit.DAYS.toMillis((int) value));
final LocalDate localDate = LocalDate.ofEpochDay((int) value);
return java.sql.Date.valueOf(localDate);
} else if (LOGICAL_TYPE_TIME_MILLIS.equals(logicalName)) {
// time-millis logical name means that the value is number of milliseconds since midnight.
return new java.sql.Time((int) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,34 @@ public void testDateStringConversion() {
assertConvertedDateEqualsEpochDays("1970-01-31", 30);
}

@Test
public void testConvertAvroRecordToMapWithLogicalDateField() throws IOException {
final String dateField = "dateField";
final String dateFormatted = "1970-01-31";
final int epochDaysExpected = 30;
final Date dateExpected = Date.valueOf(dateFormatted);

final RecordSchema recordSchema = new SimpleRecordSchema(Collections.singletonList(
new RecordField(dateField, RecordFieldType.DATE.getDataType())
));
final Map<String, Object> mapRecordValues = Collections.singletonMap(dateField, dateFormatted);
final MapRecord mapRecord = new MapRecord(recordSchema, mapRecordValues);

final Schema dateFieldSchema = Schema.create(Type.INT);
LogicalTypes.date().addToSchema(dateFieldSchema);
final Schema avroRecordSchema = Schema.createRecord(Collections.singletonList(
new Field(dateField, dateFieldSchema, null, (Object) null)
));

final GenericRecord avroRecord = AvroTypeUtil.createAvroRecord(mapRecord, avroRecordSchema);
final Object avroRecordEpochDays = avroRecord.get(dateField);
assertEquals("Epoch Days not matched", epochDaysExpected, avroRecordEpochDays);

final Map<String, Object> recordMap = AvroTypeUtil.convertAvroRecordToMap(avroRecord, recordSchema);
final Object dateConverted = recordMap.get(dateField);
assertEquals("Date not matched", dateExpected, dateConverted);
}

@Test
public void testFixedDecimalConversion(){
final LogicalTypes.Decimal decimalType = LogicalTypes.decimal(18, 8);
Expand Down

0 comments on commit 6a95ff6

Please sign in to comment.