Skip to content

Commit

Permalink
Convert byte[] to Boolean[] and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Jul 8, 2022
1 parent 57d8d76 commit 8cc6033
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
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,15 +464,17 @@ 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++) {
Object o = value[i];
if (value[i] instanceof Boolean) {
values[i] = (Boolean) o ? (byte) 1 : (byte) 0;
} else {
values[i] = o == null ? 0 : ((Number) o).byteValue();
}
values[i] = o == null ? 0 : ((Number) o).byteValue();
}
set(values);
}
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 });
}
}

0 comments on commit 8cc6033

Please sign in to comment.