Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: finish BIGNUMERIC support #1449

Merged
merged 2 commits into from Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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)
Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -205,7 +205,7 @@ private static void fillField(
if (val instanceof String) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal((String) val)));
return;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'}"}));
Expand Down