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

Gordon Wang edited comment on AVRO-2858 at 6/10/20, 9:13 AM:
-------------------------------------------------------------

Actually currently we are able to create schemas with double/float with NaN 
default values with below approach:
{code}
    Schema schema = Schema.createRecord("testRecord", "doc", "namespace", 
false);
    List<Schema.Field> fields = new ArrayList<>();
    fields.add(new Schema.Field("field_name", 
Schema.create(Schema.Type.DOUBLE), "doc", Double.NaN));
    schema.setFields(fields);
{code}
which generates a schema looks like below:
{code}
{"type":"record","name":"testRecord","namespace":"namespace","doc":"doc","fields":[{"name":"field_name","type":"double","doc":"doc","default":"NaN"}]}
{code}
I noticed if we are using *JacksonUtils.toJsonNode*, like under the situation 
we have as above example, then NaN can be recognized as number type Json node 
correctly, but SchemaBuilder is using its own toJsonNode method which converts 
a Double/Float.NaN to a TextNode. I have proposed a fix in linked PR in GitHub, 
intend to use the same JacksonUtils.toJsonNode method in SchemaBuilder to allow 
us setting a NaN default value for double/float fields. Also uploaded the patch 
with this issue.


was (Author: bulbfreeman):
Actually currently we are able to create schemas with double/float with NaN 
default values with below approach:
{code}
    Schema schema = Schema.createRecord("testRecord", "doc", "namespace", 
false);
    List<Schema.Field> fields = new ArrayList<>();
    fields.add(new Schema.Field("field_name", 
Schema.create(Schema.Type.DOUBLE), "doc", Double.NaN));
    schema.setFields(fields);
{code}
which generates a schema looks like below:
{code}
{"type":"record","name":"testRecord","namespace":"namespace","doc":"doc","fields":[{"name":"field_name","type":"double","doc":"doc","default":"NaN"}]}
{code}
I noticed if we are using *JacksonUtils.toJsonNode*, like under the situation 
we have as above example, then NaN can be recognized as number type Json node 
correctly, but SchemaBuilder is using its own toJsonNode method which converts 
a Double/Float.NaN to a TextNode. I have proposed a fix in linked PR in GitHub, 
intend to use the same JacksonUtils.toJsonNode method in SchemaBuilder to allow 
us setting a NaN default value for double/float fields.

> 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