Valentin Nikotin created AVRO-2636:
--------------------------------------

             Summary: GenericData defaultValueCache caches mutable ByteBuffers
                 Key: AVRO-2636
                 URL: https://issues.apache.org/jira/browse/AVRO-2636
             Project: Apache Avro
          Issue Type: Bug
          Components: java
            Reporter: Valentin Nikotin


It appears that for default value for Byte type (and Decimal logical type if it 
uses underlying Bytes type) value rendered with getDefaultValue is cached. This 
leads to bugs when you read the same value (for example if converted with 
DecimalConversion). For single thread environment workaround would be to reset 
ByteBuffer after read, but in concurrent environment we should not cache 
mutable objects.

 
{code:java}
@Test(expected=NumberFormatException.class)
public void testReuse() {
    Conversions.DecimalConversion decimalConversion =
            new Conversions.DecimalConversion();

    LogicalType logicalDecimal =
            LogicalTypes.decimal(38, 9);

    ByteBuffer defaultValue =
            decimalConversion.toBytes(
                    BigDecimal.valueOf(42L).setScale(9),
                    null,
                    logicalDecimal);

    Schema schema = SchemaBuilder
            .record("test")
            .fields()
            .name("decimal")
            
.type(logicalDecimal.addToSchema(SchemaBuilder.builder().bytesType()))
            .withDefault(defaultValue)
            .endRecord();

    BigDecimal firstRead = decimalConversion
            .fromBytes(
                    (ByteBuffer) 
GenericData.get().getDefaultValue(schema.getField("decimal")),
                    null,
                    logicalDecimal);

    BigDecimal secondRead = decimalConversion
            .fromBytes(
                    (ByteBuffer) 
GenericData.get().getDefaultValue(schema.getField("decimal")),
                    null,
                    logicalDecimal);

}
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to