Skip to content

Commit ccd24bf

Browse files
authored
Merge pull request #1955 from ClickHouse/v2_fix_number_conversion
[client-v2] removed float/double overflow check
2 parents d9cbaba + 8c2a624 commit ccd24bf

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatReader.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {
9999

100100
/**
101101
* Reads column with name `colName` as a float.
102-
*
102+
* Warning: this method may lose precision for float values.
103+
*
103104
* @param colName
104105
* @return
105106
*/
106107
float getFloat(String colName);
107108

108109
/**
109110
* Reads column with name `colName` as a double.
111+
* Warning: this method may lose precision for double values.
110112
*
111113
* @param colName
112114
* @return
@@ -321,6 +323,7 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {
321323

322324
/**
323325
* Reads column with name `colName` as a float.
326+
* Warning: this method may lose precision for float values.
324327
*
325328
* @param index
326329
* @return
@@ -329,6 +332,7 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {
329332

330333
/**
331334
* Reads column with name `colName` as a double.
335+
* Warning: this method may lose precision for double values.
332336
*
333337
* @param index
334338
* @return

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,7 @@ public static float toFloat(Object value) {
682682
if (value instanceof Float) {
683683
return (Float) value;
684684
} else if (value instanceof Number) {
685-
Number number = (Number) value;
686-
if (number.floatValue() == number.doubleValue()) {
687-
return number.floatValue();
688-
} else {
689-
throw new ArithmeticException("float overflow: " + value + " cannot be presented as float");
690-
}
685+
return ((Number) value).floatValue();
691686
} else if (value instanceof Boolean) {
692687
return (Boolean) value ? 1.0f : 0.0f;
693688
} else if (value instanceof String ) {
@@ -701,12 +696,7 @@ public static double toDouble(Object value) {
701696
if (value instanceof Double) {
702697
return (Double) value;
703698
} else if (value instanceof Number) {
704-
Number number = (Number) value;
705-
if (number.doubleValue() == number.floatValue()) {
706-
return number.doubleValue();
707-
} else {
708-
throw new ArithmeticException("double overflow: " + value + " cannot be presented as double");
709-
}
699+
return ((Number) value).doubleValue();
710700
} else if (value instanceof Boolean) {
711701
return (Boolean) value ? 1.0 : 0.0;
712702
} else if (value instanceof String) {

client-v2/src/main/java/com/clickhouse/client/api/query/GenericRecord.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public interface GenericRecord {
288288

289289
/**
290290
* Reads column with name `colName` as a float.
291+
* Warning: there is no overflow check for float.
291292
*
292293
* @param index
293294
* @return
@@ -296,6 +297,7 @@ public interface GenericRecord {
296297

297298
/**
298299
* Reads column with name `colName` as a double.
300+
* Warning: there is no overflow check for double.
299301
*
300302
* @param index
301303
* @return

client-v2/src/test/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatReaderTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public void testReadingNumbersWithOverflow() throws IOException {
8686
ByteArrayOutputStream out = new ByteArrayOutputStream();
8787

8888
String[] names = new String[]{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
89-
"q", "r"};
89+
"q", "r", "s"};
9090
String[] types = new String[]{"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
9191
"Int128", "Int256", "UInt128", "UInt256", "Float32", "Float64",
92-
"Decimal32(3)", "Decimal64(3)", "Decimal128(4)", "Decimal256(4)"};
92+
"Decimal32(3)", "Decimal64(3)", "Decimal128(4)", "Decimal256(4)", "Float64"};
9393

9494
BinaryStreamUtils.writeVarInt(out, names.length);
9595
for (String name : names) {
@@ -117,7 +117,7 @@ public void testReadingNumbersWithOverflow() throws IOException {
117117
BinaryStreamUtils.writeDecimal64(out, BigDecimal.valueOf(10000000.10000), 3); // p
118118
BinaryStreamUtils.writeDecimal128(out, BigDecimal.valueOf(1000000000.1000000), 4); // q
119119
BinaryStreamUtils.writeDecimal256(out, BigDecimal.valueOf(1000000000.1000000), 4); // r
120-
120+
BinaryStreamUtils.writeFloat64(out, 123.456); // s
121121

122122
InputStream in = new ByteArrayInputStream(out.toByteArray());
123123
QuerySettings querySettings = new QuerySettings().setUseTimeZone(TimeZone.getTimeZone("UTC").toZoneId().getId());
@@ -130,15 +130,23 @@ public void testReadingNumbersWithOverflow() throws IOException {
130130
Consumer<String> shortConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getShort(name));
131131
Consumer<String> integerConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getInteger(name));
132132
Consumer<String> longConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getLong(name));
133-
Consumer<String> floatConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getFloat(name));
134-
Consumer<String> doubleConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getDouble(name));
133+
// Consumer<String> floatConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getFloat(name));
134+
// Consumer<String> doubleConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getDouble(name));
135135

136136
Arrays.stream("b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r".split(",")).forEach(byteConsumer);
137137
Arrays.stream("c,d,f,g,h,i,j,k,l,m,n,o,p,q,r".split(",")).forEach(shortConsumer);
138138
Arrays.stream("d,g,h,i,j,k,l".split(",")).forEach(integerConsumer);
139139
Arrays.stream("h,i,j,k,l".split(",")).forEach(longConsumer);
140-
Arrays.stream("h,i,j,k,l,n,p,q,r".split(",")).forEach(floatConsumer);
141-
Arrays.stream("h,i,j,k,l,p,q,r".split(",")).forEach(doubleConsumer);
140+
// Arrays.stream("h,i,j,k,l,n,p,q,r".split(",")).forEach(floatConsumer);
141+
// Arrays.stream("h,i,j,k,l,p,q,r".split(",")).forEach(doubleConsumer);
142+
143+
Assert.assertEquals(reader.getFloat("m"), 900000.123f);
144+
Assert.assertEquals(reader.getDouble("m"), 900000.123f);
145+
146+
Assert.assertEquals(reader.getBigDecimal("n"), BigDecimal.valueOf(1000000.333));
147+
Assert.assertEquals(reader.getBigDecimal("n"), BigDecimal.valueOf(1000000.333));
148+
149+
Assert.assertEquals(reader.getFloat("s"), 123.456f);
142150
}
143151

144152
@Test

client-v2/src/test/java/com/clickhouse/client/internal/SmallTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.testng.Assert;
77
import org.testng.annotations.Test;
88

9+
import java.math.BigDecimal;
10+
911
public class SmallTests {
1012

1113

0 commit comments

Comments
 (0)