You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2011/12/09 21:27:30 UTC

svn commit: r1212611 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/ lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/ lang/java/compiler/src/test/idl/input/ lang/java/compiler/src/test/idl/output/ lang/java/ipc/src/te...

Author: cutting
Date: Fri Dec  9 20:27:29 2011
New Revision: 1212611

URL: http://svn.apache.org/viewvc?rev=1212611&view=rev
Log:
AVRO-972. Java: Add support for Infinity and NaN as default values for float and double.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    avro/trunk/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
    avro/trunk/lang/java/compiler/src/test/idl/input/simple.avdl
    avro/trunk/lang/java/compiler/src/test/idl/output/simple.avpr
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Dec  9 20:27:29 2011
@@ -14,6 +14,11 @@ Avro 1.6.2 (unreleased)
     AVRO-953. Python: Permit users to override HTTP path in RPC.
     (Craig Landry via cutting)
 
+    AVRO-972. Java: Add support for Infinity and NaN as default values
+    for float and double.  Since JSON does not permit these as numeric
+    types, we use the strings "NaN", "Infinity" and "-Infinity" in
+    schemas.  These are also permitted in IDL.  (cutting)
+
   BUG FIXES
 
     AVRO-962. Java: Fix Maven plugin to support string type override.

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java Fri Dec  9 20:27:29 2011
@@ -41,6 +41,7 @@ import org.codehaus.jackson.JsonParseExc
 import org.codehaus.jackson.JsonParser;
 import org.codehaus.jackson.JsonGenerator;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.DoubleNode;
 
 /** An abstract data type.
  * <p>A schema may be one of:
@@ -443,14 +444,19 @@ public abstract class Schema {
       Field that = (Field) other;
       return (name.equals(that.name)) &&
         (schema.equals(that.schema)) &&
-        (defaultValue == null
-         ? that.defaultValue == null
-         : (defaultValue.equals(that.defaultValue))) &&
-        (order.equals(that.order)) &&
+        defaultValueEquals(that.defaultValue) &&
         props.equals(that.props);
     }
     public int hashCode() { return name.hashCode() + schema.computeHash(); }
     
+    private boolean defaultValueEquals(JsonNode thatDefaultValue) {
+      if (defaultValue == null)
+        return thatDefaultValue == null;
+      if (Double.isNaN(defaultValue.getValueAsDouble()))
+        return Double.isNaN(thatDefaultValue.getValueAsDouble());
+      return defaultValue.equals(thatDefaultValue);
+    }
+
     @Override
     public String toString() {
       return name + " type:" + schema.type + " pos:" + position;
@@ -1137,8 +1143,15 @@ public abstract class Schema {
           JsonNode orderNode = field.get("order");
           if (orderNode != null)
             order = Field.Order.valueOf(orderNode.getTextValue().toUpperCase());
+          JsonNode defaultValue = field.get("default");
+          if (defaultValue != null
+              && (Type.FLOAT.equals(fieldSchema.getType())
+                  || Type.DOUBLE.equals(fieldSchema.getType()))
+              && defaultValue.isTextual())
+            defaultValue =
+              new DoubleNode(Double.valueOf(defaultValue.getTextValue()));
           Field f = new Field(fieldName, fieldSchema,
-                              fieldDoc, field.get("default"), order);
+                              fieldDoc, defaultValue, order);
           Iterator<String> i = field.getFieldNames();
           while (i.hasNext()) {                       // add field props
             String prop = i.next();

Modified: avro/trunk/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj (original)
+++ avro/trunk/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj Fri Dec  9 20:27:29 2011
@@ -228,7 +228,8 @@ TOKEN :
 |
   < FLOATING_POINT_LITERAL:
   ("-")?
-    ( <DECIMAL_FLOATING_POINT_LITERAL> | <HEXADECIMAL_FLOATING_POINT_LITERAL> )
+    ( "NaN" | "Infinity" |
+      <DECIMAL_FLOATING_POINT_LITERAL> | <HEXADECIMAL_FLOATING_POINT_LITERAL> )
   >
 |
   < #DECIMAL_FLOATING_POINT_LITERAL:

Modified: avro/trunk/lang/java/compiler/src/test/idl/input/simple.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/idl/input/simple.avdl?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/test/idl/input/simple.avdl (original)
+++ avro/trunk/lang/java/compiler/src/test/idl/input/simple.avdl Fri Dec  9 20:27:29 2011
@@ -42,6 +42,9 @@ protocol Simple {
     @foo("bar") MD5 hash;
 
     union { MD5, null} @aliases(["hash", "hsh"]) nullableHash;
+
+    double value = NaN;
+    float average = -Infinity;
   }
 
   error TestError {

Modified: avro/trunk/lang/java/compiler/src/test/idl/output/simple.avpr
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/idl/output/simple.avpr?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/test/idl/output/simple.avpr (original)
+++ avro/trunk/lang/java/compiler/src/test/idl/output/simple.avpr Fri Dec  9 20:27:29 2011
@@ -35,6 +35,14 @@
       "name" : "nullableHash",
       "type" : [ "MD5", "null" ],
       "aliases" : [ "hash", "hsh" ]
+    }, {
+      "name" : "value",
+      "type" : "double",
+      "default" : "NaN"
+    }, {
+      "name" : "average",
+      "type" : "float",
+      "default" : "-Infinity"
     } ]
   }, {
     "type" : "error",

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java?rev=1212611&r1=1212610&r2=1212611&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java Fri Dec  9 20:27:29 2011
@@ -135,6 +135,9 @@ public class TestSchema {
     assertEquals(Schema.create(Type.FLOAT),
                  Schema.parse("{\"type\":\"float\"}"));
     check("\"float\"", "1.1", new Float(1.1));
+    checkDefault("\"float\"", "\"NaN\"", Float.NaN);
+    checkDefault("\"float\"", "\"Infinity\"", Float.POSITIVE_INFINITY);
+    checkDefault("\"float\"", "\"-Infinity\"", Float.NEGATIVE_INFINITY);
   }
 
   @Test
@@ -143,6 +146,9 @@ public class TestSchema {
     assertEquals(Schema.create(Type.DOUBLE),
                  Schema.parse("{\"type\":\"double\"}"));
     check("\"double\"", "1.2", new Double(1.2));
+    checkDefault("\"double\"", "\"NaN\"", Double.NaN);
+    checkDefault("\"double\"", "\"Infinity\"", Double.POSITIVE_INFINITY);
+    checkDefault("\"double\"", "\"-Infinity\"", Double.NEGATIVE_INFINITY);
   }
 
   @Test