Skip to content

Commit 684857b

Browse files
committed
Added tests for Date
1 parent 35faa4b commit 684857b

File tree

5 files changed

+72
-108
lines changed

5 files changed

+72
-108
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,26 +366,21 @@ private static PrimitiveValue castToDate(PrimitiveType type, Object x) throws SQ
366366
return PrimitiveValue.newDate(((LocalDateTime) x).toLocalDate());
367367
} else if (x instanceof LocalDate) {
368368
return PrimitiveValue.newDate((LocalDate) x);
369+
} else if (x instanceof Integer) {
370+
return PrimitiveValue.newDate(LocalDate.ofEpochDay((Integer) x));
369371
} else if (x instanceof Long) {
370-
return PrimitiveValue.newDate(TimeUnit.MILLISECONDS.toDays((Long) x));
371-
} else if (x instanceof Timestamp) {
372-
// Normalize date - use system timezone to detect correct date
373-
Instant instant = Instant.ofEpochMilli(((Timestamp) x).getTime());
374-
LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
375-
return PrimitiveValue.newDate(ldt.toLocalDate());
372+
return PrimitiveValue.newDate(LocalDate.ofEpochDay((Long) x));
376373
} else if (x instanceof Date) {
377374
// Normalize date - use system timezone to detect correct date
378375
Instant instant = Instant.ofEpochMilli(((Date) x).getTime());
379376
LocalDate ld = instant.atZone(ZoneId.systemDefault()).toLocalDate();
380377
return PrimitiveValue.newDate(ld);
381378
} else if (x instanceof String) {
382-
Instant parsed;
383379
try {
384-
parsed = Instant.parse((String) x);
380+
return PrimitiveValue.newDate(LocalDate.parse((String) x));
385381
} catch (DateTimeParseException e) {
386382
throw castNotSupported(type, x, e);
387383
}
388-
return PrimitiveValue.newDate(parsed);
389384
}
390385
throw castNotSupported(type, x);
391386
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbDatabaseMetaDataImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public void getTypeInfo() throws SQLException {
420420
prefix.eq("'"), suffix.eq("'"), unsigned.eq(false), searchable.eq(searchNone)).assertAll();
421421

422422
rs.nextRow(name.eq("Date"), type.eq(Types.DATE), precision.eq(10), unsigned.eq(false)).assertAll();
423-
rs.nextRow(name.eq("Datetime"), type.eq(Types.TIME), precision.eq(19), unsigned.eq(false)).assertAll();
423+
rs.nextRow(name.eq("Datetime"), type.eq(Types.TIMESTAMP), precision.eq(19), unsigned.eq(false)).assertAll();
424424
rs.nextRow(name.eq("Timestamp"), type.eq(Types.TIMESTAMP), precision.eq(26), unsigned.eq(false)).assertAll();
425425
rs.nextRow(name.eq("Interval"), type.eq(Types.BIGINT), precision.eq(8), unsigned.eq(false)).assertAll();
426426

