Avro
  1. Avro
  2. AVRO-1057

Java builder API fails when default value does not match the first type in a union

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.6.3
    • Fix Version/s: None
    • Component/s: java
    • Labels:
      None

      Description

      The following definition works fine with the builder:
      record Rec {
      union

      { boolean, null } field = false;
      }

      but this one fails:
      record Rec {
      union { boolean, null }

      field = null;
      }

      Rec.newBuilder().build() fails with this error:

      org.apache.avro.AvroRuntimeException: org.apache.avro.AvroTypeException: Non-boolean default for boolean: null
      at Rec$Builder.build
      Caused by: org.apache.avro.AvroTypeException: Non-boolean default for boolean: null
      at org.apache.avro.io.parsing.ResolvingGrammarGenerator.encode(ResolvingGrammarGenerator.java:393)
      at org.apache.avro.io.parsing.ResolvingGrammarGenerator.encode(ResolvingGrammarGenerator.java:350)
      at org.apache.avro.data.RecordBuilderBase.defaultValue(RecordBuilderBase.java:178)
      at Rec$Builder.build

        Activity

        Hide
        Doug Cutting added a comment -

        I think this is correct. The specification says, "Default values for union fields correspond to the first schema in the union" (http://avro.apache.org/docs/current/spec.html). So the default value for

        {boolean null}

        must be a boolean.

        Show
        Doug Cutting added a comment - I think this is correct. The specification says, "Default values for union fields correspond to the first schema in the union" ( http://avro.apache.org/docs/current/spec.html ). So the default value for {boolean null} must be a boolean.
        Hide
        Christophe Taton added a comment -

        Ah, you are right, I missed that bit.
        I got confused as the IDL compiler and JSON schema currently allow bogus declarations.
        Could the IDL compiler and the schema parser check this more aggressively?

        Show
        Christophe Taton added a comment - Ah, you are right, I missed that bit. I got confused as the IDL compiler and JSON schema currently allow bogus declarations. Could the IDL compiler and the schema parser check this more aggressively?
        Hide
        Doug Cutting added a comment -

        Yes, this could be checked more aggressively. The existing logic for this is in RecordBuilderBase#defaultValue(). It writes the default JSON parse tree, then reads it into the appropriate Avro data structure (generic, specific, reflect). The writing does the error checking, and that's in ResolvingGrammarGenerator#encode. So the compiler and/or parser could call that. We probably don't want to check it unconditionally in the schema parser, as schema parsing is performance sensitive.

        Show
        Doug Cutting added a comment - Yes, this could be checked more aggressively. The existing logic for this is in RecordBuilderBase#defaultValue(). It writes the default JSON parse tree, then reads it into the appropriate Avro data structure (generic, specific, reflect). The writing does the error checking, and that's in ResolvingGrammarGenerator#encode. So the compiler and/or parser could call that. We probably don't want to check it unconditionally in the schema parser, as schema parsing is performance sensitive.

          People

          • Assignee:
            Unassigned
            Reporter:
            Christophe Taton
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development