Skip to content

Commit

Permalink
fix: ints/longs are numerics (#1596)
Browse files Browse the repository at this point in the history
Should allow users to pass ints and longs as numerics.

Fixes #1516
  • Loading branch information
dark0dave authored May 13, 2022
1 parent 320f5fc commit d046c8d
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Integer || val instanceof Long) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
return;
}
} else if (fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
if (val instanceof String) {
Expand All @@ -298,6 +304,12 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Integer || val instanceof Long) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(((Number) val).longValue())));
return;
}
}
}
Expand Down Expand Up @@ -504,6 +516,12 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Integer || val instanceof Long) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
added = true;
}
} else if (fieldSchema != null
&& fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
Expand All @@ -513,6 +531,12 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Integer || val instanceof Long) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(((Number) val).longValue())));
added = true;
}
}
if (!added) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Long || val instanceof Integer) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
return;
}
} else if (fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
if (val instanceof String) {
Expand All @@ -205,6 +211,12 @@ private static void fillField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
return;
} else if (val instanceof Long || val instanceof Integer) {
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
return;
}
}
}
Expand Down Expand Up @@ -357,6 +369,12 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Long || val instanceof Integer) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
added = true;
}
} else if (fieldSchema != null
&& fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
Expand All @@ -366,6 +384,12 @@ private static void fillRepeatedField(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal((String) val)));
added = true;
} else if (val instanceof Long || val instanceof Integer) {
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).longValue())));
added = true;
}
}
if (!added) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_int")
.build();
final TableFieldSchema TEST_NUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_long")
.build();
final TableFieldSchema TEST_BIGNUMERIC =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
Expand All @@ -245,6 +257,18 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_int")
.build();
final TableFieldSchema TEST_BIGNUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_long")
.build();
final TableFieldSchema TEST_INTERVAL =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.INTERVAL)
Expand Down Expand Up @@ -276,10 +300,14 @@ public void testStructComplex() throws Exception {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_BIGNUMERIC)
.addFields(18, TEST_BIGNUMERIC_STR)
.addFields(19, TEST_INTERVAL)
.addFields(20, TEST_JSON)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_BIGNUMERIC)
.addFields(20, TEST_BIGNUMERIC_STR)
.addFields(21, TEST_BIGNUMERIC_INT)
.addFields(22, TEST_BIGNUMERIC_LONG)
.addFields(23, TEST_INTERVAL)
.addFields(24, TEST_JSON)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,18 @@ public class JsonToProtoMessageTest {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
private final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_int")
.build();
private final TableFieldSchema TEST_NUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_long")
.build();
private final TableFieldSchema TEST_BIGNUMERIC =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
Expand All @@ -421,6 +433,18 @@ public class JsonToProtoMessageTest {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_int")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_long")
.build();
final TableFieldSchema TEST_INTERVAL =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.INTERVAL)
Expand Down Expand Up @@ -452,10 +476,14 @@ public class JsonToProtoMessageTest {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_BIGNUMERIC)
.addFields(18, TEST_BIGNUMERIC_STR)
.addFields(19, TEST_INTERVAL)
.addFields(20, TEST_JSON)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_BIGNUMERIC)
.addFields(20, TEST_BIGNUMERIC_STR)
.addFields(21, TEST_BIGNUMERIC_INT)
.addFields(22, TEST_BIGNUMERIC_LONG)
.addFields(23, TEST_INTERVAL)
.addFields(24, TEST_JSON)
.build();

@Test
Expand Down Expand Up @@ -888,11 +916,19 @@ public void testStructComplex() throws Exception {
new BigDecimal("-99999999999999999999999999999.999999999")))
.setTestNumericStr(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("12.4")))
.setTestNumericInt(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1)))
.setTestNumericLong(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1L)))
.setTestBignumeric(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal("578960446186580977117854925043439539266.3")))
.addTestBignumericStr(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("1.23")))
.setTestBignumericInt(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1)))
.setTestBignumericLong(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1L)))
.setTestInterval("0-0 0 0:0:0.000005")
.addTestJson("{'a':'b'}")
.build();
Expand Down Expand Up @@ -937,11 +973,15 @@ 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_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_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,18 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_int")
.build();
final TableFieldSchema TEST_NUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_long")
.build();
final TableFieldSchema TEST_BIGNUMERIC =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
Expand All @@ -245,6 +257,18 @@ public void testStructComplex() throws Exception {
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_int")
.build();
final TableFieldSchema TEST_BIGNUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_long")
.build();
final TableFieldSchema TEST_INTERVAL =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.INTERVAL)
Expand Down Expand Up @@ -276,10 +300,14 @@ public void testStructComplex() throws Exception {
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_BIGNUMERIC)
.addFields(18, TEST_BIGNUMERIC_STR)
.addFields(19, TEST_INTERVAL)
.addFields(20, TEST_JSON)
.addFields(17, TEST_NUMERIC_INT)
.addFields(18, TEST_NUMERIC_LONG)
.addFields(19, TEST_BIGNUMERIC)
.addFields(20, TEST_BIGNUMERIC_STR)
.addFields(21, TEST_BIGNUMERIC_INT)
.addFields(22, TEST_BIGNUMERIC_LONG)
.addFields(23, TEST_INTERVAL)
.addFields(24, TEST_JSON)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
Expand Down
Loading

0 comments on commit d046c8d

Please sign in to comment.