You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Everett Anderson <ev...@nuna.com> on 2016/05/30 19:43:21 UTC

Decimal types in IDL and generated Java not carrying logical types to schemas

Hi,

I was trying out the decimal Logical Type in 1.8.1 in an example record
schema and noticed that while the logical type's information is preserved
in the JSON properties, it isn't actually respected or returned from the
Schema accessor.

Is it supported to use logical types in the IDL or are they primarily for
internal use? Is there a better way to specify decimal types in the IDL?

Example: user.avsc

{
 "namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {
       "name": "name", "type": "string"
     },
     {
       "name": "account_balance",
       "type": "bytes",
       "logicalType": "decimal",
       "precision": 4,
       "scale": 2
     }
 ]
}

In the generated code,

User.getClassSchema().getField("account_balance").schema().getLogicalType();

is null. The Schema.Field's 'props' map from the JSON retains
'logicalType', 'precision', and 'scale', but its schema() doesn't have this
info or a non-null logical type member.

It looks like maybe the Schema.parse() code that parses the JSON only looks
for 'logicalType' in a record type, and when it recurses down, it only sees
this as a primitive 'bytes' type --

https://github.com/apache/avro/blob/branch-1.8/lang/java/avro/src/main/java/org/apache/avro/Schema.java#L1326

Re: Decimal types in IDL and generated Java not carrying logical types to schemas

Posted by Everett Anderson <ev...@nuna.com>.
(Sorry -- just realized in all these cases I should've been saying 'JSON
schema' instead of 'IDL'!)

On Mon, May 30, 2016 at 12:43 PM, Everett Anderson <ev...@nuna.com> wrote:

> Hi,
>
> I was trying out the decimal Logical Type in 1.8.1 in an example record
> schema and noticed that while the logical type's information is preserved
> in the JSON properties, it isn't actually respected or returned from the
> Schema accessor.
>
> Is it supported to use logical types in the IDL or are they primarily for
> internal use? Is there a better way to specify decimal types in the IDL?
>
> Example: user.avsc
>
> {
>  "namespace": "example.avro",
>  "type": "record",
>  "name": "User",
>  "fields": [
>      {
>        "name": "name", "type": "string"
>      },
>      {
>        "name": "account_balance",
>        "type": "bytes",
>        "logicalType": "decimal",
>        "precision": 4,
>        "scale": 2
>      }
>  ]
> }
>
> In the generated code,
>
>
> User.getClassSchema().getField("account_balance").schema().getLogicalType();
>
> is null. The Schema.Field's 'props' map from the JSON retains
> 'logicalType', 'precision', and 'scale', but its schema() doesn't have this
> info or a non-null logical type member.
>
> It looks like maybe the Schema.parse() code that parses the JSON only
> looks for 'logicalType' in a record type, and when it recurses down, it
> only sees this as a primitive 'bytes' type --
>
>
> https://github.com/apache/avro/blob/branch-1.8/lang/java/avro/src/main/java/org/apache/avro/Schema.java#L1326
>
>
>