ratatool icon indicating copy to clipboard operation
ratatool copied to clipboard

avroOf does not support logical types

Open drobert opened this issue 5 years ago • 4 comments

I just started using ratatool today and am using it for CaseClass and Avro generation. I believe there's an issue in the way it handles, or fails to handle, logical types in avro.

I'm using java/scala type BigDecimal, which in schema looks like this (newlines added by me for easier reading):

{
  "name":"cost",
  "type": {
    "type":"bytes",
    "logicalType":"decimal",
    "precision":10,
    "scale":2
  }
}

Decimal logical type is documented here: https://avro.apache.org/docs/current/spec.html#Logical+Types

What's happening when attempting to use avroOf[MyGenericRecord] is the type pattern match (found in AvroGenerator.scala) hits Schema.Type.BYTES and can generate byte[0].

Later, the AvroCodec, which does honor the BigDecimal logical type, attempts to convert byte[0] into a BigInteger (see https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/Conversions.java#L84), which fails with:

NumberFormatException("Zero length BigInteger")

I feel there's a few options which could be done independently here (I'll PR as I'm able):

  1. document the current limitation
  2. add support for common, documented logical types to ratatool. likely these can be done in independent PRs

drobert avatar Nov 20 '19 22:11 drobert

I'm spending some time on this, and will try to create a PR late next week..!

anish749 avatar Aug 27 '20 09:08 anish749

I see a corresponding problem for { "type": "int", "logicalType": "date" } which gives a java.lang.ClassCastException: java.lang.Integer cannot be cast to org.joda.time.LocalDate.

rydenius avatar Feb 09 '21 16:02 rydenius

Yeah I ran into this issue as well while trying to generate data for something with a decimal logical type. This would be super helpful to have.

ryanstull avatar May 27 '21 16:05 ryanstull

Ran into the same issue, is there a workaround for this?

aasokan avatar Feb 04 '22 17:02 aasokan

Fixed by #691

RustedBones avatar Feb 28 '24 08:02 RustedBones