Skip to content
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
@@ -1,10 +1,13 @@
package com.clickhouse.client;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class ClickHouseEnum {
public class ClickHouseEnum implements Serializable {
private static final long serialVersionUID = -978231193821209918L;

public static final ClickHouseEnum EMPTY = new ClickHouseEnum(Collections.emptyList());

public static ClickHouseEnum of(Class<? extends Enum> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.temporal.ChronoField;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
Expand Down Expand Up @@ -107,6 +108,7 @@ public final class ClickHouseValues {
public static final String INF_EXPR = "Inf";
public static final String NINF_EXPR = "-Inf";

public static final String ERROR_INF_OR_NAN = "Infinite or NaN";
public static final String ERROR_INVALID_POINT = "A point should have two and only two double values, but we got: ";
public static final String ERROR_SINGLETON_ARRAY = "Only singleton array is allowed, but we got: ";
public static final String ERROR_SINGLETON_COLLECTION = "Only singleton collection is allowed, but we got: ";
Expand Down Expand Up @@ -970,6 +972,46 @@ public static Object extractSingleValue(Map<?, ?> value) {
return value.values().iterator().next();
}

/**
* Creates multiple values based on given columns.
*
* @param config non-null configuration
* @param columns non-null columns
* @return non-null values with default value, either null or empty
*/
public static ClickHouseValue[] newValues(ClickHouseConfig config, List<ClickHouseColumn> columns) {
if (columns == null || columns.isEmpty()) {
return EMPTY_VALUES;
}

ClickHouseValue[] values = new ClickHouseValue[columns.size()];
int index = 0;
for (ClickHouseColumn c : columns) {
values[index++] = newValue(config, c);
}
return values;
}

/**
* Creates multiple values based on given columns.
*
* @param config non-null configuration
* @param columns non-null columns
* @return non-null values with default value, either null or empty
*/
public static ClickHouseValue[] newValues(ClickHouseConfig config, ClickHouseColumn[] columns) {
if (columns == null || columns.length == 0) {
return EMPTY_VALUES;
}

int len = columns.length;
ClickHouseValue[] values = new ClickHouseValue[len];
for (int i = 0; i < len; i++) {
values[i] = newValue(config, columns[i]);
}
return values;
}

/**
* Creates a value object based on given column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,26 @@ public double asDouble() {
return value;
}

@Override
public BigDecimal asBigDecimal() {
if (isNull) {
return null;
} else if (Double.isNaN(value) || value == Double.POSITIVE_INFINITY || value == Double.NEGATIVE_INFINITY) {
throw new NumberFormatException(ClickHouseValues.ERROR_INF_OR_NAN);
} else if (value == 0D) {
return BigDecimal.ZERO;
} else if (value == 1D) {
return BigDecimal.ONE;
}
return new BigDecimal(Double.toString(value));
}

@Override
public BigDecimal asBigDecimal(int scale) {
if (isNull) {
return null;
} else if (Double.isNaN(value) || value == Double.POSITIVE_INFINITY || value == Double.NEGATIVE_INFINITY) {
throw new NumberFormatException(ClickHouseValues.ERROR_INF_OR_NAN);
}

BigDecimal dec = BigDecimal.valueOf(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,26 @@ public double asDouble() {
return value;
}

@Override
public BigDecimal asBigDecimal() {
if (isNull) {
return null;
} else if (Float.isNaN(value) || value == Float.POSITIVE_INFINITY || value == Float.NEGATIVE_INFINITY) {
throw new NumberFormatException(ClickHouseValues.ERROR_INF_OR_NAN);
} else if (value == 0F) {
return BigDecimal.ZERO;
} else if (value == 1F) {
return BigDecimal.ONE;
}
return new BigDecimal(Float.toString(value));
}

@Override
public BigDecimal asBigDecimal(int scale) {
if (isNull) {
return null;
} else if (Float.isNaN(value) || value == Float.POSITIVE_INFINITY || value == Float.NEGATIVE_INFINITY) {
throw new NumberFormatException(ClickHouseValues.ERROR_INF_OR_NAN);
}

BigDecimal dec = new BigDecimal(Float.toString(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testValue() throws Exception {
0L, // long
0F, // float
0D, // double
BigDecimal.valueOf(0L), // BigDecimal
BigDecimal.ZERO, // BigDecimal
new BigDecimal(BigInteger.ZERO, 3), // BigDecimal
BigInteger.ZERO, // BigInteger
ClickHouseDataType.values()[0].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testValue() throws Exception {
1L, // long
1F, // float
1D, // double
BigDecimal.valueOf(1L), // BigDecimal
BigDecimal.ONE, // BigDecimal
new BigDecimal(BigInteger.ONE, 3), // BigDecimal
BigInteger.ONE, // BigInteger
ClickHouseDataType.values()[1].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -196,7 +196,7 @@ public void testValue() throws Exception {
2L, // long
2F, // float
2D, // double
BigDecimal.valueOf(2L), // BigDecimal
BigDecimal.valueOf(2D), // BigDecimal
new BigDecimal(BigInteger.valueOf(2L), 3), // BigDecimal
BigInteger.valueOf(2L), // BigInteger
ClickHouseDataType.values()[2].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -229,7 +229,7 @@ public void testValue() throws Exception {
-1L, // long
-1F, // float
-1D, // double
BigDecimal.valueOf(-1L), // BigDecimal
BigDecimal.valueOf(-1D), // BigDecimal
new BigDecimal(BigInteger.valueOf(-1L), 3), // BigDecimal
BigInteger.valueOf(-1L), // BigInteger
IllegalArgumentException.class, // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -262,7 +262,7 @@ public void testValue() throws Exception {
1L, // long
1.3333334F, // float
1.3333333333333333D, // double
BigDecimal.valueOf(1L), // BigDecimal
BigDecimal.valueOf(4D / 3), // BigDecimal
BigDecimal.valueOf(1333, 3), // BigDecimal
BigInteger.ONE, // BigInteger
ClickHouseDataType.values()[1].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -294,7 +294,7 @@ public void testValue() throws Exception {
-1L, // long
-1.3333334F, // float
-1.3333333333333333D, // double
BigDecimal.valueOf(-1L), // BigDecimal
BigDecimal.valueOf(-4D / 3), // BigDecimal
BigDecimal.valueOf(-1333, 3), // BigDecimal
BigInteger.valueOf(-1L), // BigInteger
IllegalArgumentException.class, // Enum<ClickHouseDataType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testValue() throws Exception {
0L, // long
0F, // float
0D, // double
BigDecimal.valueOf(0L), // BigDecimal
BigDecimal.ZERO, // BigDecimal
new BigDecimal(BigInteger.ZERO, 3), // BigDecimal
BigInteger.ZERO, // BigInteger
ClickHouseDataType.values()[0].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -163,7 +163,7 @@ public void testValue() throws Exception {
1L, // long
1F, // float
1D, // double
BigDecimal.valueOf(1L), // BigDecimal
BigDecimal.ONE, // BigDecimal
new BigDecimal(BigInteger.ONE, 3), // BigDecimal
BigInteger.ONE, // BigInteger
ClickHouseDataType.values()[1].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -195,7 +195,7 @@ public void testValue() throws Exception {
2L, // long
2F, // float
2D, // double
BigDecimal.valueOf(2L), // BigDecimal
BigDecimal.valueOf(2F), // BigDecimal
new BigDecimal(BigInteger.valueOf(2L), 3), // BigDecimal
BigInteger.valueOf(2L), // BigInteger
ClickHouseDataType.values()[2].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testValue() throws Exception {
-1L, // long
-1F, // float
-1D, // double
BigDecimal.valueOf(-1L), // BigDecimal
BigDecimal.valueOf(-1F), // BigDecimal
new BigDecimal(BigInteger.valueOf(-1L), 3), // BigDecimal
BigInteger.valueOf(-1L), // BigInteger
IllegalArgumentException.class, // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -261,7 +261,7 @@ public void testValue() throws Exception {
1L, // long
1.3333334F, // float
(double) (4F / 3), // double
BigDecimal.valueOf(1L), // BigDecimal
new BigDecimal(Float.toString(4F / 3)), // BigDecimal
BigDecimal.valueOf(1333, 3), // BigDecimal
BigInteger.ONE, // BigInteger
ClickHouseDataType.values()[1].name(), // Enum<ClickHouseDataType>
Expand Down Expand Up @@ -293,7 +293,7 @@ public void testValue() throws Exception {
-1L, // long
-1.3333334F, // float
(double) (-4F / 3), // double
BigDecimal.valueOf(-1L), // BigDecimal
new BigDecimal(Float.toString(-4F / 3)), // BigDecimal
BigDecimal.valueOf(-1333, 3), // BigDecimal
BigInteger.valueOf(-1L), // BigInteger
IllegalArgumentException.class, // Enum<ClickHouseDataType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public void testFloatToBigDecimal() throws SQLException {
+ "toDecimal64(-1.35, 1) n1, toDecimal64(-1.35, 2) n2")) {
while (rs.next()) {
ClickHouseRecord r = rs.unwrap(ClickHouseRecord.class);
Assert.assertEquals(r.getValue("fp").asBigDecimal(), r.getValue("p2").asObject());
Assert.assertEquals(r.getValue("fn").asBigDecimal(), r.getValue("n2").asObject());
Assert.assertEquals(r.getValue("dp").asBigDecimal(), r.getValue("p2").asObject());
Assert.assertEquals(r.getValue("dn").asBigDecimal(), r.getValue("n2").asObject());
for (int i = 1; i <= 2; i++) {
Assert.assertEquals(r.getValue("fp").asBigDecimal(i), r.getValue("p" + i).asObject());
Assert.assertEquals(r.getValue("fn").asBigDecimal(i), r.getValue("n" + i).asObject());
Expand Down