Skip to content

Commit 4be83e5

Browse files
authored
Merge pull request #985 from subkanthi/fix_clickhousearrayvalue_Boolean
Fixes number cast exception for ClickHouseArrayValue with Boolean[]
2 parents 3129c3c + 8cc6033 commit 4be83e5

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ public Object[] asArray() {
9191
public <E> E[] asArray(Class<E> clazz) {
9292
byte[] v = getValue();
9393
int len = v.length;
94-
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
95-
for (int i = 0; i < len; i++) {
96-
array[i] = clazz.cast(v[i]);
94+
if (clazz == Boolean.class) {
95+
Boolean[] array = new Boolean[len];
96+
for (int i = 0; i < len; i++) {
97+
array[i] = v[i] == (byte) 1 ? Boolean.TRUE : Boolean.FALSE;
98+
}
99+
return (E[]) array;
100+
} else {
101+
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
102+
for (int i = 0; i < len; i++) {
103+
array[i] = clazz.cast(v[i]);
104+
}
105+
return array;
97106
}
98-
return array;
99107
}
100108

101109
@Override
@@ -461,6 +469,12 @@ public ClickHouseByteArrayValue update(Object[] value) {
461469
int len = value == null ? 0 : value.length;
462470
if (len == 0) {
463471
return resetToNullOrEmpty();
472+
} else if (value instanceof Boolean[]) {
473+
byte[] values = new byte[len];
474+
for (int i = 0; i < len; i++) {
475+
values[i] = Boolean.TRUE.equals(value[i]) ? (byte) 1 : (byte) 0;
476+
}
477+
set(values);
464478
} else {
465479
byte[] values = new byte[len];
466480
for (int i = 0; i < len; i++) {

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

Lines changed: 26 additions & 0 deletions
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();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.clickhouse.client.data.array;
2+
3+
import org.junit.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class ClickHouseByteArrayValueTest {
7+
@Test(groups = { "unit" })
8+
public void testConvertToBoolean() throws Exception {
9+
ClickHouseByteArrayValue v = ClickHouseByteArrayValue
10+
.of(new byte[] { 0, 1, -1 });
11+
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 1, -1 });
12+
Assert.assertArrayEquals(v.asArray(Boolean.class), new Boolean[] { false, true, false });
13+
}
14+
15+
@Test(groups = { "unit" })
16+
public void testConvertFromBoolean() throws Exception {
17+
ClickHouseByteArrayValue v = ClickHouseByteArrayValue.ofEmpty();
18+
Assert.assertArrayEquals(v.getValue(), new byte[0]);
19+
v.update(new boolean[] { false, true, false });
20+
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 1, 0 });
21+
v.resetToNullOrEmpty();
22+
Assert.assertArrayEquals(v.getValue(), new byte[0]);
23+
v.update(new Boolean[] { Boolean.FALSE, Boolean.FALSE, Boolean.TRUE });
24+
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 0, 1 });
25+
}
26+
}

0 commit comments

Comments
 (0)