Skip to content

Commit 57d8d76

Browse files
committed
Fixes number cast exception when Boolean[] is used in ClickHouseArrayValue
1 parent ea5aaf5 commit 57d8d76

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

clickhouse-client/src/main/java/com/clickhouse/client/data/array/ClickHouseByteArrayValue.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,11 @@ public ClickHouseByteArrayValue update(Object[] value) {
460460
byte[] values = new byte[len];
461461
for (int i = 0; i < len; i++) {
462462
Object o = value[i];
463-
values[i] = o == null ? 0 : ((Number) o).byteValue();
463+
if (value[i] instanceof Boolean) {
464+
values[i] = (Boolean) o ? (byte) 1 : (byte) 0;
465+
} else {
466+
values[i] = o == null ? 0 : ((Number) o).byteValue();
467+
}
464468
}
465469
set(values);
466470
}

clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseRowBinaryProcessorTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ public void testSerializeArray() throws IOException {
175175
0x63, 0, 2, 1, 0));
176176
}
177177

178+
@Test(groups = { "unit" })
179+
public void testSerializeBoolean() throws IOException {
180+
ClickHouseConfig config = new ClickHouseConfig();
181+
182+
ClickHouseValue value = ClickHouseArrayValue.of(new Boolean[][] { new Boolean[] { true, false } });
183+
ByteArrayOutputStream bas = new ByteArrayOutputStream();
184+
ClickHouseOutputStream out = ClickHouseOutputStream.of(bas);
185+
ClickHouseRowBinaryProcessor.getMappedFunctions().serialize(value, config,
186+
ClickHouseColumn.of("a", "Array(Array(Boolean))"), out);
187+
out.flush();
188+
Assert.assertEquals(bas.toByteArray(), BinaryStreamUtilsTest.generateBytes(1, 2, 1, 0));
189+
190+
boolean[] nativeBoolArray = new boolean[3];
191+
nativeBoolArray[0] = true;
192+
nativeBoolArray[1] = false;
193+
nativeBoolArray[2] = true;
194+
195+
ClickHouseValue value2 = ClickHouseArrayValue.of(new boolean[][]{nativeBoolArray});
196+
ByteArrayOutputStream bas2 = new ByteArrayOutputStream();
197+
ClickHouseOutputStream out2 = ClickHouseOutputStream.of(bas2);
198+
ClickHouseRowBinaryProcessor.getMappedFunctions().serialize(value2, config,
199+
ClickHouseColumn.of("a", "Array(Array(boolean))"), out2);
200+
out2.flush();
201+
Assert.assertEquals(bas2.toByteArray(), BinaryStreamUtilsTest.generateBytes(1, 3, 1, 0, 1));
202+
}
203+
178204
@Test(groups = { "unit" })
179205
public void testDeserializeMap() throws IOException {
180206
ClickHouseConfig config = new ClickHouseConfig();

0 commit comments

Comments
 (0)