Skip to content

Commit 7700632

Browse files
author
Praful Makani
committed
Fixes Bigquery Timestamp
1 parent c661608 commit 7700632

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import org.threeten.bp.Instant;
3131
import org.threeten.bp.ZoneOffset;
3232
import org.threeten.bp.format.DateTimeFormatter;
33+
import org.threeten.bp.format.DateTimeFormatterBuilder;
3334
import org.threeten.bp.format.DateTimeParseException;
35+
import org.threeten.bp.temporal.ChronoField;
3436

3537
/**
3638
* A value for a QueryParameter along with its type.
@@ -60,6 +62,21 @@
6062
@AutoValue
6163
public abstract class QueryParameterValue implements Serializable {
6264

65+
private static final DateTimeFormatter timestampValidator =
66+
new DateTimeFormatterBuilder()
67+
.parseLenient()
68+
.append(DateTimeFormatter.ISO_LOCAL_DATE)
69+
.appendLiteral(' ')
70+
.append(DateTimeFormatter.ISO_LOCAL_TIME)
71+
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
72+
.optionalStart()
73+
.appendOffsetId()
74+
.optionalEnd()
75+
.optionalStart()
76+
.appendOffset("+HHMM", "Z")
77+
.optionalEnd()
78+
.toFormatter()
79+
.withZone(ZoneOffset.UTC);
6380
private static final DateTimeFormatter timestampFormatter =
6481
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx").withZone(ZoneOffset.UTC);
6582
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@@ -301,7 +318,7 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
301318
return timestampFormatter.format(Instant.ofEpochMilli(((Long) value) / 1000));
302319
} else if (value instanceof String) {
303320
// verify that the String is in the right format
304-
checkFormat(value, timestampFormatter);
321+
checkFormat(value, timestampValidator);
305322
return (String) value;
306323
}
307324
break;
@@ -335,6 +352,7 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
335352

336353
private static void checkFormat(Object value, DateTimeFormatter formatter) {
337354
try {
355+
338356
formatter.parse((String) value);
339357
} catch (DateTimeParseException e) {
340358
throw new IllegalArgumentException(e.getMessage(), e);

google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ public void testTimestamp() {
189189
assertThat(value.getArrayValues()).isNull();
190190
}
191191

192+
@Test
193+
public void testTimestampWithDateTimeFormatterBuilder() {
194+
QueryParameterValue value = QueryParameterValue.timestamp("2019-02-14 12:34:45.938993Z");
195+
assertThat(value.getValue()).isEqualTo("2019-02-14 12:34:45.938993Z");
196+
assertThat(value.getType()).isEqualTo(StandardSQLTypeName.TIMESTAMP);
197+
assertThat(value.getArrayType()).isNull();
198+
assertThat(value.getArrayValues()).isNull();
199+
QueryParameterValue value1 = QueryParameterValue.timestamp("2019-02-14 12:34:45.938993+0000");
200+
assertThat(value1.getValue()).isEqualTo("2019-02-14 12:34:45.938993+0000");
201+
assertThat(value1.getType()).isEqualTo(StandardSQLTypeName.TIMESTAMP);
202+
assertThat(value1.getArrayType()).isNull();
203+
assertThat(value1.getArrayValues()).isNull();
204+
QueryParameterValue value2 = QueryParameterValue.timestamp("2019-02-14 12:34:45.102+00:00");
205+
assertThat(value2.getValue()).isEqualTo("2019-02-14 12:34:45.102+00:00");
206+
assertThat(value2.getType()).isEqualTo(StandardSQLTypeName.TIMESTAMP);
207+
assertThat(value2.getArrayType()).isNull();
208+
assertThat(value2.getArrayValues()).isNull();
209+
}
210+
192211
@Test(expected = IllegalArgumentException.class)
193212
public void testInvalidTimestamp() {
194213
// missing the time
@@ -269,6 +288,29 @@ public void testTimestampArray() {
269288
value.getArrayValues());
270289
}
271290

291+
@Test
292+
public void testTimestampArrayWithDateTimeFormatterBuilder() {
293+
QueryParameterValue value =
294+
QueryParameterValue.array(
295+
new String[] {
296+
"2019-02-14 12:34:45.938993Z",
297+
"2019-02-14 12:34:45.938993+0000",
298+
"2019-02-14 12:34:45.102+00:00"
299+
},
300+
StandardSQLTypeName.TIMESTAMP);
301+
assertThat(value.getValue()).isNull();
302+
assertThat(value.getType()).isEqualTo(StandardSQLTypeName.ARRAY);
303+
assertThat(value.getArrayType()).isEqualTo(StandardSQLTypeName.TIMESTAMP);
304+
assertArrayDataEquals(
305+
new String[] {
306+
"2019-02-14 12:34:45.938993Z",
307+
"2019-02-14 12:34:45.938993+0000",
308+
"2019-02-14 12:34:45.102+00:00"
309+
},
310+
StandardSQLTypeName.TIMESTAMP,
311+
value.getArrayValues());
312+
}
313+
272314
@Test
273315
public void testFromEmptyArray() {
274316
QueryParameterType typePb =

0 commit comments

Comments
 (0)