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: Add short, integer, long, boolean conversions into string #2437

Merged
merged 3 commits into from
Mar 15, 2024
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.
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 @@ -650,6 +650,9 @@ private void fillField(
if (val instanceof String) {
protoMsg.setField(fieldDescriptor, val);
return;
} else if (val instanceof Short || val instanceof Integer || val instanceof Long || val instanceof Boolean) {
protoMsg.setField(fieldDescriptor, String.valueOf(val));
return;
}
break;
case DOUBLE:
Expand Down Expand Up @@ -910,6 +913,9 @@ private void fillRepeatedField(
case STRING:
if (val instanceof String) {
protoMsg.addRepeatedField(fieldDescriptor, val);
} else if (val instanceof Short || val instanceof Integer || val instanceof Long || val instanceof Boolean) {
protoMsg.addRepeatedField(fieldDescriptor, String.valueOf(val));
return;
} else {
throwWrongFieldType(fieldDescriptor, currentScope, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.cloud.bigquery.storage.v1.ConnectionWorkerPool.Settings;
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializationError;
import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Int64Value;
Expand Down Expand Up @@ -1406,8 +1407,8 @@ public void testMultipleAppendSerializationErrors()
// put a vaild value into the field
foo1.put("foo", "allen");
JSONObject foo2 = new JSONObject();
// put a number into a string field
foo2.put("foo", 666);
// put a field which is not part of the expected schema
foo2.put("not_bar", "woody");
JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);
jsonArr.put(foo1);
Expand All @@ -1425,14 +1426,11 @@ public void testMultipleAppendSerializationErrors()
} catch (AppendSerializationError appendSerializationError) {
Map<Integer, String> rowIndexToErrorMessage =
appendSerializationError.getRowIndexToErrorMessage();
assertEquals(2, rowIndexToErrorMessage.size());
assertEquals(
"The source object has fields unknown to BigQuery: root.not_foo.",
rowIndexToErrorMessage.get(0));
assertEquals(
"Field root.foo failed to convert to STRING. Error: JSONObject does not have a string"
+ " field at root.foo.",
rowIndexToErrorMessage.get(2));
ImmutableMap.of(
0, "The source object has fields unknown to BigQuery: root.not_foo.",
2, "The source object has fields unknown to BigQuery: root.not_bar."
), rowIndexToErrorMessage);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public class JsonToProtoMessageTest {
})
.put(
StringType.getDescriptor(),
new Message[] {StringType.newBuilder().setTestFieldType("test").build()})
new Message[] {
StringType.newBuilder().setTestFieldType("9223372036854775807").build(),
StringType.newBuilder().setTestFieldType("2147483647").build(),
StringType.newBuilder().setTestFieldType("true").build(),
StringType.newBuilder().setTestFieldType("test").build()
})
.put(
RepeatedType.getDescriptor(),
new Message[] {
Expand Down Expand Up @@ -147,6 +152,9 @@ public class JsonToProtoMessageTest {
.put(
RepeatedString.getDescriptor(),
new Message[] {
RepeatedString.newBuilder().addTestRepeated("9223372036854775807").build(),
RepeatedString.newBuilder().addTestRepeated("2147483647").build(),
RepeatedString.newBuilder().addTestRepeated("true").build(),
RepeatedString.newBuilder().addTestRepeated("hello").addTestRepeated("test").build()
})
.put(
Expand Down Expand Up @@ -925,6 +933,8 @@ public void testAllTypes() throws Exception {
} else if (entry.getKey() == Int64Type.getDescriptor()
|| entry.getKey() == BytesType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else if(entry.getKey() == StringType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 4, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
Expand Down Expand Up @@ -962,6 +972,8 @@ public void testAllRepeatedTypesWithLimits() throws Exception {
assertEquals(entry.getKey().getFullName(), 4, success);
} else if (entry.getKey() == RepeatedInt64.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else if (entry.getKey() == RepeatedString.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 4, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
Expand Down Expand Up @@ -1007,16 +1019,22 @@ public void testRequired() throws Exception {
}
}

@Test
public void testStructSimple() throws Exception {
@Test
public void testStructSimple() throws Exception {
structSimple("test", "test");
structSimple(true, "true");
structSimple(1, "1");
structSimple((short) 1, "1");
structSimple((long) 1, "1");
}

private void structSimple(Object value, String expected) throws Exception {
MessageType expectedProto =
MessageType.newBuilder()
.setTestFieldType(StringType.newBuilder().setTestFieldType("test").build())
.setTestFieldType(StringType.newBuilder().setTestFieldType(expected).build())
.build();
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", "test");
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
JSONObject stringType = new JSONObject(ImmutableMap.of("test_field_type", value));
JSONObject json = new JSONObject(ImmutableMap.of("test_field_type", stringType));

DynamicMessage protoMsg =
JsonToProtoMessage.INSTANCE.convertToProtoMessage(MessageType.getDescriptor(), json);
Expand All @@ -1026,7 +1044,7 @@ public void testStructSimple() throws Exception {
@Test
public void testStructSimpleFail() throws Exception {
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", 1);
stringType.put("test_field_type", new boolean[0]);
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
try {
Expand Down Expand Up @@ -1268,7 +1286,7 @@ public void testNestedRepeatedComplex() throws Exception {
@Test
public void testNestedRepeatedComplexFail() throws Exception {
double[] doubleArr = {1.1, 2.2, 3.3, 4.4, 5.5};
Boolean[] fakeStringArr = {true, false};
Boolean[][] fakeStringArr = {new Boolean[0], new Boolean[0]};
int[] intArr = {1, 2, 3, 4, 5};

JSONObject json = new JSONObject();
Expand Down
Loading