Skip to content

Commit

Permalink
convert long[] to BigInteger[] - #984
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Jul 6, 2022
1 parent 82eb49a commit 890f602
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Wraper class of long.
*/
public class ClickHouseLongValue implements ClickHouseValue {
private static final BigInteger MASK = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);
public static final BigInteger MASK = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);

/**
* Create a new instance representing null value of long.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseLongValue;
import com.clickhouse.client.data.ClickHouseObjectValue;

/**
Expand Down Expand Up @@ -91,11 +92,23 @@ public Object[] asArray() {
public <E> E[] asArray(Class<E> clazz) {
long[] 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 == BigInteger.class) {
BigInteger[] array = new BigInteger[len];
for (int i = 0; i < len; i++) {
long value = v[i];
array[i] = BigInteger.valueOf(value);
if (value < 0L) {
array[i] = array[i].and(ClickHouseLongValue.MASK);
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.clickhouse.client.data.array;

import java.math.BigInteger;

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

public class ClickHouseLongArrayValueTest {
@Test(groups = { "unit" })
public void testConvertToBigInteger() throws Exception {
ClickHouseLongArrayValue v = ClickHouseLongArrayValue
.of(new long[] { 1L, new BigInteger("9223372036854775808").longValue() });
Assert.assertArrayEquals(v.getValue(), new long[] { 1L, -9223372036854775808L });
Assert.assertArrayEquals(v.asArray(BigInteger.class),
new BigInteger[] { BigInteger.ONE, new BigInteger("9223372036854775808") });
}
}

0 comments on commit 890f602

Please sign in to comment.