Skip to content

Commit 9b8cf35

Browse files
committed
Udated GenericRecord javadoc. Fixed getStringArray() to return only arrays of strings
1 parent 076d6c5 commit 9b8cf35

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.time.ZoneOffset;
2626
import java.time.ZonedDateTime;
2727
import java.time.temporal.TemporalAmount;
28+
import java.util.Arrays;
2829
import java.util.HashMap;
2930
import java.util.List;
3031
import java.util.Map;
@@ -295,19 +296,14 @@ public String[] getStringArray(String colName) {
295296
}
296297
if (value instanceof BinaryStreamReader.ArrayValue) {
297298
BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) value;
298-
int length = array.length;
299-
String[] values = new String[length];
300-
if (array.itemType.equals(String.class)) {
301-
for (int i = 0; i < length; i++) {
302-
values[i] = (String) array.get(i);
303-
}
299+
if (array.itemType == String.class) {
300+
return (String[]) array.getArray();
301+
} else if (array.itemType == BinaryStreamReader.EnumValue.class) {
302+
BinaryStreamReader.EnumValue[] enumValues = (BinaryStreamReader.EnumValue[]) array.getArray();
303+
return Arrays.stream(enumValues).map(BinaryStreamReader.EnumValue::getName).toArray(String[]::new);
304304
} else {
305-
for (int i = 0; i < length; i++) {
306-
Object item = array.get(i);
307-
values[i] = item == null ? null : item.toString();
308-
}
305+
throw new ClientException("Not an array of strings");
309306
}
310-
return values;
311307
}
312308
throw new ClientException("Column is not of array type");
313309
}
@@ -482,7 +478,7 @@ public short[] getShortArray(int index) {
482478

483479
@Override
484480
public String[] getStringArray(int index) {
485-
return getPrimitiveArray(schema.columnIndexToName(index));
481+
return getStringArray(schema.columnIndexToName(index));
486482
}
487483

488484
@Override

client-v2/src/main/java/com/clickhouse/client/api/query/GenericRecord.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,18 @@ public interface GenericRecord {
248248

249249
short[] getShortArray(String colName);
250250

251+
/**
252+
* Returns string array for columns {@code Array(String)}.
253+
* This method doesn't make a conversion of other types to string.
254+
*
255+
* @param colName - column name
256+
* @return String[]
257+
*/
251258
String[] getStringArray(String colName);
252259

253260
/**
254261
* Reads column with name `colName` as an array of objects. Works for any array element type
255-
* including non-primitive types like DateTime, Enum, UInt64 (BigInteger), FixedString, etc.
262+
* including non-primitive types like DateTime, Enum, UInt64 (BigInteger), etc.
256263
* For nested arrays, inner ArrayValue elements are recursively converted to Object[].
257264
*
258265
* @param colName - column name

client-v2/src/test/java/com/clickhouse/client/datatypes/DataTypeTests.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,10 +1332,6 @@ public void testGetObjectArrayMethods() throws Exception {
13321332
Assert.assertEquals(uint64Arr[1], java.math.BigInteger.valueOf(200));
13331333
Assert.assertEquals(uint64Arr[2], new java.math.BigInteger("18000044073709551615"));
13341334

1335-
// Array(UInt64) -> getStringArray converts via toString()
1336-
String[] uint64Strings = row1.getStringArray("uint64_arr");
1337-
Assert.assertEquals(uint64Strings, new String[]{"100", "200", "18000044073709551615"});
1338-
13391335
// Array(Enum8) -> getObjectArray returns EnumValue[]
13401336
Object[] enumArr = row1.getObjectArray("enum_arr");
13411337
Assert.assertNotNull(enumArr);
@@ -1344,10 +1340,6 @@ public void testGetObjectArrayMethods() throws Exception {
13441340
Assert.assertEquals(enumArr[0].toString(), "abc");
13451341
Assert.assertEquals(enumArr[1].toString(), "cde");
13461342

1347-
// Array(Enum8) -> getStringArray returns enum names
1348-
String[] enumStrings = row1.getStringArray("enum_arr");
1349-
Assert.assertEquals(enumStrings, new String[]{"abc", "cde"});
1350-
13511343
// Array(DateTime) -> getObjectArray returns ZonedDateTime[]
13521344
Object[] dtArr = row1.getObjectArray("dt_arr");
13531345
Assert.assertNotNull(dtArr);

0 commit comments

Comments
 (0)