From 890f6024908928a3ac5fc40ae4bc603a4fa3eb6f Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Thu, 7 Jul 2022 05:44:00 +0800 Subject: [PATCH] convert long[] to BigInteger[] - #984 --- .../client/data/ClickHouseLongValue.java | 2 +- .../data/array/ClickHouseLongArrayValue.java | 21 +++++++++++++++---- .../array/ClickHouseLongArrayValueTest.java | 17 +++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 clickhouse-client/src/test/java/com/clickhouse/client/data/array/ClickHouseLongArrayValueTest.java diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseLongValue.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseLongValue.java index 5aabf1b38..755536235 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseLongValue.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseLongValue.java @@ -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. diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/array/ClickHouseLongArrayValue.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/array/ClickHouseLongArrayValue.java index e39fd6824..d3a899091 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/array/ClickHouseLongArrayValue.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/array/ClickHouseLongArrayValue.java @@ -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; /** @@ -91,11 +92,23 @@ public Object[] asArray() { public E[] asArray(Class 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 diff --git a/clickhouse-client/src/test/java/com/clickhouse/client/data/array/ClickHouseLongArrayValueTest.java b/clickhouse-client/src/test/java/com/clickhouse/client/data/array/ClickHouseLongArrayValueTest.java new file mode 100644 index 000000000..8aadb71b1 --- /dev/null +++ b/clickhouse-client/src/test/java/com/clickhouse/client/data/array/ClickHouseLongArrayValueTest.java @@ -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") }); + } +}