Skip to content

Commit 8383bdf

Browse files
author
Praful Makani
committed
Fix Standard Date in QueryParameterValue classToType
1 parent 4e4ce6c commit 8383bdf

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import com.google.common.io.BaseEncoding;
2525
import java.io.Serializable;
2626
import java.math.BigDecimal;
27+
import java.text.DateFormat;
28+
import java.text.SimpleDateFormat;
2729
import java.util.ArrayList;
30+
import java.util.Date;
2831
import java.util.List;
2932
import javax.annotation.Nullable;
3033
import org.threeten.bp.Instant;
@@ -67,6 +70,7 @@ public abstract class QueryParameterValue implements Serializable {
6770
DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS");
6871
private static final DateTimeFormatter datetimeFormatter =
6972
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
73+
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
7074

7175
static final Function<
7276
QueryParameterValue, com.google.api.services.bigquery.model.QueryParameterValue>
@@ -256,6 +260,8 @@ private static <T> StandardSQLTypeName classToType(Class<T> type) {
256260
return StandardSQLTypeName.FLOAT64;
257261
} else if (BigDecimal.class.isAssignableFrom(type)) {
258262
return StandardSQLTypeName.NUMERIC;
263+
} else if (Date.class.isAssignableFrom(type)) {
264+
return StandardSQLTypeName.DATE;
259265
}
260266
throw new IllegalArgumentException("Unsupported object type for QueryParameter: " + type);
261267
}
@@ -310,6 +316,9 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
310316
// verify that the String is in the right format
311317
checkFormat(value, dateFormatter);
312318
return (String) value;
319+
} else if (value instanceof Date) {
320+
checkFormat(value, dateFormat);
321+
return dateFormat.format(value);
313322
}
314323
break;
315324
case TIME:
@@ -341,6 +350,14 @@ private static void checkFormat(Object value, DateTimeFormatter formatter) {
341350
}
342351
}
343352

353+
private static void checkFormat(Object value, DateFormat format) {
354+
try {
355+
format.format(value);
356+
} catch (Exception e) {
357+
throw new IllegalArgumentException(e.getMessage(), e);
358+
}
359+
}
360+
344361
/** Returns a builder for a QueryParameterValue object with given value. */
345362
public abstract Builder toBuilder();
346363

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020

2121
import com.google.api.services.bigquery.model.QueryParameterType;
2222
import java.math.BigDecimal;
23+
import java.text.ParseException;
24+
import java.text.SimpleDateFormat;
25+
import java.util.Date;
2326
import java.util.List;
2427
import org.junit.Test;
2528

2629
public class QueryParameterValueTest {
2730

31+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
32+
2833
@Test
2934
public void testBool() {
3035
QueryParameterValue value = QueryParameterValue.bool(true);
@@ -204,6 +209,16 @@ public void testDate() {
204209
assertThat(value.getArrayValues()).isNull();
205210
}
206211

212+
@Test
213+
public void testStandardDate() throws ParseException {
214+
Date date = SIMPLE_DATE_FORMAT.parse("2016-09-18");
215+
QueryParameterValue value = QueryParameterValue.of(date, Date.class);
216+
assertThat(value.getValue()).isEqualTo("2016-09-18");
217+
assertThat(value.getType()).isEqualTo(StandardSQLTypeName.DATE);
218+
assertThat(value.getArrayType()).isNull();
219+
assertThat(value.getArrayValues()).isNull();
220+
}
221+
207222
@Test(expected = IllegalArgumentException.class)
208223
public void testInvalidDate() {
209224
// not supposed to have the time

0 commit comments

Comments
 (0)