From 20ff9e180bf4293ba5ec5afe8326086dcaac7da8 Mon Sep 17 00:00:00 2001 From: JacobStocklass Date: Mon, 29 Mar 2021 23:49:21 +0000 Subject: [PATCH] Changing from BigNumeric to Numeric --- .../v1beta2/BigDecimalByteStringEncoder.java | 18 +++++++------- .../BigDecimalByteStringEncoderTest.java | 24 ++++++++----------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoder.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoder.java index f28fd17f8a..6cdcad95dd 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoder.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoder.java @@ -27,25 +27,23 @@ import java.math.BigInteger; public class BigDecimalByteStringEncoder { - private static int BigDecimalScale = 38; - private static final BigDecimal MAX_BIGNUMERIC_VALUE = - new BigDecimal( - "578960446186580977117854925043439539266.34992332820282019728792003956564819967"); - private static final BigDecimal MIN_BIGNUMERIC_VALUE = - new BigDecimal( - "-578960446186580977117854925043439539266.34992332820282019728792003956564819968"); + private static int NumericScale = 9; + private static final BigDecimal MAX_NUMERIC_VALUE = + new BigDecimal("99999999999999999999999999999.999999999"); + private static final BigDecimal MIN_NUMERIC_VALUE = + new BigDecimal("-99999999999999999999999999999.999999999"); public static ByteString encodeToNumericByteString(BigDecimal bigDecimal) { ByteString byteString = serializeBigDecimal( - bigDecimal, BigDecimalScale, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "ByteString"); + bigDecimal, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "ByteString"); return byteString; } - public static BigDecimal decodeToBigDecimal(ByteString byteString) { + public static BigDecimal decodeNumericByteString(ByteString byteString) { BigDecimal bigDecimal = deserializeBigDecimal( - byteString, BigDecimalScale, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "BigDecimal"); + byteString, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "BigDecimal"); return bigDecimal; } // Make these private and make public wrapper that internalizes these min/max/scale/type diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoderTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoderTest.java index 9eae4eb06f..f73a0e1549 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoderTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1beta2/BigDecimalByteStringEncoderTest.java @@ -27,39 +27,35 @@ public class BigDecimalByteStringEncoderTest { @Test public void testEncodeBigDecimalandEncodeByteString() { - BigDecimal testBD = BigDecimal.valueOf(0); // expected result bd + BigDecimal testBD = new BigDecimal("0"); // expected result bd ByteString testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); // convert expected to bs BigDecimal resultBD = - BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); // convert bs to bd + BigDecimalByteStringEncoder.decodeNumericByteString(testBS); // convert bs to bd Assert.assertEquals( 0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd - testBD = BigDecimal.valueOf(1.2); + testBD = new BigDecimal("1.2"); testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); - resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); + resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS); Assert.assertEquals( 0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd - testBD = BigDecimal.valueOf(-1.2); + testBD = new BigDecimal("-1.2"); testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); - resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); + resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS); Assert.assertEquals( 0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd - testBD = - BigDecimal.valueOf( - 578960446186580977117854925043439539266.34992332820282019728792003956564819967); + testBD = new BigDecimal("99999999999999999999999999999.999999999"); testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); - resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); + resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS); Assert.assertEquals( 0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd - testBD = - BigDecimal.valueOf( - -578960446186580977117854925043439539266.34992332820282019728792003956564819967); + testBD = new BigDecimal("-99999999999999999999999999999.999999999"); testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); - resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); + resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS); Assert.assertEquals( 0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd }