Skip to content

Commit

Permalink
Fix #913
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed May 7, 2022
1 parent ea44bc5 commit 192e7a7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,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
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

0 comments on commit 192e7a7

Please sign in to comment.