diff --git a/README.md b/README.md index 12792a10b6..8fd23d2c2d 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies ```Groovy -implementation platform('com.google.cloud:libraries-bom:24.1.0') +implementation platform('com.google.cloud:libraries-bom:24.1.1') implementation 'com.google.cloud:google-cloud-bigquerystorage' ``` diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptor.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptor.java index 6c2a3dfebc..7073cfbe64 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptor.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptor.java @@ -52,6 +52,7 @@ public class BQTableSchemaToProtoDescriptor { .put(TableFieldSchema.Type.GEOGRAPHY, FieldDescriptorProto.Type.TYPE_STRING) .put(TableFieldSchema.Type.INT64, FieldDescriptorProto.Type.TYPE_INT64) .put(TableFieldSchema.Type.NUMERIC, FieldDescriptorProto.Type.TYPE_BYTES) + .put(TableFieldSchema.Type.BIGNUMERIC, FieldDescriptorProto.Type.TYPE_BYTES) .put(TableFieldSchema.Type.STRING, FieldDescriptorProto.Type.TYPE_STRING) .put(TableFieldSchema.Type.STRUCT, FieldDescriptorProto.Type.TYPE_MESSAGE) .put(TableFieldSchema.Type.TIME, FieldDescriptorProto.Type.TYPE_INT64) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BigDecimalByteStringEncoder.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BigDecimalByteStringEncoder.java index b5c5992437..c652f57ad2 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BigDecimalByteStringEncoder.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/BigDecimalByteStringEncoder.java @@ -27,25 +27,50 @@ import java.math.BigInteger; public class BigDecimalByteStringEncoder { - private static int NumericScale = 9; + private static int NUMERIC_SCALE = 9; private static final BigDecimal MAX_NUMERIC_VALUE = new BigDecimal("99999999999999999999999999999.999999999"); private static final BigDecimal MIN_NUMERIC_VALUE = new BigDecimal("-99999999999999999999999999999.999999999"); + // Number of digits after the decimal point supported by the BIGNUMERIC data type. + private static final int BIGNUMERIC_SCALE = 38; + // Maximum and minimum allowed values for the BIGNUMERIC data type. + private static final BigDecimal MAX_BIGNUMERIC_VALUE = + new BigDecimal( + "578960446186580977117854925043439539266.34992332820282019728792003956564819967"); + private static final BigDecimal MIN_BIGNUMERIC_VALUE = + new BigDecimal( + "-578960446186580977117854925043439539266.34992332820282019728792003956564819968"); + public static ByteString encodeToNumericByteString(BigDecimal bigDecimal) { ByteString byteString = serializeBigDecimal( - bigDecimal, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "ByteString"); + bigDecimal, NUMERIC_SCALE, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "ByteString"); + return byteString; + } + + public static ByteString encodeToBigNumericByteString(BigDecimal bigDecimal) { + ByteString byteString = + serializeBigDecimal( + bigDecimal, BIGNUMERIC_SCALE, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "ByteString"); return byteString; } public static BigDecimal decodeNumericByteString(ByteString byteString) { BigDecimal bigDecimal = deserializeBigDecimal( - byteString, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "BigDecimal"); + byteString, NUMERIC_SCALE, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "BigDecimal"); return bigDecimal; } + + public static BigDecimal decodeBigNumericByteString(ByteString byteString) { + BigDecimal bigDecimal = + deserializeBigDecimal( + byteString, BIGNUMERIC_SCALE, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "BigDecimal"); + return bigDecimal; + } + // Make these private and make public wrapper that internalizes these min/max/scale/type private static BigDecimal deserializeBigDecimal( ByteString serializedValue, diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java index 6423fe43f7..88b32d5a3c 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java @@ -205,7 +205,7 @@ private static void fillField( if (val instanceof String) { protoMsg.setField( fieldDescriptor, - BigDecimalByteStringEncoder.encodeToNumericByteString( + BigDecimalByteStringEncoder.encodeToBigNumericByteString( new BigDecimal((String) val))); return; } @@ -373,7 +373,7 @@ private static void fillRepeatedField( if (val instanceof String) { protoMsg.addRepeatedField( fieldDescriptor, - BigDecimalByteStringEncoder.encodeToNumericByteString( + BigDecimalByteStringEncoder.encodeToBigNumericByteString( new BigDecimal((String) val))); added = true; } diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptorTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptorTest.java index 35b9944735..40c3fc3022 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptorTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/BQTableSchemaToProtoDescriptorTest.java @@ -235,13 +235,13 @@ public void testStructComplex() throws Exception { .build(); final TableFieldSchema TEST_BIGNUMERIC = TableFieldSchema.newBuilder() - .setType(TableFieldSchema.Type.NUMERIC) + .setType(TableFieldSchema.Type.BIGNUMERIC) .setMode(TableFieldSchema.Mode.NULLABLE) .setName("test_bignumeric") .build(); final TableFieldSchema TEST_BIGNUMERIC_STR = TableFieldSchema.newBuilder() - .setType(TableFieldSchema.Type.NUMERIC) + .setType(TableFieldSchema.Type.BIGNUMERIC) .setMode(TableFieldSchema.Mode.REPEATED) .setName("test_bignumeric_str") .build(); diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java index 96b46ed8d0..14c2d3763b 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java @@ -411,13 +411,13 @@ public class JsonToProtoMessageTest { .build(); private final TableFieldSchema TEST_BIGNUMERIC = TableFieldSchema.newBuilder() - .setType(TableFieldSchema.Type.NUMERIC) + .setType(TableFieldSchema.Type.BIGNUMERIC) .setMode(TableFieldSchema.Mode.NULLABLE) .setName("test_bignumeric") .build(); private final TableFieldSchema TEST_BIGNUMERIC_STR = TableFieldSchema.newBuilder() - .setType(TableFieldSchema.Type.NUMERIC) + .setType(TableFieldSchema.Type.BIGNUMERIC) .setMode(TableFieldSchema.Mode.REPEATED) .setName("test_bignumeric_str") .build(); @@ -831,9 +831,10 @@ public void testStructComplex() throws Exception { .setTestNumericStr( BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("12.4"))) .setTestBignumeric( - BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("2.3"))) + BigDecimalByteStringEncoder.encodeToBigNumericByteString( + new BigDecimal("578960446186580977117854925043439539266.3"))) .addTestBignumericStr( - BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.23"))) + BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("1.23"))) .setTestInterval("0-0 0 0:0:0.000005") .addTestJson("{'a':'b'}") .build(); @@ -880,7 +881,8 @@ public void testStructComplex() throws Exception { json.put("test_numeric_str", "12.4"); json.put( "test_bignumeric", - BigDecimalByteStringEncoder.encodeToNumericByteString(BigDecimal.valueOf(2.3))); + BigDecimalByteStringEncoder.encodeToBigNumericByteString( + new BigDecimal("578960446186580977117854925043439539266.3"))); json.put("test_bignumeric_str", new JSONArray(new String[] {"1.23"})); json.put("test_interval", "0-0 0 0:0:0.000005"); json.put("test_json", new JSONArray(new String[] {"{'a':'b'}"}));