Repository: nifi Updated Branches: refs/heads/master 102a5288e -> 023f0c41c
NIFI-5662 - Support for generic fixed when using decimal logical type Signed-off-by: Pierre Villard <[email protected]> This closes #3175. Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/023f0c41 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/023f0c41 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/023f0c41 Branch: refs/heads/master Commit: 023f0c41cec8aca798f17cb5f9ef13c589db23f8 Parents: 102a528 Author: gkkorir <[email protected]> Authored: Fri Nov 16 15:49:24 2018 +0300 Committer: Pierre Villard <[email protected]> Committed: Sat Nov 17 16:36:04 2018 +0100 ---------------------------------------------------------------------- .../java/org/apache/nifi/avro/AvroTypeUtil.java | 4 +++- .../org/apache/nifi/avro/TestAvroTypeUtil.java | 25 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/023f0c41/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java index 9e023cc..2e2c8f7 100755 --- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java @@ -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); http://git-wip-us.apache.org/repos/asf/nifi/blob/023f0c41/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java index 83d54c6..1f6c29b 100755 --- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java @@ -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; @@ -397,6 +398,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")); RecordSchema recordSchema = AvroTypeUtil.createSchema(schema);
