Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2078

Avro does not enforce schema resolution rules for Decimal type

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Patch Available
    • Major
    • Resolution: Unresolved
    • 1.8.2
    • None
    • java
    • None

    Description

      According to http://avro.apache.org/docs/1.8.2/spec.html#Decimal

      For the purposes of schema resolution, two schemas that are decimal logical types match if their scales and precisions match.

      This is not enforced.

      I wrote a file with (precision 5, scale 2) and tried to read it with a reader schema with (precision 3, scale 1). I expected an AvroTypeException to be thrown, but none was thrown.

      Test data file attached. The code to read it is:

      ReadDecimal.java
      import java.io.File;
      import org.apache.avro.Schema;
      import org.apache.avro.file.DataFileReader;
      import org.apache.avro.generic.GenericDatumReader;
      import org.apache.avro.generic.GenericRecord;
      import org.apache.avro.io.DatumReader;
      
      
      public class ReadDecimal {
        public static void main(String[] args) throws Exception {
          Schema schema = new Schema.Parser().parse("{\n" + "  \"type\" : \"record\",\n" + "  \"name\" : \"some_schema\",\n"
              + "  \"namespace\" : \"com.howdy\",\n" + "  \"fields\" : [ {\n" + "    \"name\" : \"name\",\n"
              + "    \"type\" : \"string\"\n" + "  }, {\n" + "    \"name\" : \"value\",\n" + "    \"type\" : {\n"
              + "      \"type\" : \"bytes\",\n" + "      \"logicalType\" : \"decimal\",\n" + "      \"precision\" : 3,\n"
              + "      \"scale\" : 1\n" + "    }\n" + "  } ]\n" + "}");
      
          DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema);
      
          // dec.avro has precision 5, scale 2
          DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(
              new File("/tmp/dec.avro"), datumReader);
          GenericRecord foo = null;
          while (dataFileReader.hasNext()) {
            foo = dataFileReader.next(foo);  // AvroTypeException expected due to change in scale/precision but none occurs
          }
        }
      }
      

      Attachments

        1. dec.avro
          0.3 kB
          Anthony Hsu

        Issue Links

          Activity

            People

              nkollar Nándor Kollár
              erwaman Anthony Hsu
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated: