|
21 | 21 | import static com.google.common.truth.Truth.assertThat; |
22 | 22 | import static org.apache.arrow.vector.types.Types.MinorType.INT; |
23 | 23 | import static org.apache.arrow.vector.types.Types.MinorType.VARCHAR; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
24 | 25 | import static org.mockito.Mockito.mock; |
25 | 26 |
|
26 | 27 | import com.google.cloud.bigquery.Field; |
27 | 28 | import com.google.cloud.bigquery.Field.Mode; |
28 | 29 | import com.google.cloud.bigquery.FieldList; |
29 | 30 | import com.google.cloud.bigquery.Schema; |
30 | 31 | import com.google.cloud.bigquery.StandardSQLTypeName; |
| 32 | +import com.google.cloud.bigquery.jdbc.rules.TimeZoneRule; |
31 | 33 | import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch; |
32 | 34 | import com.google.cloud.bigquery.storage.v1.ArrowSchema; |
33 | 35 | import com.google.common.collect.ImmutableList; |
34 | 36 | import java.io.IOException; |
| 37 | +import java.math.BigDecimal; |
35 | 38 | import java.sql.Array; |
| 39 | +import java.sql.Date; |
36 | 40 | import java.sql.ResultSet; |
37 | 41 | import java.sql.SQLException; |
38 | 42 | import java.sql.Struct; |
| 43 | +import java.sql.Time; |
| 44 | +import java.sql.Timestamp; |
| 45 | +import java.time.LocalDate; |
| 46 | +import java.time.LocalDateTime; |
| 47 | +import java.time.LocalTime; |
39 | 48 | import java.util.Arrays; |
40 | 49 | import java.util.List; |
| 50 | +import java.util.TimeZone; |
41 | 51 | import java.util.concurrent.BlockingQueue; |
42 | 52 | import java.util.concurrent.LinkedBlockingDeque; |
| 53 | +import java.util.stream.Stream; |
43 | 54 | import org.apache.arrow.memory.RootAllocator; |
44 | 55 | import org.apache.arrow.vector.BitVector; |
45 | 56 | import org.apache.arrow.vector.DateMilliVector; |
|
59 | 70 | import org.apache.arrow.vector.util.Text; |
60 | 71 | import org.junit.jupiter.api.BeforeEach; |
61 | 72 | import org.junit.jupiter.api.Test; |
| 73 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 74 | +import org.junit.jupiter.params.ParameterizedTest; |
| 75 | +import org.junit.jupiter.params.provider.Arguments; |
| 76 | +import org.junit.jupiter.params.provider.MethodSource; |
62 | 77 |
|
63 | 78 | public class BigQueryArrowResultSetTest { |
64 | 79 |
|
| 80 | + static { |
| 81 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
| 82 | + TimeZoneCache.reset(); |
| 83 | + } |
| 84 | + |
| 85 | + @RegisterExtension public static final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC"); |
| 86 | + |
65 | 87 | private static final FieldList fieldList = |
66 | 88 | FieldList.of( |
67 | 89 | Field.of("boolField", StandardSQLTypeName.BOOL), |
@@ -110,7 +132,7 @@ private VectorSchemaRoot getTestVectorSchemaRoot() { |
110 | 132 | Float8Vector float64Field = |
111 | 133 | new Float8Vector("float64Field", allocator); // Mapped with StandardSQLTypeName.FLOAT64 |
112 | 134 | float64Field.allocateNew(2); |
113 | | - float64Field.set(0, 1.1f); |
| 135 | + float64Field.set(0, 1.1); |
114 | 136 | float64Field.setValueCount(1); |
115 | 137 | VarCharVector stringField = |
116 | 138 | new VarCharVector("stringField", allocator); // Mapped with StandardSQLTypeName.STRING |
@@ -346,6 +368,66 @@ public void testIterationNested() throws SQLException { |
346 | 368 | assertThat(bigQueryArrowResultSetNested.isAfterLast()).isTrue(); |
347 | 369 | } |
348 | 370 |
|
| 371 | + public static Stream<Arguments> successfulCoercionCases() { |
| 372 | + return Stream.of( |
| 373 | + Arguments.of("dateField", LocalDate.class, LocalDate.of(1970, 1, 1)), |
| 374 | + Arguments.of(11, LocalDate.class, LocalDate.of(1970, 1, 1)), |
| 375 | + Arguments.of("timeField", LocalTime.class, LocalTime.of(0, 0, 1, 234_000_000)), |
| 376 | + Arguments.of(10, LocalTime.class, LocalTime.of(0, 0, 1, 234_000_000)), |
| 377 | + Arguments.of( |
| 378 | + "timeStampField", |
| 379 | + LocalDateTime.class, |
| 380 | + LocalDateTime.of(1970, 1, 1, 0, 0, 0, 10_000_000)), |
| 381 | + Arguments.of(5, LocalDateTime.class, LocalDateTime.of(1970, 1, 1, 0, 0, 0, 10_000_000)), |
| 382 | + Arguments.of("boolField", Boolean.class, false), |
| 383 | + Arguments.of(1, Boolean.class, false), |
| 384 | + Arguments.of("int64Filed", Long.class, 1L), |
| 385 | + Arguments.of(2, Integer.class, 1), |
| 386 | + Arguments.of(2, String.class, "1"), |
| 387 | + Arguments.of("float64Field", Double.class, 1.1), |
| 388 | + Arguments.of("stringField", String.class, "text1"), |
| 389 | + Arguments.of("numericField", BigDecimal.class, BigDecimal.ONE), |
| 390 | + Arguments.of(9, Long.class, 1L), |
| 391 | + Arguments.of("timeField", Time.class, new Time(1234L)), |
| 392 | + Arguments.of(10, Time.class, new Time(1234L)), |
| 393 | + Arguments.of("timeStampField", Timestamp.class, new Timestamp(10L)), |
| 394 | + Arguments.of(5, Timestamp.class, new Timestamp(10L)), |
| 395 | + Arguments.of("dateField", Date.class, new Date(0L)), |
| 396 | + Arguments.of(11, Date.class, new Date(0L))); |
| 397 | + } |
| 398 | + |
| 399 | + public static Stream<Arguments> failingCoercionCases() { |
| 400 | + return Stream.of( |
| 401 | + Arguments.of("boolField", LocalDate.class), |
| 402 | + Arguments.of("dateField", Boolean.class), |
| 403 | + Arguments.of("stringField", BigDecimal.class)); |
| 404 | + } |
| 405 | + |
| 406 | + @ParameterizedTest |
| 407 | + @MethodSource("successfulCoercionCases") |
| 408 | + public void testGetObjectWithType_success(Object column, Class<?> type, Object expectedValue) |
| 409 | + throws SQLException { |
| 410 | + assertThat(bigQueryArrowResultSet.next()).isTrue(); |
| 411 | + if (column instanceof String) { |
| 412 | + assertThat(bigQueryArrowResultSet.getObject((String) column, type)).isEqualTo(expectedValue); |
| 413 | + } else { |
| 414 | + assertThat(bigQueryArrowResultSet.getObject((Integer) column, type)).isEqualTo(expectedValue); |
| 415 | + } |
| 416 | + } |
| 417 | + |
| 418 | + @ParameterizedTest |
| 419 | + @MethodSource("failingCoercionCases") |
| 420 | + public void testGetObjectWithType_failure(Object column, Class<?> type) throws SQLException { |
| 421 | + assertThat(bigQueryArrowResultSet.next()).isTrue(); |
| 422 | + if (column instanceof String) { |
| 423 | + assertThrows( |
| 424 | + SQLException.class, () -> bigQueryArrowResultSet.getObject((String) column, type)); |
| 425 | + } else { |
| 426 | + assertThrows( |
| 427 | + SQLException.class, () -> bigQueryArrowResultSet.getObject((Integer) column, type)); |
| 428 | + } |
| 429 | + } |
| 430 | + |
349 | 431 | private int resultSetRowCount(BigQueryArrowResultSet resultSet) throws SQLException { |
350 | 432 | int rowCount = 0; |
351 | 433 | while (resultSet.next()) { |
|
0 commit comments