Skip to content

Commit 914b648

Browse files
committed
Added tests for Timestamp
1 parent a6cef6b commit 914b648

File tree

5 files changed

+211
-189
lines changed

5 files changed

+211
-189
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.Reader;
66
import java.math.BigDecimal;
77
import java.math.BigInteger;
8+
import java.sql.Date;
89
import java.sql.SQLException;
910
import java.sql.Timestamp;
1011
import java.time.Duration;
@@ -17,9 +18,7 @@
1718
import java.time.format.DateTimeParseException;
1819
import java.util.Arrays;
1920
import java.util.Collection;
20-
import java.util.Date;
2121
import java.util.UUID;
22-
import java.util.concurrent.TimeUnit;
2322

2423
import com.google.common.io.ByteStreams;
2524
import com.google.common.io.CharStreams;
@@ -370,6 +369,11 @@ private static PrimitiveValue castToDate(PrimitiveType type, Object x) throws SQ
370369
return PrimitiveValue.newDate(LocalDate.ofEpochDay((Integer) x));
371370
} else if (x instanceof Long) {
372371
return PrimitiveValue.newDate(LocalDate.ofEpochDay((Long) x));
372+
} else if (x instanceof Timestamp) {
373+
// Normalize date - use system timezone to detect correct date
374+
Instant instant = Instant.ofEpochMilli(((Timestamp) x).getTime());
375+
LocalDate ld = instant.atZone(ZoneId.systemDefault()).toLocalDate();
376+
return PrimitiveValue.newDate(ld);
373377
} else if (x instanceof Date) {
374378
// Normalize date - use system timezone to detect correct date
375379
Instant instant = Instant.ofEpochMilli(((Date) x).getTime());
@@ -394,11 +398,16 @@ private static PrimitiveValue castToDateTime(PrimitiveType type, Object x) throw
394398
return PrimitiveValue.newDatetime(((LocalDate) x).atStartOfDay());
395399
} else if (x instanceof Long) {
396400
return PrimitiveValue.newDatetime(LocalDateTime.ofEpochSecond((Long) x, 0, ZoneOffset.UTC));
397-
} else if (x instanceof Date) {
401+
} else if (x instanceof Timestamp) {
398402
// Normalize date - use system timezone to detect correct date
399-
Instant instant = Instant.ofEpochMilli(((Date) x).getTime());
403+
Instant instant = Instant.ofEpochMilli(((Timestamp) x).getTime());
400404
LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
401405
return PrimitiveValue.newDatetime(ldt);
406+
} else if (x instanceof Date) {
407+
// Normalize date - use system timezone to detect correct date
408+
Instant instant = Instant.ofEpochMilli(((Date) x).getTime());
409+
LocalDate ld = instant.atZone(ZoneId.systemDefault()).toLocalDate();
410+
return PrimitiveValue.newDatetime(ld.atStartOfDay());
402411
} else if (x instanceof String) {
403412
try {
404413
return PrimitiveValue.newDatetime(LocalDateTime.parse((String) x));
@@ -413,29 +422,23 @@ private static PrimitiveValue castToTimestamp(PrimitiveType type, Object x) thro
413422
if (x instanceof Instant) {
414423
return PrimitiveValue.newTimestamp((Instant) x);
415424
} else if (x instanceof Long) {
416-
return PrimitiveValue.newTimestamp(TimeUnit.MILLISECONDS.toMicros((Long) x));
425+
return PrimitiveValue.newTimestamp(Instant.ofEpochMilli((Long) x));
417426
} else if (x instanceof LocalDate) {
418-
return PrimitiveValue.newTimestamp(((LocalDate) x).toEpochDay() * 24 * 60 * 60 * 1000000);
427+
return PrimitiveValue.newTimestamp(((LocalDate) x).atStartOfDay().toInstant(ZoneOffset.UTC));
419428
} else if (x instanceof LocalDateTime) {
420-
// LocalDateTime is usually used as Datetime analog, so truncate it to seconds
421-
long seconds = ((LocalDateTime) x).toInstant(ZoneOffset.UTC).getEpochSecond();
422-
return PrimitiveValue.newTimestamp(seconds * 1000000);
429+
long epochSeconds = ((LocalDateTime) x).toEpochSecond(ZoneOffset.UTC);
430+
return PrimitiveValue.newTimestamp(Instant.ofEpochSecond(epochSeconds));
423431
} else if (x instanceof Timestamp) {
424-
return PrimitiveValue.newTimestamp(TimeUnit.MILLISECONDS.toMicros(((Timestamp) x).getTime()));
432+
return PrimitiveValue.newTimestamp(((Timestamp) x).toInstant());
425433
} else if (x instanceof Date) {
426-
// Normalize date to UTC
427-
// Normalize date - use system timezone to detect correct datetime
428-
Instant instant = Instant.ofEpochMilli(((Date) x).getTime());
429-
LocalDate ld = instant.atZone(ZoneId.systemDefault()).toLocalDate();
430-
return PrimitiveValue.newTimestamp(ld.toEpochDay() * 24 * 60 * 60 * 1000000);
434+
Instant instant = ((Date) x).toLocalDate().atStartOfDay().toInstant(ZoneOffset.UTC);
435+
return PrimitiveValue.newTimestamp(instant);
431436
} else if (x instanceof String) {
432-
Instant parsed;
433437
try {
434-
parsed = Instant.parse((String) x);
438+
return PrimitiveValue.newTimestamp(Instant.parse((String) x));
435439
} catch (DateTimeParseException e) {
436440
throw castNotSupported(type, x, e);
437441
}
438-
return PrimitiveValue.newTimestamp(parsed);
439442
}
440443
throw castNotSupported(type, x);
441444
}

jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public boolean isNullType() {
4747
return type.getKind() == Type.Kind.NULL || type.getKind() == Type.Kind.VOID;
4848
}
4949

50+
public boolean isTimestamp() {
51+
return type == PrimitiveType.Timestamp;
52+
}
5053
public boolean isOptional() {
5154
return optional;
5255
}

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbResultSetImpl.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Objects;
2929
import java.util.Optional;
3030
import java.util.TimeZone;
31-
import java.util.function.Function;
3231

3332
import tech.ydb.jdbc.YdbConst;
3433
import tech.ydb.jdbc.YdbResultSet;
@@ -148,17 +147,41 @@ public byte[] getBytes(int columnIndex) throws SQLException {
148147

149148
@Override
150149
public Date getDate(int columnIndex) throws SQLException {
151-
return getDateImpl(columnIndex, instant -> Date.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalDate()));
150+
Instant instant = getInstant(columnIndex);
151+
if (instant == null) {
152+
return null;
153+
}
154+
if (state.description.isTimestamp()) {
155+
return new Date(instant.toEpochMilli());
156+
}
157+
158+
return Date.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalDate());
152159
}
153160

