[ 
https://issues.apache.org/jira/browse/AVRO-2858?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17130748#comment-17130748
 ] 

Ryan Skraba commented on AVRO-2858:
-----------------------------------

I saw the PR!  Thanks for fixing it up.  It looks like this brings in an Avro 
schema specification change, but not a major one, and I think the intention is 
correct!

For the moment, the default values for a double field can *only* be a [JSON 
number|https://avro.apache.org/docs/1.9.2/spec.html#schema_record].  In your 
example it's a string.  We should probably clarify the spec to include this 
possibility (to avoid excluding NaN and Infinity from default values).

I think the spec change is the right thing to do, and permit JSON numbers or 
JSON strings for all numeric default values.  This would have another benefit 
-- if I remember correctly, the precision of a JSON number depends on the 
implementation of the JSON library.  We've definitely encountered some 
libraries that don't keep enough precision to store an 8 byte Long!

I'll ask on the mailing list if there's any strong objection.

> SchemaBuilder.GenericDefault withDefault method does not handle Double.NaN 
> correctly
> ------------------------------------------------------------------------------------
>
>                 Key: AVRO-2858
>                 URL: https://issues.apache.org/jira/browse/AVRO-2858
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.9.2
>            Reporter: Gordon Wang
>            Priority: Major
>         Attachments: AVRO-2858.patch
>
>
> Upgraded to Avro 1.9.2 and found when using the withDefault method in 
> SchemaBuilder.GenericDefault to set default value for a Double typed field, 
> it cannot set Double.NaN correctly, throwing out below error:
> {code}
>  Exception in thread "main" org.apache.avro.AvroTypeException: Invalid 
> default for field field: "NaN" not a "double"
>  at org.apache.avro.Schema.validateDefault(Schema.java:1540)
>  at org.apache.avro.Schema.access$500(Schema.java:87)
>  at org.apache.avro.Schema$Field.<init>(Schema.java:521)
>  at 
> org.apache.avro.SchemaBuilder$FieldBuilder.completeField(SchemaBuilder.java:2238)
>  at 
> org.apache.avro.SchemaBuilder$FieldBuilder.completeField(SchemaBuilder.java:2230)
>  at 
> org.apache.avro.SchemaBuilder$FieldBuilder.access$5300(SchemaBuilder.java:2148)
>  at 
> org.apache.avro.SchemaBuilder$GenericDefault.withDefault(SchemaBuilder.java:2545)
> {code}
> After debugging it seemed the *toJsonNode* method in SchemaBuilder is 
> returning a TextNode rather than a NUMBER typed one.
> The problem can be replicated using below sample code:
> {code}
> SchemaBuilder.FieldBuilder fieldBuilder = 
> SchemaBuilder.record("record").fields().name("field");
> Schema fieldSchema = Schema.create(Schema.Type.DOUBLE);
> SchemaBuilder.GenericDefault genericDefault = fieldBuilder.type(fieldSchema);
> genericDefault.withDefault(Double.NaN);
> // Exception thrown
> {code}
>  



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

Reply via email to