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

Luke Jackson edited comment on AVRO-2858 at 6/30/20, 2:14 AM:
--------------------------------------------------------------

An alternative patch is to rely on the underlying support in Jackson for 
non-numeric numbers.

i.e. in Schema.java:
{code:java}
static {
 FACTORY.enable(JsonParser.Feature.ALLOW_COMMENTS);
 FACTORY.setCodec(MAPPER);
} {code}
add the additional feature:
{code:java}
static {
 FACTORY.enable(JsonParser.Feature.ALLOW_COMMENTS); 
 FACTORY.enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
 FACTORY.setCodec(MAPPER);
} {code}
This would allow NaN and positive/negative infinity to be specified as literals 
and not as strings.

JavaDoc for the Jackson feature:
{code:java}
/**
 * Feature that allows parser to recognize set of
 * "Not-a-Number" (NaN) tokens as legal floating number
 * values (similar to how many other data formats and
 * programming language source code allows it).
 * Specific subset contains values that
 * <a href="http://www.w3.org/TR/xmlschema-2/";>XML Schema</a>
 * (see section 3.2.4.1, Lexical Representation)
 * allows (tokens are quoted contents, not including quotes):
 *<ul>
 * <li>"INF" (for positive infinity), as well as alias of "Infinity"
 * <li>"-INF" (for negative infinity), alias "-Infinity"
 * <li>"NaN" (for other not-a-numbers, like result of division by zero)
 *</ul>
 *<p>
 * Since JSON specification does not allow use of such values,
 * this is a non-standard feature, and as such disabled by default.
 */
@SuppressWarnings("deprecation")
ALLOW_NON_NUMERIC_NUMBERS(false, JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS),

{code}


was (Author: lukejackson):
An alternative patch is to rely on the underlying support in Jackson for 
non-numeric numbers.

i.e. in Schema.java:
{code:java}
static {
 FACTORY.enable(JsonParser.Feature.ALLOW_COMMENTS);
 FACTORY.setCodec(MAPPER);
} {code}
add the additional feature:
{code:java}
static {
 FACTORY.enable(JsonParser.Feature.ALLOW_COMMENTS); 
 FACTORY.enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
 FACTORY.setCodec(MAPPER);
} {code}
This would allow NaN and positive/negative infinity to be specified as numbers 
and not as strings.

JavaDoc for the Jackson feature:

{code}

/**
 * Feature that allows parser to recognize set of
 * "Not-a-Number" (NaN) tokens as legal floating number
 * values (similar to how many other data formats and
 * programming language source code allows it).
 * Specific subset contains values that
 * <a href="http://www.w3.org/TR/xmlschema-2/";>XML Schema</a>
 * (see section 3.2.4.1, Lexical Representation)
 * allows (tokens are quoted contents, not including quotes):
 *<ul>
 * <li>"INF" (for positive infinity), as well as alias of "Infinity"
 * <li>"-INF" (for negative infinity), alias "-Infinity"
 * <li>"NaN" (for other not-a-numbers, like result of division by zero)
 *</ul>
 *<p>
 * Since JSON specification does not allow use of such values,
 * this is a non-standard feature, and as such disabled by default.
 */
@SuppressWarnings("deprecation")
ALLOW_NON_NUMERIC_NUMBERS(false, JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS),

{code}

> 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