mirror of https://github.com/apache/nifi.git
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:
parent
102a5288ef
commit
023f0c41ce
|
@ -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);
|
||||
|
|
|
@ -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"));
|
||||
|
|
Loading…
Reference in New Issue