Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes number cast exception for ClickHouseArrayValue with Boolean[] #985

Merged
merged 3 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ public Object[] asArray() {
public <E> E[] asArray(Class<E> clazz) {
byte[] v = getValue();
int len = v.length;
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
for (int i = 0; i < len; i++) {
array[i] = clazz.cast(v[i]);
if (clazz == Boolean.class) {
Boolean[] array = new Boolean[len];
for (int i = 0; i < len; i++) {
array[i] = v[i] == (byte) 1 ? Boolean.TRUE : Boolean.FALSE;
}
return (E[]) array;
} else {
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
for (int i = 0; i < len; i++) {
array[i] = clazz.cast(v[i]);
}
return array;
}
return array;
}

@Override
Expand Down Expand Up @@ -456,6 +464,12 @@ public ClickHouseByteArrayValue update(Object[] value) {
int len = value == null ? 0 : value.length;
if (len == 0) {
return resetToNullOrEmpty();
} else if (value instanceof Boolean[]) {
byte[] values = new byte[len];
for (int i = 0; i < len; i++) {
values[i] = Boolean.TRUE.equals(value[i]) ? (byte) 1 : (byte) 0;
}
set(values);
} else {
byte[] values = new byte[len];
for (int i = 0; i < len; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ public void testSerializeArray() throws IOException {
0x63, 0, 2, 1, 0));
}

@Test(groups = { "unit" })
public void testSerializeBoolean() throws IOException {
ClickHouseConfig config = new ClickHouseConfig();

ClickHouseValue value = ClickHouseArrayValue.of(new Boolean[][] { new Boolean[] { true, false } });
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ClickHouseOutputStream out = ClickHouseOutputStream.of(bas);
ClickHouseRowBinaryProcessor.getMappedFunctions().serialize(value, config,
ClickHouseColumn.of("a", "Array(Array(Boolean))"), out);
out.flush();
Assert.assertEquals(bas.toByteArray(), BinaryStreamUtilsTest.generateBytes(1, 2, 1, 0));

boolean[] nativeBoolArray = new boolean[3];
nativeBoolArray[0] = true;
nativeBoolArray[1] = false;
nativeBoolArray[2] = true;

ClickHouseValue value2 = ClickHouseArrayValue.of(new boolean[][]{nativeBoolArray});
ByteArrayOutputStream bas2 = new ByteArrayOutputStream();
ClickHouseOutputStream out2 = ClickHouseOutputStream.of(bas2);
ClickHouseRowBinaryProcessor.getMappedFunctions().serialize(value2, config,
ClickHouseColumn.of("a", "Array(Array(boolean))"), out2);
out2.flush();
Assert.assertEquals(bas2.toByteArray(), BinaryStreamUtilsTest.generateBytes(1, 3, 1, 0, 1));
}

@Test(groups = { "unit" })
public void testDeserializeMap() throws IOException {
ClickHouseConfig config = new ClickHouseConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.clickhouse.client.data.array;

import org.junit.Assert;
import org.testng.annotations.Test;

public class ClickHouseByteArrayValueTest {
@Test(groups = { "unit" })
public void testConvertToBoolean() throws Exception {
ClickHouseByteArrayValue v = ClickHouseByteArrayValue
.of(new byte[] { 0, 1, -1 });
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 1, -1 });
Assert.assertArrayEquals(v.asArray(Boolean.class), new Boolean[] { false, true, false });
}

@Test(groups = { "unit" })
public void testConvertFromBoolean() throws Exception {
ClickHouseByteArrayValue v = ClickHouseByteArrayValue.ofEmpty();
Assert.assertArrayEquals(v.getValue(), new byte[0]);
v.update(new boolean[] { false, true, false });
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 1, 0 });
v.resetToNullOrEmpty();
Assert.assertArrayEquals(v.getValue(), new byte[0]);
v.update(new Boolean[] { Boolean.FALSE, Boolean.FALSE, Boolean.TRUE });
Assert.assertArrayEquals(v.getValue(), new byte[] { 0, 0, 1 });
}
}