@@ -596,7 +596,7 @@ public void getColumns() throws SQLException {
596596

597597
rs.nextRow(columnName.eq("c_Date"), dataType.eq(Types.DATE), typeName.eq("Date"),
598598
columnSize.eq(10), ordinal.eq(18)).assertAll();
599-
rs.nextRow(columnName.eq("c_Datetime"), dataType.eq(Types.TIME), typeName.eq("Datetime"),
599+
rs.nextRow(columnName.eq("c_Datetime"), dataType.eq(Types.TIMESTAMP), typeName.eq("Datetime"),
600600
columnSize.eq(19), ordinal.eq(19)).assertAll();
601601
rs.nextRow(columnName.eq("c_Timestamp"), dataType.eq(Types.TIMESTAMP), typeName.eq("Timestamp"),
602602
columnSize.eq(26), ordinal.eq(20)).assertAll();

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 60 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -563,113 +563,83 @@ public void datetimeTest(SqlQueries.JdbcQuery query) throws SQLException {
563563
}
564564
};
565565

566-
private void assertDate(ResultSet rs, LocalDate ld, LocalDateTime ldt, Instant instant) throws SQLException {
567-
// TODO: NOT SUPPORTED YET
568-
// Assert.assertEquals(ld, rs.getObject("c_Datetime", LocalDate.class));
569-
// Assert.assertEquals(ldt, rs.getObject("c_Datetime", LocalDateTime.class));
570-
Object obj = rs.getObject("c_Date");
571-
Assert.assertTrue(obj instanceof LocalDate);
572-
Assert.assertEquals(ld, obj);
573-
574-
Assert.assertEquals(new Date(instant.toEpochMilli()), rs.getDate("c_Date"));
575-
Assert.assertEquals(new Timestamp(instant.toEpochMilli()), rs.getTimestamp("c_Date"));
576-
Assert.assertEquals(instant.toEpochMilli(), rs.getLong("c_Date"));
577-
Assert.assertEquals(ld.toString(), rs.getString("c_Date"));
578-
}
579-
580-
@Disabled
581566
@ParameterizedTest(name = "with {0}")
582567
@EnumSource(SqlQueries.JdbcQuery.class)
583568
public void dateTest(SqlQueries.JdbcQuery query) throws SQLException {
584569
String upsert = TEST_TABLE.upsertOne(query, "c_Date", "Date");
570+
boolean castingSupported = query != SqlQueries.JdbcQuery.IN_MEMORY;
585571

586-
try (PreparedStatement statement = jdbc.connection().prepareStatement(upsert)) {
587-
statement.setInt(1, 1);
588-
statement.setObject(2, LocalDate.of(2023, Month.MARCH, 3));
589-
statement.execute();
572+
try (PreparedStatement ps = jdbc.connection().prepareStatement(upsert)) {
573+
ps.setInt(1, 1);
574+
ps.setObject(2, LocalDate.of(2025, Month.AUGUST, 10));
575+
ps.execute();
590576

591-
statement.setInt(1, 2);
592-
statement.setObject(2, LocalDateTime.of(2023, Month.MARCH, 3, 14, 56, 59, 123456789));
593-
statement.execute();
577+
ps.setInt(1, 2);
578+
ps.setDate(2, new Date(INSTANT.toEpochMilli()));
579+
ps.execute();
594580

595-
statement.setInt(1, 3);
596-
statement.setDate(2, new Date(INSTANT.toEpochMilli()));
597-
statement.execute();
581+
if (castingSupported) {
582+
ps.setInt(1, 3);
583+
ps.setTimestamp(2, new Timestamp(INSTANT.toEpochMilli()));
584+
ps.execute();
598585

599-
statement.setInt(1, 4);
600-
statement.setTimestamp(2, new Timestamp(INSTANT.toEpochMilli()));
601-
statement.execute();
586+
ps.setInt(1, 4);
587+
ps.setInt(2, 10); // Jan 11 1970
588+
ps.execute();
602589

603-
if (query != SqlQueries.JdbcQuery.IN_MEMORY) { // IN MEMORY is not typed mode, casting is not supported
604-
statement.setInt(1, 5);
605-
statement.setLong(2, 1585932011123l);
606-
statement.execute();
590+
ps.setInt(1, 5);
591+
ps.setLong(2, 12345); // Oct 20 2003
592+
ps.execute();
607593

608-
statement.setInt(1, 6);
609-
statement.setString(2, "2011-12-03T10:15:30.456789123Z");
610-
statement.execute();
594+
ps.setInt(1, 6);
595+
ps.setObject(2, LocalDateTime.of(2023, Month.MARCH, 3, 14, 56, 59, 123456789));
596+
ps.execute();
597+
598+
ps.setInt(1, 7);
599+
ps.setString(2, "2011-12-03");
600+
ps.execute();
611601
}
612602
}
613603

614-
try (Statement statement = jdbc.connection().createStatement()) {
615-
try (ResultSet rs = statement.executeQuery(TEST_TABLE.selectColumn("c_Date"))) {
616-
Assert.assertTrue(rs.next());
617-
Assert.assertEquals(1, rs.getInt("key"));
618-
// LocalDate.of(2023, Month.MARCH, 3) UTC == 1677801600000l;
619-
assertDate(rs,
620-
LocalDate.of(2023, Month.MARCH, 3),
621-
LocalDateTime.of(2023, Month.MARCH, 3, 0, 0, 0),
622-
Instant.ofEpochSecond(1677801600l, 0)
623-
);
624-
625-
Assert.assertTrue(rs.next());
626-
Assert.assertEquals(2, rs.getInt("key"));
627-
// LocalDateTime.of(2023, Month.MARCH, 3, 14, 56, 59, 123456789) UTC == 1677855419123l;
628-
assertDate(rs,
629-
LocalDate.of(2023, Month.MARCH, 3),
630-
LocalDateTime.of(2023, Month.MARCH, 3, 0, 0, 0), // Timestamp supports only micros
631-
Instant.ofEpochSecond(1677801600l)
632-
);
633-
634-
Assert.assertTrue(rs.next());
635-
Assert.assertEquals(3, rs.getInt("key"));
636-
assertDate(rs,
637-
LocalDate.of(2020, Month.APRIL, 3),
638-
LocalDateTime.of(2020, Month.APRIL, 3, 0, 0, 0),
639-
Instant.ofEpochSecond(1585872000l) // Friday, April 3, 2022 00:00:00 UTC
640-
);
641-
642-
Assert.assertTrue(rs.next());
643-
Assert.assertEquals(4, rs.getInt("key"));
644-
// Instant.ofEpochMilli(1585932011123l) == Friday, April 3, 2020 16:40:11.123 UTC
645-
assertDate(rs,
646-
LocalDate.of(2020, Month.APRIL, 3),
647-
LocalDateTime.of(2020, Month.APRIL, 3, 0, 0, 0), // Timestamp supports only micros
648-
Instant.ofEpochSecond(1585872000l)
649-
);
650-
651-
if (query != SqlQueries.JdbcQuery.IN_MEMORY) { // IN MEMORY is not typed mode, casting is not supported
652-
Assert.assertTrue(rs.next());
653-
Assert.assertEquals(5, rs.getInt("key"));
654-
// Instant.ofEpochMilli(1585932011123l) == Friday, April 3, 2020 16:40:11.123 UTC
655-
assertDate(rs,
656-
LocalDate.of(2020, Month.APRIL, 3),
657-
LocalDateTime.of(2020, Month.APRIL, 3, 16, 40, 11),
658-
Instant.ofEpochSecond(1585872000l)
659-
);
660-
661-
Assert.assertTrue(rs.next());
662-
Assert.assertEquals(6, rs.getInt("key"));
663-
// 2011-12-03T10:15:30.456789123Z
664-
assertDate(rs,
665-
LocalDate.of(2011, Month.DECEMBER, 3),
666-
LocalDateTime.of(2011, Month.DECEMBER, 3, 10, 15, 30),
667-
Instant.ofEpochSecond(1322870400l)
668-
);
604+
try (Statement st = jdbc.connection().createStatement()) {
605+
try (ResultSet rs = st.executeQuery(TEST_TABLE.selectColumn("c_Date"))) {
606+
assertNextDate(rs, 1, LocalDate.of(2025, Month.AUGUST, 10));
607+
assertNextDate(rs, 2, INSTANT.atZone(ZoneId.systemDefault()).toLocalDate());
608+
609+
if (castingSupported) {
610+
assertNextDate(rs, 3, INSTANT.atZone(ZoneId.systemDefault()).toLocalDate());
611+
assertNextDate(rs, 4, LocalDate.of(1970, Month.JANUARY, 11));
612+
assertNextDate(rs, 5, LocalDate.of(2003, Month.OCTOBER, 20));
613+
assertNextDate(rs, 6, LocalDate.of(2023, Month.MARCH, 3));
614+
assertNextDate(rs, 7, LocalDate.of(2011, Month.DECEMBER, 3));
669615
}
670616

671617
Assert.assertFalse(rs.next());
672618
}
673619
}
674620
};
621+
622+
private void assertNextDate(ResultSet rs, int key, LocalDate ld) throws SQLException {
623+
Assert.assertTrue(rs.next());
624+
Assert.assertEquals(key, rs.getInt("key"));
625+
626+
Object obj = rs.getObject("c_Date");
627+
Assert.assertTrue(obj instanceof LocalDate);
628+
Assert.assertEquals(ld, obj);
629+
630+
Assert.assertEquals(ld.toEpochDay(), rs.getInt("c_Date"));
631+
Assert.assertEquals(ld.toEpochDay(), rs.getLong("c_Date"));
632+
633+
Assert.assertEquals(Date.valueOf(ld), rs.getDate("c_Date"));
634+
Assert.assertEquals(Timestamp.valueOf(LocalDateTime.of(ld, LocalTime.MIN)), rs.getTimestamp("c_Date"));
635+
Assert.assertEquals(ld.toString(), rs.getString("c_Date"));
636+
637+
Assert.assertEquals(Long.valueOf(ld.toEpochDay()), rs.getObject("c_Date", Long.class));
638+
Assert.assertEquals(ld, rs.getObject("c_Date", LocalDate.class));
639+
Assert.assertEquals(ld.atTime(LocalTime.MIN), rs.getObject("c_Date", LocalDateTime.class));
640+
Assert.assertEquals(ld.atTime(LocalTime.MIN)
641+
.atZone(ZoneId.systemDefault())
642+
.toInstant(),
643+
rs.getObject("c_Date", Instant.class));
644+
}
675645
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbResultSetImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ public void getLong() throws SQLException {
10791079
.value(8, "c_Uint16", 20002L)
10801080
.value(9, "c_Uint32", 2000000002L)
10811081
.value(10, "c_Uint64", 2000000000002L)
1082-
.value(18, "c_Date", 3111L * 24 * 60 * 60 * 1000)
1082+
.value(18, "c_Date", 3111l)
10831083
.value(19, "c_Datetime", 311111156L * 1000)
10841084
.value(20, "c_Timestamp", 311111223342L / 1000)
10851085
.value(21, "c_Interval", 3111113L)
@@ -1096,7 +1096,7 @@ public void getLong() throws SQLException {
10961096
.value(8, "c_Uint16", 40002L)
10971097
.value(9, "c_Uint32", 4000000002L)
10981098
.value(10, "c_Uint64", 4000000000002L)
1099-
.value(18, "c_Date", 3112L * 24 * 60 * 60 * 1000)
1099+
.value(18, "c_Date", 3112l)
11001100
.value(19, "c_Datetime", 211211100L * 1000)
11011101
.value(20, "c_Timestamp", 111111223342L / 1000)
11021102
.value(21, "c_Interval", 3112113L)
@@ -1130,7 +1130,7 @@ public void getLong() throws SQLException {
11301130
.value(8, "c_Uint16", 1L)
11311131
.value(9, "c_Uint32", 1L)
11321132
.value(10, "c_Uint64", 1L)
1133-
.value(18, "c_Date", 1L * 24 * 60 * 60 * 1000)
1133+
.value(18, "c_Date", 1L)
11341134
.value(19, "c_Datetime", 1L * 1000L)
11351135
.value(20, "c_Timestamp", 1L / 1000)
11361136
.value(21, "c_Interval", 1L)

jdbc/src/test/java/tech/ydb/jdbc/impl/helper/SqlQueries.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class SqlQueries {
1414
public enum JdbcQuery {
15-
STANDART,
15+
STANDARD,
1616
IN_MEMORY,
1717
TYPED,
1818
BATCHED,
@@ -41,8 +41,7 @@ public enum YqlQuery {
4141
private static final String SELECT_COLUMN = "select key, #column from #tableName";
4242
private static final String WRONG_SELECT = "select key2 from #tableName";
4343

44-
private static final Map<JdbcQuery, String> JDBC_UPSERT_ONE = ImmutableMap.of(
45-
JdbcQuery.STANDART, "" +
44+
private static final Map<JdbcQuery, String> JDBC_UPSERT_ONE = ImmutableMap.of(JdbcQuery.STANDARD, "" +
4645
"upsert into #tableName (key, #column) values (?, ?)",
4746

4847
JdbcQuery.IN_MEMORY, "" +
@@ -129,7 +128,7 @@ public String upsertAll(JdbcQuery mode) {
129128
return withTableName(TYPED_BATCH, tableName);
130129
case TYPED:
131130
return withTableName(TYPED_UPSERT, tableName);
132-
case STANDART:
131+
case STANDARD:
133132
default:
134133
return withTableName(SIMPLE_UPSERT, tableName);
135134
}

0 commit comments

Comments
 (0)