avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ieme...@apache.org
Subject [avro] branch branch-1.9 updated: AVRO-2592: Avoid consuming ByteBuffer for decimal.
Date Fri, 31 Jan 2020 09:43:19 GMT
This is an automated email from the ASF dual-hosted git repository.

iemejia pushed a commit to branch branch-1.9
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.9 by this push:
     new 6ee4b6b  AVRO-2592: Avoid consuming ByteBuffer for decimal.
6ee4b6b is described below

commit 6ee4b6b90271ccc78ce3b51c422bcbdb9abab995
Author: Ryan Skraba <ryan@skraba.com>
AuthorDate: Fri Nov 29 13:23:20 2019 +0100

    AVRO-2592: Avoid consuming ByteBuffer for decimal.
---
 .../src/main/java/org/apache/avro/Conversions.java |  6 ++--
 .../avro/generic/TestGenericLogicalTypes.java      | 41 ++++++++++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/Conversions.java b/lang/java/avro/src/main/java/org/apache/avro/Conversions.java
index a54d558..d995fce 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Conversions.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Conversions.java
@@ -80,7 +80,7 @@ public class Conversions {
       int scale = ((LogicalTypes.Decimal) type).getScale();
       // always copy the bytes out because BigInteger has no offset/length ctor
       byte[] bytes = new byte[value.remaining()];
-      value.get(bytes);
+      value.duplicate().get(bytes);
       return new BigDecimal(new BigInteger(bytes), scale);
     }
 
@@ -122,7 +122,7 @@ public class Conversions {
   /**
    * Convert a underlying representation of a logical type (such as a ByteBuffer)
    * to a higher level object (such as a BigDecimal).
-   * 
+   *
    * @param datum      The object to be converted.
    * @param schema     The schema of datum. Cannot be null if datum is not null.
    * @param type       The {@link org.apache.avro.LogicalType} of datum. Cannot be
@@ -181,7 +181,7 @@ public class Conversions {
   /**
    * Convert a high level representation of a logical type (such as a BigDecimal)
    * to the its underlying representation object (such as a ByteBuffer)
-   * 
+   *
    * @param datum      The object to be converted.
    * @param schema     The schema of datum. Cannot be null if datum is not null.
    * @param type       The {@link org.apache.avro.LogicalType} of datum. Cannot be
diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
index 0603bfe..17c00af 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
@@ -36,6 +36,9 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
 public class TestGenericLogicalTypes {
 
   @Rule
@@ -139,6 +142,44 @@ public class TestGenericLogicalTypes {
   }
 
   @Test
+  public void testDecimalToFromBytes() throws IOException {
+    LogicalType decimal = LogicalTypes.decimal(9, 2);
+    Schema bytesSchema = Schema.create(Schema.Type.BYTES);
+
+    // Check that the round trip to and from bytes
+    BigDecimal d1 = new BigDecimal("-34.34");
+    BigDecimal d2 = new BigDecimal("117230.00");
+
+    Conversion<BigDecimal> conversion = new Conversions.DecimalConversion();
+
+    ByteBuffer d1bytes = conversion.toBytes(d1, bytesSchema, decimal);
+    ByteBuffer d2bytes = conversion.toBytes(d2, bytesSchema, decimal);
+
+    assertThat(conversion.fromBytes(d1bytes, bytesSchema, decimal), is(d1));
+    assertThat(conversion.fromBytes(d2bytes, bytesSchema, decimal), is(d2));
+
+    assertThat("Ensure ByteBuffer not consumed by conversion", conversion.fromBytes(d1bytes,
bytesSchema, decimal),
+        is(d1));
+  }
+
+  @Test
+  public void testDecimalToFromFixed() throws IOException {
+    LogicalType decimal = LogicalTypes.decimal(9, 2);
+    Schema fixedSchema = Schema.createFixed("aFixed", null, null, 4);
+
+    // Check that the round trip to and from fixed data.
+    BigDecimal d1 = new BigDecimal("-34.34");
+    BigDecimal d2 = new BigDecimal("117230.00");
+
+    Conversion<BigDecimal> conversion = new Conversions.DecimalConversion();
+
+    GenericFixed d1fixed = conversion.toFixed(d1, fixedSchema, decimal);
+    GenericFixed d2fixed = conversion.toFixed(d2, fixedSchema, decimal);
+    assertThat(conversion.fromFixed(d1fixed, fixedSchema, decimal), is(d1));
+    assertThat(conversion.fromFixed(d2fixed, fixedSchema, decimal), is(d2));
+  }
+
+  @Test
   public void testReadDecimalBytes() throws IOException {
     LogicalType decimal = LogicalTypes.decimal(9, 2);
     Schema bytesSchema = Schema.create(Schema.Type.BYTES);


Mime
View raw message