Skip to content

Commit a2a7930

Browse files
committed
Add unit tests for java.time objects for TimeStampVectorAccessor
1 parent ee121e5 commit a2a7930

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@
2424
import java.sql.Date;
2525
import java.sql.Time;
2626
import java.sql.Timestamp;
27+
import java.time.Instant;
2728
import java.time.LocalDateTime;
29+
import java.time.OffsetDateTime;
30+
import java.time.ZoneId;
31+
import java.time.ZonedDateTime;
2832
import java.util.Calendar;
33+
import java.util.Objects;
2934
import java.util.TimeZone;
3035
import java.util.concurrent.TimeUnit;
3136
import java.util.function.Supplier;
@@ -198,6 +203,82 @@ public void testShouldGetTimestampReturnValidTimestampWithCalendar(
198203
});
199204
}
200205

206+
@ParameterizedTest
207+
@MethodSource("data")
208+
public void testShouldGetObjectReturnValidLocalDateTime(
209+
Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone)
210+
throws Exception {
211+
setup(vectorSupplier);
212+
final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC");
213+
214+
accessorIterator.iterate(
215+
vector,
216+
(accessor, currentRow) -> {
217+
final LocalDateTime value = accessor.getObject(LocalDateTime.class);
218+
219+
assertThat(
220+
value, equalTo(getZonedDateTime(currentRow, expectedTimeZone).toLocalDateTime()));
221+
assertThat(accessor.wasNull(), is(false));
222+
});
223+
}
224+
225+
@ParameterizedTest
226+
@MethodSource("data")
227+
public void testShouldGetObjectReturnValidInstant(
228+
Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone)
229+
throws Exception {
230+
setup(vectorSupplier);
231+
final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC");
232+
233+
accessorIterator.iterate(
234+
vector,
235+
(accessor, currentRow) -> {
236+
final Instant value = accessor.getObject(Instant.class);
237+
238+
assertThat(value, equalTo(getZonedDateTime(currentRow, expectedTimeZone).toInstant()));
239+
assertThat(accessor.wasNull(), is(false));
240+
});
241+
}
242+
243+
@ParameterizedTest
244+
@MethodSource("data")
245+
public void testShouldGetObjectReturnValidOffsetDateTime(
246+
Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone)
247+
throws Exception {
248+
setup(vectorSupplier);
249+
final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC");
250+
251+
accessorIterator.iterate(
252+
vector,
253+
(accessor, currentRow) -> {
254+
final OffsetDateTime value = accessor.getObject(OffsetDateTime.class);
255+
final OffsetDateTime vectorValue =
256+
getZonedDateTime(currentRow, expectedTimeZone).toOffsetDateTime();
257+
assertThat(value, equalTo(vectorValue));
258+
assertThat(value.getOffset(), equalTo(vectorValue.getOffset()));
259+
assertThat(accessor.wasNull(), is(false));
260+
});
261+
}
262+
263+
@ParameterizedTest
264+
@MethodSource("data")
265+
public void testShouldGetObjectReturnValidZonedDateTime(
266+
Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone)
267+
throws Exception {
268+
setup(vectorSupplier);
269+
final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC");
270+
271+
accessorIterator.iterate(
272+
vector,
273+
(accessor, currentRow) -> {
274+
final ZonedDateTime value = accessor.getObject(ZonedDateTime.class);
275+
276+
assertThat(value, equalTo(getZonedDateTime(currentRow, expectedTimeZone)));
277+
assertThat(value.getZone(), equalTo(ZoneId.of(expectedTimeZone)));
278+
assertThat(accessor.wasNull(), is(false));
279+
});
280+
}
281+
201282
@ParameterizedTest
202283
@MethodSource("data")
203284
public void testShouldGetTimestampReturnNull(Supplier<TimeStampVector> vectorSupplier) {
@@ -319,6 +400,24 @@ private Timestamp getTimestampForVector(int currentRow, String timeZone) {
319400
return expectedTimestamp;
320401
}
321402

403+
private ZonedDateTime getZonedDateTime(int currentRow, String timeZone) {
404+
Object object = vector.getObject(currentRow);
405+
TimeZone tz = TimeZone.getTimeZone(timeZone);
406+
ZonedDateTime expectedTimestamp = null;
407+
if (object instanceof LocalDateTime) {
408+
expectedTimestamp = ((LocalDateTime) object).atZone(tz.toZoneId());
409+
} else if (object instanceof Long) {
410+
TimeUnit timeUnit = getTimeUnitForVector(vector);
411+
long millis = timeUnit.toMillis((Long) object);
412+
long offset = tz.getOffset(millis);
413+
// TODO: should we actually add the offset here? I'm not completely sure how the value is
414+
// stored in the vector
415+
LocalDateTime local = new Timestamp(millis + offset).toLocalDateTime();
416+
expectedTimestamp = ZonedDateTime.of(local, tz.toZoneId());
417+
}
418+
return expectedTimestamp;
419+
}
420+
322421
@ParameterizedTest
323422
@MethodSource("data")
324423
public void testShouldGetObjectClass(Supplier<TimeStampVector> vectorSupplier) throws Exception {

0 commit comments

Comments
 (0)