Avro
  1. Avro
  2. AVRO-972

Avro IDL compiler should support "NaN", "Infinity", and "-Infinity" as float literals

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.6.1
    • Fix Version/s: 1.6.2
    • Component/s: java
    • Labels:
      None

      Description

      The Avro IDL defines floating point literals as follows:
      < FLOATING_POINT_LITERAL:
      ("-")?
      ( <DECIMAL_FLOATING_POINT_LITERAL> | <HEXADECIMAL_FLOATING_POINT_LITERAL> )
      >

      < #DECIMAL_FLOATING_POINT_LITERAL:
      (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?

      "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
      (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
      (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]
      >

      < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >

      < #HEXADECIMAL_FLOATING_POINT_LITERAL:
      "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?

      "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
      >

      < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >

      Java allows for "NaN", "Infinity", and "-Infinity" when parsing a Double from a String:
      http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf(java.lang.String)

      The IDL compiler should allow the same set of values as Java Double.valueOf. Among other things, this would allow "NaN" to be used as a default value for float fields in Avro records.

      1. AVRO-972.patch
        5 kB
        Doug Cutting
      2. AVRO-972.patch
        5 kB
        Doug Cutting
      3. AVRO-972.patch
        4 kB
        Doug Cutting

        Activity

        Brandon Ballinger created issue -
        Hide
        Doug Cutting added a comment -

        Here's a patch that should implement this. Since JSON doesn't support Nan and infinities we encode them as strings there to keep our JSON portable.

        The only odd thing is that a schema that includes a NaN as a default value is not equal to itself, since NaN is not equal to anything. We should perhaps explicitly check for this in Schema.Field#equals().

        Show
        Doug Cutting added a comment - Here's a patch that should implement this. Since JSON doesn't support Nan and infinities we encode them as strings there to keep our JSON portable. The only odd thing is that a schema that includes a NaN as a default value is not equal to itself, since NaN is not equal to anything. We should perhaps explicitly check for this in Schema.Field#equals().
        Doug Cutting made changes -
        Field Original Value New Value
        Attachment AVRO-972.patch [ 12506657 ]
        Hide
        Doug Cutting added a comment -

        Here's a new version that also fixes Schema.Field#equals() so that two fields whose default values are NaN are equal.

        Show
        Doug Cutting added a comment - Here's a new version that also fixes Schema.Field#equals() so that two fields whose default values are NaN are equal.
        Hide
        Doug Cutting added a comment -

        I'll commit this soon unless someone objects.

        Show
        Doug Cutting added a comment - I'll commit this soon unless someone objects.
        Doug Cutting made changes -
        Status Open [ 1 ] Patch Available [ 10002 ]
        Assignee Doug Cutting [ cutting ]
        Fix Version/s 1.6.2 [ 12319074 ]
        Doug Cutting made changes -
        Attachment AVRO-972.patch [ 12506774 ]
        Hide
        Doug Cutting added a comment -

        Add tests for float too.

        Show
        Doug Cutting added a comment - Add tests for float too.
        Doug Cutting made changes -
        Attachment AVRO-972.patch [ 12506776 ]
        Hide
        Brandon Ballinger added a comment -

        Thank you!

        On Fri, Dec 9, 2011 at 11:00 AM, Doug Cutting (Updated) (JIRA) <

        Show
        Brandon Ballinger added a comment - Thank you! On Fri, Dec 9, 2011 at 11:00 AM, Doug Cutting (Updated) (JIRA) <
        Hide
        Doug Cutting added a comment -

        I committed this.

        Show
        Doug Cutting added a comment - I committed this.
        Doug Cutting made changes -
        Status Patch Available [ 10002 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Doug Cutting made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Doug Cutting
            Reporter:
            Brandon Ballinger
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development