NIFI-5662 - Support for generic fixed when using decimal logical type

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #3175.
This commit is contained in:
gkkorir 2018-11-16 15:49:24 +03:00 committed by Pierre Villard
parent 102a5288ef
commit 023f0c41ce
2 changed files with 28 additions and 1 deletions

View File

@ -631,7 +631,9 @@ public class AvroTypeUtil {
final int desiredScale = decimalType.getScale();
final BigDecimal decimal = rawDecimal.scale() == desiredScale
? rawDecimal : rawDecimal.setScale(desiredScale, BigDecimal.ROUND_HALF_UP);
return new Conversions.DecimalConversion().toBytes(decimal, fieldSchema, logicalType);
return fieldSchema.getType() == Type.BYTES
? new Conversions.DecimalConversion().toBytes(decimal, fieldSchema, logicalType) //return GenericByte
: new Conversions.DecimalConversion().toFixed(decimal, fieldSchema, logicalType); //return GenericFixed
}
if (rawValue instanceof byte[]) {
return ByteBuffer.wrap((byte[]) rawValue);

View File

@ -42,6 +42,7 @@ import org.apache.avro.Schema.Type;
import org.apache.avro.file.DataFileStream;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericFixed;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.GenericRecordBuilder;
import org.apache.avro.generic.GenericData.Record;
@ -396,6 +397,30 @@ public class TestAvroTypeUtil {
}
@Test
public void testBytesDecimalConversion(){
final LogicalTypes.Decimal decimalType = LogicalTypes.decimal(18, 8);
final Schema fieldSchema = Schema.create(Type.BYTES);
decimalType.addToSchema(fieldSchema);
final Object convertedValue = AvroTypeUtil.convertToAvroObject("2.5", fieldSchema, StandardCharsets.UTF_8);
assertTrue(convertedValue instanceof ByteBuffer);
final ByteBuffer serializedBytes = (ByteBuffer)convertedValue;
final BigDecimal bigDecimal = new Conversions.DecimalConversion().fromBytes(serializedBytes, fieldSchema, decimalType);
assertEquals(new BigDecimal("2.5").setScale(8), bigDecimal);
}
@Test
public void testFixedDecimalConversion(){
final LogicalTypes.Decimal decimalType = LogicalTypes.decimal(18, 8);
final Schema fieldSchema = Schema.createFixed("mydecimal", "no doc", "myspace", 18);
decimalType.addToSchema(fieldSchema);
final Object convertedValue = AvroTypeUtil.convertToAvroObject("2.5", fieldSchema, StandardCharsets.UTF_8);
assertTrue(convertedValue instanceof GenericFixed);
final GenericFixed genericFixed = (GenericFixed)convertedValue;
final BigDecimal bigDecimal = new Conversions.DecimalConversion().fromFixed(genericFixed, fieldSchema, decimalType);
assertEquals(new BigDecimal("2.5").setScale(8), bigDecimal);
}
@Test
public void testSchemaNameNotEmpty() throws IOException {
Schema schema = new Schema.Parser().parse(getClass().getResourceAsStream("simpleSchema.json"));