From 192e7a70d2715743b74c34fb981e26917374cdb4 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Sat, 7 May 2022 10:08:25 +0800 Subject: [PATCH] Fix #913 --- .../com/clickhouse/client/ClickHouseValues.java | 1 + .../client/data/ClickHouseDoubleValue.java | 16 ++++++++++++++++ .../client/data/ClickHouseFloatValue.java | 16 ++++++++++++++++ .../client/data/ClickHouseDoubleValueTest.java | 12 ++++++------ .../client/data/ClickHouseFloatValueTest.java | 12 ++++++------ .../clickhouse/jdbc/ClickHouseResultSetTest.java | 4 ++++ 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseValues.java b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseValues.java index 96096eb40..89c44b4be 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseValues.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseValues.java @@ -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: "; diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseDoubleValue.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseDoubleValue.java index beb30ae18..63e6a046e 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseDoubleValue.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseDoubleValue.java @@ -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); diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseFloatValue.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseFloatValue.java index a6cb3bb3f..279d76bd2 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseFloatValue.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseFloatValue.java @@ -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)); diff --git a/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseDoubleValueTest.java b/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseDoubleValueTest.java index 9cd5366fe..e91f7e173 100644 --- a/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseDoubleValueTest.java +++ b/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseDoubleValueTest.java @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseFloatValueTest.java b/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseFloatValueTest.java index f884f4602..63b92b932 100644 --- a/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseFloatValueTest.java +++ b/clickhouse-client/src/test/java/com/clickhouse/client/data/ClickHouseFloatValueTest.java @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseResultSetTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseResultSetTest.java index 5b1ad3dea..49ef1f95c 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseResultSetTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseResultSetTest.java @@ -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());