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: support DATETIME field that has a space between date and time and has only date #2176

Merged
merged 2 commits into from
Jun 30, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are using Maven without the 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:26.17.0')
implementation platform('com.google.cloud:libraries-bom:26.18.0')

implementation 'com.google.cloud:google-cloud-bigquerystorage'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ public class JsonToProtoMessage implements ToProtoConverter<Object> {
.toFormatter()
.withZone(ZoneOffset.UTC);

private static final DateTimeFormatter DATETIME_FORMATTER =
new DateTimeFormatterBuilder()
.parseLenient()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.optionalStart()
.optionalStart()
.parseCaseInsensitive()
.appendLiteral('T')
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.optionalEnd()
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.optionalEnd()
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter();

/** You can use {@link #INSTANCE} instead */
public JsonToProtoMessage() {}

Expand Down Expand Up @@ -402,7 +421,8 @@ private void fillField(
if (val instanceof String) {
protoMsg.setField(
fieldDescriptor,
CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.parse((String) val)));
CivilTimeEncoder.encodePacked64DatetimeMicros(
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
return;
} else if (val instanceof Long) {
protoMsg.setField(fieldDescriptor, val);
Expand Down Expand Up @@ -663,7 +683,8 @@ private void fillRepeatedField(
if (val instanceof String) {
protoMsg.addRepeatedField(
fieldDescriptor,
CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.parse((String) val)));
CivilTimeEncoder.encodePacked64DatetimeMicros(
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
} else if (val instanceof Long) {
protoMsg.addRepeatedField(fieldDescriptor, val);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void testDateTimeMismatch() throws Exception {
TableFieldSchema.newBuilder()
.setName("datetime")
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.REPEATED)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
JSONObject json = new JSONObject();
Expand All @@ -623,6 +623,34 @@ public void testDateTimeMismatch() throws Exception {
}
}

private void dateTimeMatch_Internal(String jsonVal, Long expectedVal) throws Exception {
TableFieldSchema field =
TableFieldSchema.newBuilder()
.setName("datetime")
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
TestDatetime expectedProto = TestDatetime.newBuilder().setDatetime(expectedVal).build();
JSONObject json = new JSONObject();
json.put("datetime", jsonVal);
DynamicMessage protoMsg =
JsonToProtoMessage.INSTANCE.convertToProtoMessage(
TestDatetime.getDescriptor(), tableSchema, json);
assertEquals(expectedProto, protoMsg);
}

@Test
public void testDateTimeMatch() throws Exception {
dateTimeMatch_Internal("2021-09-27T20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27t20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27 20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-9-27T20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27T00:00:00", 142258525253402624L);
dateTimeMatch_Internal("2021-09-27T00:0:00", 142258525253402624L);
dateTimeMatch_Internal("2021-09-27", 142258525253402624L);
}

@Test
public void testTimeMismatch() throws Exception {
TableFieldSchema field =
Expand Down Expand Up @@ -952,6 +980,9 @@ public void testStructComplex() throws Exception {
.setTestDate(1)
.setTestDatetime(1)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258525253402624L)
.addTestDatetimeStr(142258525253402624L)
.setComplexLvl1(
ComplexLvl1.newBuilder()
Expand Down Expand Up @@ -1020,7 +1051,14 @@ public void testStructComplex() throws Exception {
json.put("test_datetime", 1);
json.put(
"test_datetime_str",
new JSONArray(new String[] {"2021-09-27T20:51:10.752", "2021-09-27T00:00:00"}));
new JSONArray(
new String[] {
"2021-09-27T20:51:10.752",
"2021-09-27t20:51:10.752",
"2021-09-27 20:51:10.752",
"2021-09-27T00:00:00",
"2021-09-27"
}));
json.put("complex_lvl1", complex_lvl1);
json.put("complex_lvl2", complex_lvl2);
json.put(
Expand Down