154161
@Override
155162
public Time getTime(int columnIndex) throws SQLException {
156-
return getDateImpl(columnIndex, instant -> Time.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalTime()));
163+
Instant instant = getInstant(columnIndex);
164+
if (instant == null) {
165+
return null;
166+
}
167+
if (state.description.isTimestamp()) {
168+
return new Time(instant.toEpochMilli());
169+
}
170+
171+
return Time.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalTime());
157172
}
158173

159174
@Override
160-
public Timestamp getTimestamp(int inx) throws SQLException {
161-
return getDateImpl(inx, instant -> Timestamp.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalDateTime()));
175+
public Timestamp getTimestamp(int columnIndex) throws SQLException {
176+
Instant instant = getInstant(columnIndex);
177+
if (instant == null) {
178+
return null;
179+
}
180+
if (state.description.isTimestamp()) {
181+
return Timestamp.from(instant);
182+
}
183+
184+
return Timestamp.valueOf(instant.atOffset(ZoneOffset.UTC).toLocalDateTime());
162185
}
163186

164187
@Deprecated
@@ -436,8 +459,12 @@ public YdbStatement getStatement() {
436459

437460
@Override
438461
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
462+
Instant instant = getInstant(columnIndex);
463+
if (instant == null) {
464+
return null;
465+
}
439466
final TimeZone tz = cal != null ? cal.getTimeZone() : Calendar.getInstance().getTimeZone();
440-
return getDateImpl(columnIndex, instant -> Date.valueOf(instant.atZone(tz.toZoneId()).toLocalDate()));
467+
return Date.valueOf(instant.atZone(tz.toZoneId()).toLocalDate());
441468
}
442469

443470
@Override
@@ -447,8 +474,12 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
447474

448475
@Override
449476
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
477+
Instant instant = getInstant(columnIndex);
478+
if (instant == null) {
479+
return null;
480+
}
450481
final TimeZone tz = cal != null ? cal.getTimeZone() : Calendar.getInstance().getTimeZone();
451-
return getDateImpl(columnIndex, instant -> Time.valueOf(instant.atZone(tz.toZoneId()).toLocalTime()));
482+
return Time.valueOf(instant.atZone(tz.toZoneId()).toLocalTime());
452483
}
453484

454485
@Override
@@ -458,8 +489,12 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
458489

459490
@Override
460491
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
492+
Instant instant = getInstant(columnIndex);
493+
if (instant == null) {
494+
return null;
495+
}
461496
final TimeZone tz = cal != null ? cal.getTimeZone() : Calendar.getInstance().getTimeZone();
462-
return getDateImpl(columnIndex, instant -> Timestamp.valueOf(instant.atZone(tz.toZoneId()).toLocalDateTime()));
497+
return Timestamp.valueOf(instant.atZone(tz.toZoneId()).toLocalDateTime());
463498
}
464499

465500
@Override
@@ -550,13 +585,13 @@ public ResultSetReader getYdbResultSetReader() {
550585

551586
//
552587

553-
private <T> T getDateImpl(int columnIndex, Function<Instant, T> fromMillis) throws SQLException {
588+
private Instant getInstant(int columnIndex) throws SQLException {
554589
initValueReader(columnIndex);
555-
Instant longValue = state.description.getters().readInstant(state.value);
590+
Instant instant = state.description.getters().readInstant(state.value);
556591
if (state.nullValue) {
557592
return null;
558593
}
559-
return fromMillis.apply(longValue);
594+
return instant;
560595
}
561596

562597

0 commit comments

Comments
 (0)