1
1
package ru .yandex .clickhouse .integration ;
2
2
3
+ import static org .testng .Assert .assertNull ;
3
4
import static org .testng .Assert .assertEquals ;
4
5
import static org .testng .Assert .assertTrue ;
5
6
24
25
import java .util .Calendar ;
25
26
import java .util .Objects ;
26
27
import java .util .TimeZone ;
28
+ import java .util .UUID ;
27
29
28
30
import org .testng .annotations .AfterTest ;
29
31
import org .testng .annotations .BeforeTest ;
30
32
import org .testng .annotations .DataProvider ;
31
33
import org .testng .annotations .Test ;
32
34
35
+ import ru .yandex .clickhouse .ClickHouseArray ;
33
36
import ru .yandex .clickhouse .ClickHouseConnection ;
34
37
import ru .yandex .clickhouse .ClickHouseContainerForTest ;
35
38
import ru .yandex .clickhouse .ClickHouseDataSource ;
@@ -47,7 +50,7 @@ private LocalDate instantToLocalDate(Instant instant, ZoneId zone) {
47
50
ZoneRules rules = zone .getRules ();
48
51
ZoneOffset offset = rules .getOffset (instant );
49
52
long localSecond = instant .getEpochSecond () + offset .getTotalSeconds ();
50
- long localEpochDay = Math .floorDiv (localSecond , 24 * 3600 );
53
+ long localEpochDay = Math .floorDiv (localSecond , 24 * 3600L );
51
54
return LocalDate .ofEpochDay (localEpochDay );
52
55
}
53
56
@@ -56,7 +59,7 @@ private LocalTime instantToLocalTime(Instant instant, ZoneId zone) {
56
59
Objects .requireNonNull (zone , "zone" );
57
60
ZoneOffset offset = zone .getRules ().getOffset (instant );
58
61
long localSecond = instant .getEpochSecond () + offset .getTotalSeconds ();
59
- int secondsADay = 24 * 3600 ;
62
+ long secondsADay = 24 * 3600L ;
60
63
int secsOfDay = (int ) (localSecond - Math .floorDiv (localSecond , secondsADay ) * secondsADay );
61
64
return LocalTime .ofNanoOfDay (secsOfDay * 1000_000_000L + instant .getNano ());
62
65
}
@@ -322,8 +325,9 @@ public void testDateTimeWithTimeZone(String d1TimeZone, String d2TimeZone, Strin
322
325
assertEquals (r .getDate ("d2" ), expectedDate );
323
326
assertEquals (r .getObject ("d2" , Date .class ), expectedDate );
324
327
}
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());
327
331
try (Statement s = connServerTz .createStatement (); ResultSet r = s .executeQuery (query );) {
328
332
assertTrue (r .next ());
329
333
assertEquals (r .getDate ("d0" ), expectedDate );
@@ -877,6 +881,77 @@ public void testDateWithTimeZone(String testTimeZone) throws Exception {
877
881
}
878
882
}
879
883
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
+
880
955
@ Test
881
956
public void testDateTimes () throws Exception {
882
957
try (Statement s = conn .createStatement ()) {
0 commit comments