Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5257C200D0E for ; Tue, 26 Sep 2017 17:45:06 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 50A9F1609C1; Tue, 26 Sep 2017 15:45:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9369A1609B4 for ; Tue, 26 Sep 2017 17:45:05 +0200 (CEST) Received: (qmail 49588 invoked by uid 500); 26 Sep 2017 15:45:04 -0000 Mailing-List: contact dev-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list dev@avro.apache.org Received: (qmail 49577 invoked by uid 99); 26 Sep 2017 15:45:04 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Sep 2017 15:45:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 2C2DA1A1963 for ; Tue, 26 Sep 2017 15:45:04 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.202 X-Spam-Level: X-Spam-Status: No, score=-99.202 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id xDOyRKbZZ-xe for ; Tue, 26 Sep 2017 15:45:02 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id 3FCFF6106C for ; Tue, 26 Sep 2017 15:45:02 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 32936E0EFE for ; Tue, 26 Sep 2017 15:45:01 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id A8D1224266 for ; Tue, 26 Sep 2017 15:45:00 +0000 (UTC) Date: Tue, 26 Sep 2017 15:45:00 +0000 (UTC) From: "Andy Coates (JIRA)" To: dev@avro.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (AVRO-2078) Avro does not enforce schema resolution rules for Decimal type MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Tue, 26 Sep 2017 15:45:06 -0000 [ https://issues.apache.org/jira/browse/AVRO-2078?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16180950#comment-16180950 ] Andy Coates edited comment on AVRO-2078 at 9/26/17 3:44 PM: ------------------------------------------------------------ This is particularly nasty bug as it can easily lead to data corruption. If you write decimal "1.2345" with a write schema with a scale of 4 and then deserialize with a scale of 3, the value comes out as "12.345"!!!! {code:java} @Test public void shouldThrowIfExistingFieldChangesType() throws Exception { GenericData genericData = new GenericData(); genericData.addLogicalTypeConversion(new Conversions.DecimalConversion()); final Schema v1 = Schema.createRecord("thing", "", "namespace", false, ImmutableList.of( new Schema.Field("decimal", LogicalTypes.decimal(3, 3).addToSchema(Schema.create(Schema.Type.BYTES)), "", Schema.NULL_VALUE) )); final Schema v2 = Schema.createRecord("thing", "", "namespace", false, ImmutableList.of( new Schema.Field("decimal", LogicalTypes.decimal(6, 4).addToSchema(Schema.create(Schema.Type.BYTES)), "", Schema.NULL_VALUE) )); final GenericData.Record recordV2 = new GenericData.Record(v2); recordV2.put("decimal", new BigDecimal("1.2345")); ByteBuffer bytes = serialize(genericData, recordV2); final GenericRecord deserialized = deserialize(genericData, v1, v2, bytes); final Object result = deserialized.get("decimal"); // Below fails because result is 'new BigDecimal("12.345")' assertThat(result, is (new BigDecimal("1.2345"))); } private ByteBuffer serialize(final GenericData genericData, final GenericData.Record recordV2) throws java.io.IOException { ByteBufferOutputStream output = new ByteBufferOutputStream(); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(output, null); DatumWriter datumWriter = genericData.createDatumWriter(recordV2.getSchema()); datumWriter.write(recordV2, encoder); encoder.flush(); return output.getBufferList().get(0); } private GenericRecord deserialize(final GenericData genericData, final Schema v1, final Schema v2, final ByteBuffer bytes) throws java.io.IOException { ByteBufferInputStream input = new ByteBufferInputStream(bytes); final DatumReader datumReader = genericData.createDatumReader(v2, v1); return datumReader.read(new GenericData.Record(v1), DecoderFactory.get().binaryDecoder(input, null)); } {code} was (Author: bigandy): This is particularly nasty bug as it can easily lead to data corruption. If you write decimal "1.2345" with a write schema with a scale of 4 and then deserialize with a scale of 3, the value comes out as "12.345"!!!! > Avro does not enforce schema resolution rules for Decimal type > -------------------------------------------------------------- > > Key: AVRO-2078 > URL: https://issues.apache.org/jira/browse/AVRO-2078 > Project: Avro > Issue Type: Bug > Reporter: Anthony Hsu > Assignee: Nandor Kollar > Fix For: 1.8.2 > > Attachments: dec.avro > > > According to http://avro.apache.org/docs/1.8.2/spec.html#Decimal > bq. 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: > {noformat:title=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 datumReader = new GenericDatumReader<>(schema); > // dec.avro has precision 5, scale 2 > DataFileReader 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 > } > } > } > {noformat} -- This message was sent by Atlassian JIRA (v6.4.14#64029)