Skip to content

Commit 38991f1

Browse files
committed
fixed primitive arrays. added more tests
1 parent bc4c13b commit 38991f1

File tree

4 files changed

+99
-25
lines changed

4 files changed

+99
-25
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.clickhouse.client.api.metadata.NoSuchColumnException;
99
import com.clickhouse.client.api.metadata.TableSchema;
1010
import com.clickhouse.client.api.query.NullValueException;
11-
import com.clickhouse.client.api.serde.POJOFieldDeserializer;
1211
import com.clickhouse.client.api.query.QuerySettings;
12+
import com.clickhouse.client.api.serde.POJOFieldDeserializer;
1313
import com.clickhouse.data.ClickHouseColumn;
1414
import com.clickhouse.data.ClickHouseDataType;
1515
import com.clickhouse.data.value.ClickHouseBitmap;
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.lang.ref.WeakReference;
27+
import java.lang.reflect.Array;
2728
import java.math.BigDecimal;
2829
import java.math.BigInteger;
2930
import java.net.Inet4Address;
@@ -524,56 +525,93 @@ public ClickHouseGeoMultiPolygonValue getGeoMultiPolygon(String colName) {
524525

525526
@Override
526527
public <T> List<T> getList(String colName) {
527-
try {
528-
BinaryStreamReader.ArrayValue array = readValue(colName);
529-
return array.asList();
530-
} catch (ClassCastException e) {
531-
throw new ClientException("Column is not of array type", e);
528+
Object value = readValue(colName);
529+
if (value instanceof BinaryStreamReader.ArrayValue) {
530+
return ((BinaryStreamReader.ArrayValue) value).asList();
531+
} else if (value instanceof List<?>) {
532+
return (List<T>) value;
533+
} else {
534+
throw new ClientException("Column is not of array type");
532535
}
533536
}
534537

535538

536-
private <T> T getPrimitiveArray(String colName) {
539+
private <T> T getPrimitiveArray(String colName, Class<?> componentType) {
537540
try {
538-
BinaryStreamReader.ArrayValue array = readValue(colName);
539-
if (array.itemType.isPrimitive()) {
540-
return (T) array.array;
541-
} else {
542-
throw new ClientException("Array is not of primitive type");
541+
Object value = readValue(colName);
542+
if (value instanceof BinaryStreamReader.ArrayValue) {
543+
BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) value;
544+
if (array.itemType.isPrimitive()) {
545+
return (T) array.array;
546+
} else {
547+
throw new ClientException("Array is not of primitive type");
548+
}
549+
} else if (value instanceof List<?>) {
550+
List<?> list = (List<?>) value;
551+
Object array = Array.newInstance(componentType, list.size());
552+
for (int i = 0; i < list.size(); i++) {
553+
Array.set(array, i, list.get(i));
554+
}
555+
return (T)array;
543556
}
557+
throw new ClientException("Column is not of array type");
544558
} catch (ClassCastException e) {
545559
throw new ClientException("Column is not of array type", e);
546560
}
547561
}
548562

549563
@Override
550564
public byte[] getByteArray(String colName) {
551-
return getPrimitiveArray(colName);
565+
try {
566+
return getPrimitiveArray(colName, byte.class);
567+
} catch (ClassCastException | IllegalArgumentException e) {
568+
throw new ClientException("Value cannot be converted to an array of primitives", e);
569+
}
552570
}
553571

554572
@Override
555573
public int[] getIntArray(String colName) {
556-
return getPrimitiveArray(colName);
574+
try {
575+
return getPrimitiveArray(colName, int.class);
576+
} catch (ClassCastException | IllegalArgumentException e) {
577+
throw new ClientException("Value cannot be converted to an array of primitives", e);
578+
}
557579
}
558580

559581
@Override
560582
public long[] getLongArray(String colName) {
561-
return getPrimitiveArray(colName);
583+
try {
584+
return getPrimitiveArray(colName, long.class);
585+
} catch (ClassCastException | IllegalArgumentException e) {
586+
throw new ClientException("Value cannot be converted to an array of primitives", e);
587+
}
562588
}
563589

564590
@Override
565591
public float[] getFloatArray(String colName) {
566-
return getPrimitiveArray(colName);
592+
try {
593+
return getPrimitiveArray(colName, float.class);
594+
} catch (ClassCastException | IllegalArgumentException e) {
595+
throw new ClientException("Value cannot be converted to an array of primitives", e);
596+
}
567597
}
568598

569599
@Override
570600
public double[] getDoubleArray(String colName) {
571-
return getPrimitiveArray(colName);
601+
try {
602+
return getPrimitiveArray(colName, double.class);
603+
} catch (ClassCastException | IllegalArgumentException e) {
604+
throw new ClientException("Value cannot be converted to an array of primitives", e);
605+
}
572606
}
573607

574608
@Override
575609
public boolean[] getBooleanArray(String colName) {
576-
return getPrimitiveArray(colName);
610+
try {
611+
return getPrimitiveArray(colName, boolean.class);
612+
} catch (ClassCastException | IllegalArgumentException e) {
613+
throw new ClientException("Value cannot be converted to an array of primitives", e);
614+
}
577615
}
578616

579617
@Override
@@ -693,32 +731,32 @@ public <T> List<T> getList(int index) {
693731

694732
@Override
695733
public byte[] getByteArray(int index) {
696-
return getPrimitiveArray(schema.columnIndexToName(index));
734+
return getByteArray(schema.columnIndexToName(index));
697735
}
698736

699737
@Override
700738
public int[] getIntArray(int index) {
701-
return getPrimitiveArray(schema.columnIndexToName(index));
739+
return getIntArray(schema.columnIndexToName(index));
702740
}
703741

704742
@Override
705743
public long[] getLongArray(int index) {
706-
return getPrimitiveArray(schema.columnIndexToName(index));
744+
return getLongArray(schema.columnIndexToName(index));
707745
}
708746

709747
@Override
710748
public float[] getFloatArray(int index) {
711-
return getPrimitiveArray(schema.columnIndexToName(index));
749+
return getFloatArray(schema.columnIndexToName(index));
712750
}
713751

714752
@Override
715753
public double[] getDoubleArray(int index) {
716-
return getPrimitiveArray(schema.columnIndexToName(index));
754+
return getDoubleArray(schema.columnIndexToName(index));
717755
}
718756

719757
@Override
720758
public boolean[] getBooleanArray(int index) {
721-
return getPrimitiveArray(schema.columnIndexToName(index));
759+
return getBooleanArray(schema.columnIndexToName(index));
722760
}
723761

724762
@Override

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/MapBackedRecord.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ public <T> List<T> getList(String colName) {
199199
Object value = readValue(colName);
200200
if (value instanceof BinaryStreamReader.ArrayValue) {
201201
return ((BinaryStreamReader.ArrayValue) value).asList();
202+
} else if (value instanceof List<?>) {
203+
return (List<T>) value;
202204
} else {
203205
throw new ClientException("Column is not of array type");
204206
}

client-v2/src/main/java/com/clickhouse/client/api/internal/StopWatch.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public long getElapsedTime() {
3333
return TimeUnit.NANOSECONDS.toMillis(elapsedNanoTime);
3434
}
3535

36+
public long getElapsedNanos() {
37+
return elapsedNanoTime;
38+
}
39+
3640
@Override
3741
public String toString() {
3842
return "{" +

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.clickhouse.client.api.insert.InsertResponse;
1919
import com.clickhouse.client.api.insert.InsertSettings;
2020
import com.clickhouse.client.api.internal.ServerSettings;
21+
import com.clickhouse.client.api.internal.StopWatch;
2122
import com.clickhouse.client.api.metadata.TableSchema;
2223
import com.clickhouse.client.api.metrics.ClientMetrics;
2324
import com.clickhouse.client.api.metrics.OperationMetrics;
@@ -571,6 +572,35 @@ public void testArrayValues() throws Exception {
571572
Assert.assertEquals(reader.getList("col5"), ((List)data.get(0).get("col5")));
572573
}
573574

575+
576+
@Test
577+
public void testPrimitiveArrays() throws Exception {
578+
final String table = "primitive_arrays_test_table";
579+
580+
try (QueryResponse response = client.query("SELECT [1, 2, 3]::Array(UInt32) as arr").get()) {
581+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
582+
583+
reader.next();
584+
Assert.assertEquals(reader.readValue(1).getClass(), BinaryStreamReader.ArrayValue.class);
585+
Assert.assertEquals(reader.getList(1).get(0).getClass(), Long.class);
586+
Assert.assertEquals(reader.getList(1), Arrays.asList((long)1, (long)2, (long)3));
587+
Assert.assertEquals(reader.getLongArray(1), new long[]{1, 2, 3});
588+
Assert.assertThrows(ClientException.class, () -> reader.getIntArray(1));
589+
}
590+
591+
try (Client client1 = newClient().typeHintMapping(Collections.singletonMap(ClickHouseDataType.Array, List.class)).build();
592+
QueryResponse response = client1.query("SELECT [1, 2, 3]::Array(UInt32) as arr").get()) {
593+
ClickHouseBinaryFormatReader reader = client1.newBinaryFormatReader(response);
594+
595+
reader.next();
596+
Assert.assertEquals(reader.readValue(1).getClass(), ArrayList.class);
597+
Assert.assertEquals(reader.getList(1).get(0).getClass(), Long.class);
598+
Assert.assertEquals(reader.getList(1), Arrays.asList((long)1, (long)2, (long)3));
599+
Assert.assertEquals(reader.getLongArray(1), new long[]{1, 2, 3});
600+
Assert.assertThrows(ClientException.class, () -> reader.getIntArray(1));
601+
}
602+
}
603+
574604
@Test
575605
public void testArraysAsList() {
576606
GenericRecord record =
@@ -1405,7 +1435,7 @@ public void testQueryMetrics() throws Exception {
14051435
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), rowsToInsert); // 10 rows in the table
14061436
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
14071437
Assert.assertEquals(response.getReadRows(), rowsToInsert);
1408-
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
1438+
Assert.assertTrue(((StopWatch)metrics.getMetric(ClientMetrics.OP_DURATION)).getElapsedNanos() > 0);
14091439
Assert.assertEquals(metrics.getQueryId(), uuid);
14101440
Assert.assertEquals(response.getQueryId(), uuid);
14111441
}

0 commit comments

Comments
 (0)