50
50
import java .io .OutputStreamWriter ;
51
51
import java .math .BigDecimal ;
52
52
import java .math .BigInteger ;
53
+ import java .net .Inet4Address ;
54
+ import java .net .Inet6Address ;
55
+ import java .net .InetAddress ;
56
+ import java .net .UnknownHostException ;
53
57
import java .time .LocalDate ;
54
58
import java .time .LocalDateTime ;
55
59
import java .time .ZoneId ;
@@ -181,6 +185,39 @@ public void testBigUnsignedInt() throws Exception {
181
185
Assert .assertEquals (firstRecord .getBigInteger ("i256" ), expected256 );
182
186
}
183
187
188
+ @ Test (groups = {"integration" })
189
+ public void testEndianReadingNumbers () throws Exception {
190
+
191
+ byte [][] numbers = new byte [][] {
192
+ new byte [] {0x00 , 0x02 , 0x00 , 0x01 },
193
+ new byte [] {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 },
194
+ new byte [] {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x10 },
195
+ };
196
+
197
+
198
+ for (byte [] number : numbers ) {
199
+ String typeName = "UInt32" ;
200
+ if (number .length == 8 ) {
201
+ typeName = "UInt64" ;
202
+ } else if (number .length == 16 ) {
203
+ typeName = "UInt128" ;
204
+ }
205
+ BigInteger expected = new BigInteger (number );
206
+ String sqlQuery = "SELECT to" + typeName + "('" + expected + "') as value1" ;
207
+ System .out .println (sqlQuery );
208
+ Records records = client .queryRecords (sqlQuery ).get (3 , TimeUnit .SECONDS );
209
+ GenericRecord firstRecord = records .iterator ().next ();
210
+
211
+ if (number .length == 4 ) {
212
+ System .out .println (firstRecord .getLong ("value1" ));
213
+ Assert .assertEquals (firstRecord .getLong ("value1" ), expected .longValue ());
214
+ } else {
215
+ System .out .println (firstRecord .getBigInteger ("value1" ));
216
+ Assert .assertEquals (firstRecord .getBigInteger ("value1" ), expected );
217
+ }
218
+ }
219
+ }
220
+
184
221
@ Test (groups = {"integration" })
185
222
public void testReadRecordsWithStreamAPI () throws Exception {
186
223
final int tables = 10 ;
@@ -426,7 +463,8 @@ record = reader.next();
426
463
427
464
private final static List <Function <String , Object >> ARRAY_VALUE_GENERATORS = Arrays .asList (
428
465
c ->
429
- RANDOM .ints (10 , 0 , 100 ),
466
+ RANDOM .ints (10 , 0 , 100 )
467
+ .asLongStream ().collect (ArrayList ::new , ArrayList ::add , ArrayList ::addAll ),
430
468
c -> {
431
469
List <List <Integer >> values = new ArrayList <>();
432
470
for (int i = 0 ; i < 10 ; i ++) {
@@ -453,9 +491,13 @@ public void testArrayValues() throws Exception {
453
491
454
492
Map <String , Object > record = reader .next ();
455
493
Assert .assertNotNull (record );
494
+ Map <String , Object > datasetRecord = data .get (0 );
456
495
long [] col1Values = reader .getLongArray ("col1" );
457
- System .out .println ("col1: " + Arrays .toString (col1Values ));
458
- System .out .println ("Record: " + record );
496
+ Assert .assertEquals (Arrays .stream (col1Values ).collect (ArrayList <Long >::new , ArrayList ::add ,
497
+ ArrayList ::addAll ), datasetRecord .get ("col1" ));
498
+ Assert .assertEquals (reader .getList ("col1" ), datasetRecord .get ("col1" ));
499
+ List <List <Long >> col2Values = reader .getList ("col2" );
500
+ Assert .assertEquals (col2Values , data .get (0 ).get ("col2" ));
459
501
}
460
502
461
503
private final static List <String > MAP_COLUMNS = Arrays .asList (
@@ -574,6 +616,65 @@ public void testNullValues() throws Exception {
574
616
}
575
617
}
576
618
619
+ @ Test
620
+ public void testIPAddresses () throws Exception {
621
+
622
+ final List <String > columns = Arrays .asList (
623
+ "srcV4 IPv4" ,
624
+ "targetV4 IPv4" ,
625
+ "srcV6 IPv6" ,
626
+ "targetV6 IPv6"
627
+
628
+ );
629
+
630
+ Random random = new Random ();
631
+ byte [] ipv4 = new byte [4 ];
632
+ random .nextBytes (ipv4 );
633
+ InetAddress ipv4src = Inet4Address .getByAddress (ipv4 );
634
+ random .nextBytes (ipv4 );
635
+ InetAddress ipv4target = Inet4Address .getByAddress (ipv4 );
636
+ byte [] ipv6 = new byte [16 ];
637
+ random .nextBytes (ipv6 );
638
+ InetAddress ipv6src = Inet6Address .getByAddress (ipv6 );
639
+ random .nextBytes (ipv6 );
640
+ InetAddress ipv6target = Inet6Address .getByAddress (ipv6 );
641
+
642
+
643
+ final List <Supplier <String >> valueGenerators = Arrays .asList (
644
+ () -> sq (ipv4src .getHostAddress ()),
645
+ () -> sq (ipv4target .getHostAddress ()),
646
+ () -> sq (ipv6src .getHostAddress ()),
647
+ () -> sq (ipv6target .getHostAddress ())
648
+ );
649
+
650
+ final List <Consumer <ClickHouseBinaryFormatReader >> verifiers = new ArrayList <>();
651
+ verifiers .add (r -> {
652
+ Assert .assertTrue (r .hasValue ("srcV4" ), "No value for column srcV4 found" );
653
+ Assert .assertEquals (r .getInet4Address ("srcV4" ), ipv4src );
654
+ Assert .assertEquals (r .getInet4Address (1 ), ipv4src );
655
+ });
656
+
657
+ verifiers .add (r -> {
658
+ Assert .assertTrue (r .hasValue ("targetV4" ), "No value for column targetV4 found" );
659
+ Assert .assertEquals (r .getInet4Address ("targetV4" ), ipv4target );
660
+ Assert .assertEquals (r .getInet4Address (2 ), ipv4target );
661
+ });
662
+
663
+ verifiers .add (r -> {
664
+ Assert .assertTrue (r .hasValue ("srcV6" ), "No value for column src6 found" );
665
+ Assert .assertEquals (r .getInet6Address ("srcV6" ), ipv6src );
666
+ Assert .assertEquals (r .getInet6Address (3 ), ipv6src );
667
+ });
668
+
669
+ verifiers .add (r -> {
670
+ Assert .assertTrue (r .hasValue ("targetV6" ), "No value for column targetV6 found" );
671
+ Assert .assertEquals (r .getInet6Address ("targetV6" ), ipv6target );
672
+ Assert .assertEquals (r .getInet6Address (4 ), ipv6target );
673
+ });
674
+
675
+ testDataTypes (columns , valueGenerators , verifiers );
676
+ }
677
+
577
678
@ Test
578
679
public void testDateTimeDataTypes () {
579
680
final List <String > columns = Arrays .asList (
0 commit comments