11package ru .yandex .clickhouse .integration ;
22
3+ import static org .testng .Assert .assertNull ;
34import static org .testng .Assert .assertEquals ;
45import static org .testng .Assert .assertTrue ;
56
2425import java .util .Calendar ;
2526import java .util .Objects ;
2627import java .util .TimeZone ;
28+ import java .util .UUID ;
2729
2830import org .testng .annotations .AfterTest ;
2931import org .testng .annotations .BeforeTest ;
3032import org .testng .annotations .DataProvider ;
3133import org .testng .annotations .Test ;
3234
35+ import ru .yandex .clickhouse .ClickHouseArray ;
3336import ru .yandex .clickhouse .ClickHouseConnection ;
3437import ru .yandex .clickhouse .ClickHouseContainerForTest ;
3538import ru .yandex .clickhouse .ClickHouseDataSource ;
@@ -47,7 +50,7 @@ private LocalDate instantToLocalDate(Instant instant, ZoneId zone) {
4750 ZoneRules rules = zone .getRules ();
4851 ZoneOffset offset = rules .getOffset (instant );
4952 long localSecond = instant .getEpochSecond () + offset .getTotalSeconds ();
50- long localEpochDay = Math .floorDiv (localSecond , 24 * 3600 );
53+ long localEpochDay = Math .floorDiv (localSecond , 24 * 3600L );
5154 return LocalDate .ofEpochDay (localEpochDay );
5255 }
5356
@@ -56,7 +59,7 @@ private LocalTime instantToLocalTime(Instant instant, ZoneId zone) {
5659 Objects .requireNonNull (zone , "zone" );
5760 ZoneOffset offset = zone .getRules ().getOffset (instant );
5861 long localSecond = instant .getEpochSecond () + offset .getTotalSeconds ();
59- int secondsADay = 24 * 3600 ;
62+ long secondsADay = 24 * 3600L ;
6063 int secsOfDay = (int ) (localSecond - Math .floorDiv (localSecond , secondsADay ) * secondsADay );
6164 return LocalTime .ofNanoOfDay (secsOfDay * 1000_000_000L + instant .getNano ());
6265 }
@@ -322,8 +325,9 @@ public void testDateTimeWithTimeZone(String d1TimeZone, String d2TimeZone, Strin
322325 assertEquals (r .getDate ("d2" ), expectedDate );
323326 assertEquals (r .getObject ("d2" , Date .class ), expectedDate );
324327 }
325- //expectedDate = new Date(testInstant.atZone(connServerTz.getServerTimeZone().toZoneId())
326- // .truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli());
328+ // expectedDate = new
329+ // Date(testInstant.atZone(connServerTz.getServerTimeZone().toZoneId())
330+ // .truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli());
327331 try (Statement s = connServerTz .createStatement (); ResultSet r = s .executeQuery (query );) {
328332 assertTrue (r .next ());
329333 assertEquals (r .getDate ("d0" ), expectedDate );
@@ -877,6 +881,77 @@ public void testDateWithTimeZone(String testTimeZone) throws Exception {
877881 }
878882 }
879883
884+ @ Test
885+ public void testUUID () throws Exception {
886+ try (Statement s = conn .createStatement ()) {
887+ s .execute ("DROP TABLE IF EXISTS test_uuid" );
888+ s .execute (
889+ "CREATE TABLE IF NOT EXISTS test_uuid(u0 UUID, u1 Nullable(UUID), u2 Array(UUID), u3 Array(Nullable(UUID))) ENGINE = Memory" );
890+ } catch (ClickHouseException e ) {
891+ return ;
892+ }
893+
894+ try (Statement s = conn .createStatement ()) {
895+ UUID uuid = UUID .randomUUID ();
896+ String str = uuid .toString ();
897+ s .execute ("insert into test_uuid values ('" + str + "', null, ['" + str + "'], [null])" );
898+
899+ try (ResultSet rs = s .executeQuery ("select * from test_uuid" )) {
900+ assertTrue (rs .next ());
901+
902+ assertEquals (rs .getString (1 ), str );
903+ assertEquals (rs .getObject (1 ), uuid );
904+ assertEquals (rs .getObject (1 , UUID .class ), uuid );
905+
906+ assertNull (rs .getString (2 ));
907+ assertNull (rs .getObject (2 ));
908+ assertNull (rs .getObject (2 , UUID .class ));
909+
910+ assertEquals (rs .getString (3 ), "['" + str + "']" );
911+ assertEquals (rs .getArray (3 ).getArray (), new UUID [] { uuid });
912+ assertEquals (rs .getObject (3 , ClickHouseArray .class ).getArray (), new UUID [] { uuid });
913+ assertEquals (rs .getObject (3 , UUID [].class ), new UUID [] { uuid });
914+
915+ assertEquals (rs .getString (4 ), "[NULL]" );
916+ assertEquals (rs .getArray (4 ).getArray (), new UUID [] { null });
917+ assertEquals (rs .getObject (4 , ClickHouseArray .class ).getArray (), new UUID [] { null });
918+ assertEquals (rs .getObject (4 , UUID [].class ), new UUID [] { null });
919+ }
920+
921+ s .execute ("truncate table test_uuid" );
922+ }
923+ }
924+
925+ @ Test
926+ public void testDateTime64 () throws Exception {
927+ try (Statement s = conn .createStatement ()) {
928+ s .execute ("DROP TABLE IF EXISTS test_datetime64" );
929+ s .execute (
930+ "CREATE TABLE IF NOT EXISTS test_datetime64(d0 DateTime64(3, 'UTC'), d1 Nullable(DateTime64)) ENGINE = Memory" );
931+ } catch (ClickHouseException e ) {
932+ return ;
933+ }
934+
935+ try (Statement s = conn .createStatement ()) {
936+ s .execute ("insert into test_datetime64 values (1, null)" );
937+
938+ try (ResultSet rs = s .executeQuery ("select * from test_datetime64" )) {
939+ assertTrue (rs .next ());
940+ assertEquals (rs .getObject (1 ), new Timestamp (1L ));
941+ assertEquals (rs .getObject (1 , Instant .class ), Instant .ofEpochMilli (1L ));
942+ assertEquals (rs .getObject (1 , LocalDate .class ), LocalDate .ofEpochDay (0 ));
943+ assertEquals (rs .getObject (1 , LocalDateTime .class ),
944+ Instant .ofEpochMilli (1L ).atZone (ZoneId .of ("UTC" )).toLocalDateTime ());
945+ assertNull (rs .getObject (2 ));
946+ assertNull (rs .getObject (2 , Instant .class ));
947+ assertNull (rs .getObject (2 , LocalDate .class ));
948+ assertNull (rs .getObject (2 , LocalDateTime .class ));
949+ }
950+
951+ s .execute ("truncate table test_datetime64" );
952+ }
953+ }
954+
880955 @ Test
881956 public void testDateTimes () throws Exception {
882957 try (Statement s = conn .createStatement ()) {
0 commit comments