Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2086

Can't create specific record with a single Decimal field with default value

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Patch Available
    • Major
    • Resolution: Unresolved
    • 1.8.2
    • None
    • java
    • None

    Description

      Given a simple record schema with a single byte field annotated with Decimal logical type and has default value set:

      {
         "type":"record",
         "name":"TestRecordWithDecimalFieldWithDefault",
         "namespace":"org.apache.avro.specific",
         "fields":[
            {
               "name":"f1",
               "type":{
                  "type":"bytes",
                  "logicalType":"decimal",
                  "precision":9,
                  "scale":2
               },
               "default":"0.00"
            }
         ]
      }
      

      compiled with
      avro-tools compile -bigDecimal schema TestRecordWithDecimalFieldWithDefault.avsc .

      The following test case (which should use the decimal's default value):

        @Test
        public void testRecordWithDecimalLogicalTypeWithDefault() throws IOException {
          SpecificData.get().addLogicalTypeConversion(new Conversions.DecimalConversion());
          TestRecordWithDecimalFieldWithDefault record = TestRecordWithDecimalFieldWithDefault.newBuilder().build();
          File data = write(TestRecordWithDecimalFieldWithDefault.getClassSchema(), record);
          List<TestRecordWithDecimalFieldWithDefault> actual = read(
            TestRecordWithDecimalFieldWithDefault.getClassSchema(), data);
      
          assertEquals(new BigDecimal(0.00).setScale(2), actual.get(0).getF1());
        }
      

      fails with

      org.apache.avro.AvroRuntimeException: org.apache.avro.AvroRuntimeException: Cannot convert 8083333.60:BigDecimal: expected generic type
      
      	at org.apache.avro.specific.TestRecordWithDecimalFieldWithDefault$Builder.build(TestRecordWithDecimalFieldWithDefault.java:239)
      	at org.apache.avro.specific.TestSpecificLogicalTypes.testRecordWithDecimalLogicalTypeWithDefault(TestSpecificLogicalTypes.java:123)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
      	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
      	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
      	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
      	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
      Caused by: org.apache.avro.AvroRuntimeException: Cannot convert 8083333.60:BigDecimal: expected generic type
      	at org.apache.avro.Conversions.convertToLogicalType(Conversions.java:172)
      	at org.apache.avro.data.RecordBuilderBase.defaultValue(RecordBuilderBase.java:159)
      	at org.apache.avro.specific.TestRecordWithDecimalFieldWithDefault$Builder.build(TestRecordWithDecimalFieldWithDefault.java:236)
      	... 30 more
      Caused by: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.nio.ByteBuffer
      	at org.apache.avro.Conversions.convertToLogicalType(Conversions.java:162)
      	... 32 more
      

      Attachments

        Issue Links

          Activity

            People

              nkollar Nándor Kollár
              nkollar Nándor Kollár
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: