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

feat(shortsAndBigDecimals): add shorts and big decimals #1674

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Integer || val instanceof Long) {
} else if (val instanceof Short || val instanceof Integer || val instanceof Long) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
Expand All @@ -320,6 +320,11 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(String.valueOf(val))));
return;
} else if (val instanceof BigDecimal) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString((BigDecimal) val));
return;
}
} else if (fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
if (val instanceof String) {
Expand All @@ -328,7 +333,7 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Integer || val instanceof Long) {
} else if (val instanceof Short || val instanceof Integer || val instanceof Long) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
Expand All @@ -340,6 +345,11 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(String.valueOf(val))));
return;
} else if (val instanceof BigDecimal) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString((BigDecimal) val));
return;
}
}
}
Expand Down Expand Up @@ -542,7 +552,7 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Integer || val instanceof Long) {
} else if (val instanceof Short || val instanceof Integer || val instanceof Long) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
Expand All @@ -554,6 +564,11 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(String.valueOf(val))));
added = true;
} else if (val instanceof BigDecimal) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString((BigDecimal) val));
added = true;
}
} else if (fieldSchema != null
&& fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
Expand All @@ -563,7 +578,7 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Integer || val instanceof Long) {
} else if (val instanceof Short || val instanceof Integer || val instanceof Long) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
Expand All @@ -575,6 +590,11 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(String.valueOf(val))));
added = true;
} else if (val instanceof BigDecimal) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString((BigDecimal) val));
added = true;
}
}
if (!added) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
final TableFieldSchema TEST_NUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_short")
.build();
final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
Expand Down Expand Up @@ -269,6 +275,12 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
final TableFieldSchema TEST_BIGNUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_short")
.build();
final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
Expand Down Expand Up @@ -324,18 +336,20 @@ public void testStructComplex() throws Exception {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_NUMERIC_FLOAT)
.addFields(20, TEST_NUMERIC_DOUBLE)
.addFields(21, TEST_BIGNUMERIC)
.addFields(22, TEST_BIGNUMERIC_STR)
.addFields(23, TEST_BIGNUMERIC_INT)
.addFields(24, TEST_BIGNUMERIC_LONG)
.addFields(25, TEST_BIGNUMERIC_FLOAT)
.addFields(26, TEST_BIGNUMERIC_DOUBLE)
.addFields(27, TEST_INTERVAL)
.addFields(28, TEST_JSON)
.addFields(17, TEST_NUMERIC_SHORT)
.addFields(18, TEST_NUMERIC_INT)
.addFields(19, TEST_NUMERIC_LONG)
.addFields(20, TEST_NUMERIC_FLOAT)
.addFields(21, TEST_NUMERIC_DOUBLE)
.addFields(22, TEST_BIGNUMERIC)
.addFields(23, TEST_BIGNUMERIC_STR)
.addFields(24, TEST_BIGNUMERIC_SHORT)
.addFields(25, TEST_BIGNUMERIC_INT)
.addFields(26, TEST_BIGNUMERIC_LONG)
.addFields(27, TEST_BIGNUMERIC_FLOAT)
.addFields(28, TEST_BIGNUMERIC_DOUBLE)
.addFields(29, TEST_INTERVAL)
.addFields(30, TEST_JSON)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ public class JsonToProtoMessageTest {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
private final TableFieldSchema TEST_NUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_short")
.build();
private final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
Expand Down Expand Up @@ -446,6 +452,12 @@ public class JsonToProtoMessageTest {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_short")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
Expand Down Expand Up @@ -501,18 +513,20 @@ public class JsonToProtoMessageTest {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_NUMERIC_FLOAT)
.addFields(20, TEST_NUMERIC_DOUBLE)
.addFields(21, TEST_BIGNUMERIC)
.addFields(22, TEST_BIGNUMERIC_STR)
.addFields(23, TEST_BIGNUMERIC_INT)
.addFields(24, TEST_BIGNUMERIC_LONG)
.addFields(25, TEST_BIGNUMERIC_FLOAT)
.addFields(26, TEST_BIGNUMERIC_DOUBLE)
.addFields(27, TEST_INTERVAL)
.addFields(28, TEST_JSON)
.addFields(17, TEST_NUMERIC_SHORT)
.addFields(18, TEST_NUMERIC_INT)
.addFields(19, TEST_NUMERIC_LONG)
.addFields(20, TEST_NUMERIC_FLOAT)
.addFields(21, TEST_NUMERIC_DOUBLE)
.addFields(22, TEST_BIGNUMERIC)
.addFields(23, TEST_BIGNUMERIC_STR)
.addFields(24, TEST_BIGNUMERIC_SHORT)
.addFields(25, TEST_BIGNUMERIC_INT)
.addFields(26, TEST_BIGNUMERIC_LONG)
.addFields(27, TEST_BIGNUMERIC_FLOAT)
.addFields(28, TEST_BIGNUMERIC_DOUBLE)
.addFields(29, TEST_INTERVAL)
.addFields(30, TEST_JSON)
.build();

@Test
Expand Down Expand Up @@ -903,27 +917,31 @@ public void testStructComplex() throws Exception {
new BigDecimal("-99999999999999999999999999999.999999999")))
.setTestNumericStr(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("12.4")))
.setTestNumericInt(
.setTestNumericShort(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1)))
.setTestNumericInt(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(2)))
.setTestNumericLong(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1L)))
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(3L)))
.setTestNumericFloat(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1f)))
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(4f)))
.setTestNumericDouble(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1D)))
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(5D)))
.setTestBignumeric(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal("578960446186580977117854925043439539266.3")))
.addTestBignumericStr(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("1.23")))
.setTestBignumericInt(
.setTestBignumericShort(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1)))
.setTestBignumericInt(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(2)))
.setTestBignumericLong(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1L)))
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(3L)))
.setTestBignumericFloat(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1f)))
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(4f)))
.setTestBignumericDouble(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1D)))
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(5D)))
.setTestInterval("0-0 0 0:0:0.000005")
.addTestJson("{'a':'b'}")
.build();
Expand Down Expand Up @@ -968,19 +986,21 @@ public void testStructComplex() throws Exception {
json.put("test_time", CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(1, 0, 1)));
json.put("test_time_str", "20:51:10.1234");
json.put("test_numeric_str", "12.4");
json.put("test_numeric_int", 1);
json.put("test_numeric_long", 1L);
json.put("test_numeric_float", 1f);
json.put("test_numeric_double", 1D);
json.put("test_numeric_short", 1);
json.put("test_numeric_int", 2);
json.put("test_numeric_long", 3L);
json.put("test_numeric_float", 4f);
json.put("test_numeric_double", 5D);
json.put(
"test_bignumeric",
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal("578960446186580977117854925043439539266.3")));
json.put("test_bignumeric_str", new JSONArray(new String[] {"1.23"}));
json.put("test_bignumeric_int", 1);
json.put("test_bignumeric_long", 1L);
json.put("test_bignumeric_float", 1f);
json.put("test_bignumeric_double", 1D);
json.put("test_bignumeric_short", 1);
json.put("test_bignumeric_int", 2);
json.put("test_bignumeric_long", 3L);
json.put("test_bignumeric_float", 4f);
json.put("test_bignumeric_double", 5D);
json.put("test_interval", "0-0 0 0:0:0.000005");
json.put("test_json", new JSONArray(new String[] {"{'a':'b'}"}));
DynamicMessage protoMsg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
final TableFieldSchema TEST_NUMERIC_SHORT =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since only the v1 client is updated, v1beta2 tests are not needed. Can you please remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, I wanted to maintain backwards compat, but if thats not an issue I will change

TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_short")
.build();
final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
Expand Down Expand Up @@ -269,6 +275,13 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();

final TableFieldSchema TEST_BIGNUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_short")
.build();
final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
Expand Down Expand Up @@ -324,18 +337,20 @@ public void testStructComplex() throws Exception {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_NUMERIC_FLOAT)
.addFields(20, TEST_NUMERIC_DOUBLE)
.addFields(21, TEST_BIGNUMERIC)
.addFields(22, TEST_BIGNUMERIC_STR)
.addFields(23, TEST_BIGNUMERIC_INT)
.addFields(24, TEST_BIGNUMERIC_FLOAT)
.addFields(25, TEST_BIGNUMERIC_DOUBLE)
.addFields(26, TEST_BIGNUMERIC_LONG)
.addFields(27, TEST_INTERVAL)
.addFields(28, TEST_JSON)
.addFields(17, TEST_NUMERIC_SHORT)
.addFields(18, TEST_NUMERIC_INT)
.addFields(19, TEST_NUMERIC_LONG)
.addFields(20, TEST_NUMERIC_FLOAT)
.addFields(21, TEST_NUMERIC_DOUBLE)
.addFields(22, TEST_BIGNUMERIC)
.addFields(23, TEST_BIGNUMERIC_STR)
.addFields(24, TEST_BIGNUMERIC_SHORT)
.addFields(25, TEST_BIGNUMERIC_INT)
.addFields(26, TEST_BIGNUMERIC_FLOAT)
.addFields(27, TEST_BIGNUMERIC_DOUBLE)
.addFields(28, TEST_BIGNUMERIC_LONG)
.addFields(29, TEST_INTERVAL)
.addFields(30, TEST_JSON)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
Expand Down
Loading