You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Valentin Nikotin (Jira)" <ji...@apache.org> on 2019/11/20 10:03:00 UTC

[jira] [Created] (AVRO-2636) GenericData defaultValueCache caches mutable ByteBuffers

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)